-
Notifications
You must be signed in to change notification settings - Fork 3
84 facade migration t3 major #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DjesikaV
wants to merge
13
commits into
main
Choose a base branch
from
84-Facade-Migration-T3-major
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9672935
add MajorValidator
acbd514
simplify MajorService
7fb32be
migrate to facade
fdd2a93
refactor MajorServiceTest
eac357b
add MajorValidatorTest and MajorWebFacadeTest
beeb324
remove validations from MajorResponseDto
5bc5973
add validation messages to MajorRequestDto
89c9b82
combine validation logic for create and update into a single method
703ab2a
Merge branch 'main' into 84-Facade-Migration-T3-major
DjesikaV f5e7c56
Merge branch 'main' into 84-Facade-Migration-T3-major
DjesikaV 79cb492
map facultyId in MajorMapper, remove misleading repository methods
d5a7aef
Merge branch '84-Facade-Migration-T3-major' of https://github.com/uni…
813924a
add tests for findAllMajorWithCoursesByFacultyId
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,43 @@ | ||
| package org.unilab.uniplan.major; | ||
|
|
||
| import org.unilab.uniplan.major.dto.MajorCoursesDto; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.MAJOR_NOT_FOUND; | ||
|
|
||
| import jakarta.transaction.Transactional; | ||
| import org.unilab.uniplan.common.model.BaseService; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.unilab.uniplan.exception.ResourceNotFoundException; | ||
| import org.unilab.uniplan.major.dto.MajorDto; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class MajorService { | ||
| public class MajorService implements BaseService<Major> { | ||
|
|
||
| private final MajorRepository majorRepository; | ||
| private final MajorMapper majorMapper; | ||
|
|
||
| @Transactional | ||
| public MajorDto createMajor(final MajorDto majorDTO) { | ||
| final Major major = majorMapper.toEntity(majorDTO); | ||
| return saveEntityAndConvertToDto(major); | ||
| } | ||
|
|
||
| public MajorDto findMajorById(final UUID id) { | ||
| return majorRepository.findById(id) | ||
| .map(majorMapper::toDto) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| MAJOR_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| } | ||
|
|
||
| public MajorCoursesDto findMajorWithCoursesById(final UUID id) { | ||
| return majorRepository.findById(id) | ||
| .map(majorMapper::toFullDto) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| MAJOR_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| } | ||
|
|
||
| public List<MajorDto> findAll() { | ||
| return majorRepository.findAll() | ||
| .stream().map(majorMapper::toDto).toList(); | ||
| } | ||
|
|
||
| public List<MajorDto> findAllMajorByFacultyId(final UUID facultyId) { | ||
| return majorRepository.findAllByFacultyId(facultyId) | ||
| .stream() | ||
| .map(majorMapper::toDto) | ||
| .toList(); | ||
| @Override | ||
| public void save(final Major major) { | ||
| majorRepository.save(major); | ||
| } | ||
|
|
||
| public List<MajorCoursesDto> findAllMajorWithCoursesByFacultyId(final UUID facultyId) { | ||
| return majorRepository.findAllByFacultyId(facultyId) | ||
| .stream() | ||
| .map(majorMapper::toFullDto) | ||
| .toList(); | ||
| @Override | ||
| public Optional<Major> getById(final UUID id) { | ||
| return majorRepository.findById(id); | ||
| } | ||
|
|
||
| @Transactional | ||
| public MajorDto updateMajor(final UUID id, final MajorDto majorDTO) { | ||
| return majorRepository.findById(id).map(existingMajor -> updateEntityAndConvertToDto( | ||
| majorDTO, | ||
| existingMajor)) | ||
| .orElseThrow(() -> new ResourceNotFoundException(MAJOR_NOT_FOUND.getMessage( | ||
| String.valueOf(id)))); | ||
| @Override | ||
| public List<Major> getAll() { | ||
| return majorRepository.findAll(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteMajor(final UUID id) { | ||
| final Major major = majorRepository.findById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| MAJOR_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| @Override | ||
| public void delete(final Major major) { | ||
| majorRepository.delete(major); | ||
| } | ||
|
|
||
| private MajorDto updateEntityAndConvertToDto(final MajorDto dto, | ||
| final Major entity) { | ||
| majorMapper.updateEntityFromDto(dto, entity); | ||
| return saveEntityAndConvertToDto(entity); | ||
| public List<Major> findAllMajorByFacultyId(final UUID facultyId) { | ||
| return majorRepository.findAllByFacultyId(facultyId); | ||
| } | ||
|
|
||
| private MajorDto saveEntityAndConvertToDto(final Major entity) { | ||
| final Major savedEntity = majorRepository.save(entity); | ||
| return majorMapper.toDto(savedEntity); | ||
| public List<Major> findAllMajorWithCoursesByFacultyId(final UUID facultyId) { | ||
| return majorRepository.findAllMajorWithCoursesByFacultyId(facultyId); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/org/unilab/uniplan/major/MajorValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package org.unilab.uniplan.major; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.FACULTY_NOT_FOUND; | ||
|
|
||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import org.unilab.uniplan.exception.ResourceNotFoundException; | ||
| import org.unilab.uniplan.faculty.FacultyRepository; | ||
| import org.unilab.uniplan.major.dto.MajorRequestDto; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class MajorValidator { | ||
|
|
||
| private final FacultyRepository facultyRepository; | ||
|
|
||
| public void validate(final MajorRequestDto requestDto) { | ||
| validateFacultyExists(requestDto.facultyId()); | ||
| } | ||
|
|
||
| private void validateFacultyExists(final UUID facultyId) { | ||
| if (!facultyRepository.existsById(facultyId)) { | ||
| throw new ResourceNotFoundException( | ||
| FACULTY_NOT_FOUND.getMessage(String.valueOf(facultyId)) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
src/main/java/org/unilab/uniplan/major/MajorWebFacade.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package org.unilab.uniplan.major; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.MAJOR_NOT_FOUND; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.unilab.uniplan.exception.ResourceNotFoundException; | ||
| import org.unilab.uniplan.major.dto.MajorCoursesResponseDto; | ||
| import org.unilab.uniplan.major.dto.MajorRequestDto; | ||
| import org.unilab.uniplan.major.dto.MajorResponseDto; | ||
|
|
||
| @Component | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class MajorWebFacade { | ||
|
|
||
| private final MajorService majorService; | ||
| private final MajorMapper majorMapper; | ||
| private final MajorValidator majorValidator; | ||
|
|
||
| @Transactional | ||
| public void createMajor(final MajorRequestDto requestDto) { | ||
| majorValidator.validate(requestDto); | ||
|
|
||
| final Major major = majorMapper.toEntity(requestDto); | ||
| majorService.save(major); | ||
|
|
||
| log.info("Created major with ID: {}", major.getId()); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<MajorResponseDto> getAllMajors() { | ||
| return majorMapper.toResponseDtoList(majorService.getAll()); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public MajorResponseDto getMajorById(final UUID id) { | ||
| final Major major = getMajorOrThrow(id); | ||
|
|
||
| return majorMapper.toResponseDto(major); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateMajor(final UUID id, | ||
| final MajorRequestDto requestDto) { | ||
| majorValidator.validate(requestDto); | ||
|
|
||
| final Major major = getMajorOrThrow(id); | ||
| majorMapper.updateEntityFromDto(requestDto, major); | ||
| majorService.save(major); | ||
|
|
||
| log.info("Updated major with ID: {}", id); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteMajor(final UUID id) { | ||
| final Major major = getMajorOrThrow(id); | ||
|
|
||
| majorService.delete(major); | ||
|
|
||
| log.info("Deleted major with ID: {}", id); | ||
| } | ||
|
|
||
| private Major getMajorOrThrow(final UUID id) { | ||
| return majorService.getById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| MAJOR_NOT_FOUND.getMessage(String.valueOf(id)) | ||
| )); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public MajorCoursesResponseDto getMajorWithCoursesById(final UUID id) { | ||
| final Major major = getMajorOrThrow(id); | ||
|
|
||
| return majorMapper.toFullResponseDto(major); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<MajorResponseDto> getMajorsByFacultyId(final UUID facultyId) { | ||
| return majorMapper.toResponseDtoList( | ||
| majorService.findAllMajorByFacultyId(facultyId) | ||
| ); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<MajorCoursesResponseDto> getMajorsWithCoursesByFacultyId(final UUID facultyId) { | ||
| return majorMapper.toFullResponseDtoList( | ||
| majorService.findAllMajorWithCoursesByFacultyId(facultyId) | ||
| ); | ||
| } | ||
| } |
7 changes: 4 additions & 3 deletions
7
src/main/java/org/unilab/uniplan/major/dto/MajorRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.