-
Notifications
You must be signed in to change notification settings - Fork 3
Facade migration t3/ migrate lector to facade pattern #124
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
DrDeathDrop
wants to merge
9
commits into
main
Choose a base branch
from
Facade-migration-T3/-Migrate-lector-to-facade-pattern
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
9 commits
Select commit
Hold shift + click to select a range
c0ba3fc
Migrated LectorWebFacade, written tests for it, updated controller an…
ae8f2d3
Rewritten log that had a mistake
5a9d4ae
fixed broken edit faculty on lector, added mapper tests
b182de4
fixed broken edit faculty on lector, added mapper tests
e2f3f72
Added validator and tests for the validator
162ad29
Added validator and tests for the validator
d2ac09c
Merge branch 'main' into Facade-migration-T3/-Migrate-lector-to-facad…
DrDeathDrop f40ec26
Fixed validator now uses LectorRequestDto instead of Lector entity, r…
d927f02
Merge branch 'Facade-migration-T3/-Migrate-lector-to-facade-pattern' …
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
28 changes: 13 additions & 15 deletions
28
src/main/java/org/unilab/uniplan/lector/LectorMapper.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 |
|---|---|---|
| @@ -1,37 +1,35 @@ | ||
| package org.unilab.uniplan.lector; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.MappingTarget; | ||
| import org.unilab.uniplan.faculty.Faculty; | ||
| import org.unilab.uniplan.lector.dto.LectorDto; | ||
| import org.unilab.uniplan.lector.dto.LectorRequestDto; | ||
| import org.unilab.uniplan.lector.dto.LectorResponseDto; | ||
|
|
||
| @Mapper | ||
| public interface LectorMapper { | ||
|
|
||
| @Mapping(source = "faculty.id", target = "facultyId") | ||
| LectorDto toDto(Lector lector); | ||
|
|
||
| @Mapping(source = "facultyId", target = "faculty.id") | ||
| Lector toEntity(LectorDto lectorDto); | ||
| @Mapping(target = "id", ignore = true) | ||
| Lector toEntity(LectorRequestDto requestDto); | ||
|
|
||
| @Mapping(source = "facultyId", target = "faculty.id") | ||
| void updateEntity(LectorDto dto, @MappingTarget Lector entity); | ||
| @Mapping(source = "faculty.id", target = "facultyId") | ||
| LectorDto toDto(Lector lector); | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| LectorDto toInternalDto(final LectorRequestDto lectorRequestDto); | ||
| @Mapping(source = "facultyId", target = "faculty") | ||
| void updateEntity(LectorRequestDto requestDto, @MappingTarget Lector lector); | ||
|
|
||
| LectorResponseDto toResponseDto(final LectorDto lectorDto); | ||
| @Mapping(source = "faculty.id", target = "facultyId") | ||
| LectorResponseDto toResponseDto(Lector lector); | ||
|
|
||
| List<LectorDto> toDtos(List<Lector> lectors); | ||
| List<LectorResponseDto> toResponseDtoList(List<Lector> lectors); | ||
|
|
||
| List<LectorResponseDto> toResponseDtoList(List<LectorDto> lectorDtos); | ||
| @Mapping(target = "id", source = "facultyId") | ||
| Faculty toFaculty(UUID facultyId); | ||
|
|
||
| @Mapping(target = "email", source = "lectorDto.email") | ||
| @Mapping(target = "firstName", source = "lectorDto.firstName") | ||
| @Mapping(target = "lastName", source = "lectorDto.lastName") | ||
| @Mapping(target = "id", ignore = true) | ||
| void updateEntityFromDto(final LectorDto lectorDto, @MappingTarget final Lector lector); | ||
| } |
66 changes: 16 additions & 50 deletions
66
src/main/java/org/unilab/uniplan/lector/LectorService.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 |
|---|---|---|
| @@ -1,71 +1,37 @@ | ||
| package org.unilab.uniplan.lector; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.LECTOR_NOT_FOUND; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.unilab.uniplan.exception.ResourceNotFoundException; | ||
| import org.unilab.uniplan.faculty.FacultyService; | ||
| import org.unilab.uniplan.lector.dto.LectorDto; | ||
| import org.unilab.uniplan.common.model.BaseService; | ||
|
|
||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class LectorService { | ||
| public class LectorService implements BaseService<Lector> { | ||
|
|
||
| private final LectorRepository lectorRepository; | ||
|
|
||
| private final LectorMapper lectorMapper; | ||
|
|
||
| private final FacultyService facultyService; | ||
|
|
||
| @Transactional | ||
| public LectorDto createLector(LectorDto lectorDto) { | ||
| final Lector lector = lectorMapper.toEntity(lectorDto); | ||
|
|
||
| return saveEntityAndConvertToDto(lector); | ||
| } | ||
|
|
||
| public List<LectorDto> getAllLectors() { | ||
| final List<Lector> lectors = lectorRepository.findAll(); | ||
| return lectorMapper.toDtos(lectors); | ||
| } | ||
|
|
||
| public LectorDto getLectorById(UUID id) { | ||
| return lectorRepository.findById(id) | ||
| .map(lectorMapper::toDto) | ||
| .orElseThrow(() -> new ResourceNotFoundException(LECTOR_NOT_FOUND.getMessage( | ||
| String.valueOf(id)))); | ||
| } | ||
|
|
||
| @Transactional | ||
| public LectorDto updateLector(UUID id, LectorDto lectorDto) { | ||
| return lectorRepository.findById(id) | ||
| .map(existingLector -> updateEntityAndConvertToDto( | ||
| lectorDto, | ||
| existingLector)).orElseThrow(() -> new ResourceNotFoundException( | ||
| LECTOR_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| @Override | ||
| public void save(final Lector entity) { | ||
| lectorRepository.save(entity); | ||
| } | ||
|
|
||
| public void deleteLector(UUID id) { | ||
| final Lector lector =lectorRepository.findById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| LECTOR_NOT_FOUND.getMessage(String.valueOf(id)) | ||
| )); | ||
| lectorRepository.delete(lector); | ||
| @Override | ||
| public List<Lector> getAll() { | ||
| return lectorRepository.findAll(); | ||
| } | ||
|
|
||
| private LectorDto updateEntityAndConvertToDto(final LectorDto dto, | ||
| final Lector entity) { | ||
| lectorMapper.updateEntityFromDto(dto, entity); | ||
| return saveEntityAndConvertToDto(entity); | ||
| @Override | ||
| public Optional<Lector> getById(final UUID id) { | ||
| return lectorRepository.findById(id); | ||
| } | ||
|
|
||
| private LectorDto saveEntityAndConvertToDto(final Lector entity) { | ||
| final Lector savedEntity = lectorRepository.save(entity); | ||
| return lectorMapper.toDto(savedEntity); | ||
| @Override | ||
| public void delete(final Lector entity) { | ||
| lectorRepository.delete(entity); | ||
| } | ||
| } | ||
|
|
24 changes: 24 additions & 0 deletions
24
src/main/java/org/unilab/uniplan/lector/LectorValidator.java
|
DrDeathDrop marked this conversation as resolved.
|
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,24 @@ | ||
| package org.unilab.uniplan.lector; | ||
|
|
||
| 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.lector.dto.LectorRequestDto; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class LectorValidator { | ||
|
|
||
| private final FacultyRepository facultyRepository; | ||
|
|
||
| public void validate(final LectorRequestDto request) { | ||
| final UUID id = request.facultyId(); | ||
| if (!facultyRepository.existsById(id)) { | ||
| throw new ResourceNotFoundException(FACULTY_NOT_FOUND.getMessage(id.toString())); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/unilab/uniplan/lector/LectorWebFacade.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,67 @@ | ||
| package org.unilab.uniplan.lector; | ||
|
|
||
|
|
||
| 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.lector.dto.LectorRequestDto; | ||
| import org.unilab.uniplan.lector.dto.LectorResponseDto; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.LECTOR_NOT_FOUND; | ||
|
|
||
| @Component | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class LectorWebFacade { | ||
|
|
||
| private final LectorMapper lectorMapper; | ||
| private final LectorService lectorService; | ||
| private final LectorValidator lectorValidator; | ||
|
|
||
| @Transactional | ||
| public void createLector(final LectorRequestDto request) { | ||
| final Lector lector = lectorMapper.toEntity(request); | ||
| lectorValidator.validate(request); | ||
| lectorService.save(lector); | ||
| log.info("Lector with id {} has been created", lector.getId()); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<LectorResponseDto> getAllLectors() { | ||
| return lectorMapper.toResponseDtoList(lectorService.getAll()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public LectorResponseDto getLectorById(final UUID id) { | ||
| final Lector lector = getLectorOrThrow(id); | ||
| return lectorMapper.toResponseDto(lector); | ||
| } | ||
|
|
||
| private Lector getLectorOrThrow(final UUID id){ | ||
| return lectorService.getById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| LECTOR_NOT_FOUND.getMessage(String.valueOf(id)) | ||
| )); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateLector(final UUID id, | ||
| final LectorRequestDto request) { | ||
| final Lector lector = getLectorOrThrow(id); | ||
| lectorMapper.updateEntity(request, lector); | ||
| lectorValidator.validate(request); | ||
|
Comment on lines
+55
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong order, the validation should be first than the updateEntity() |
||
| lectorService.save(lector); | ||
| log.info("Lector with id {} has been updated", lector.getId()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteLectorById(final UUID id) { | ||
| final Lector lector = getLectorOrThrow(id); | ||
| lectorService.delete(lector); | ||
| log.info("Lector with id {} has been deleted", lector.getId()); | ||
| } | ||
| } | ||
91 changes: 91 additions & 0 deletions
91
src/test/java/org/unilab/uniplan/lector/LectorMapperTest.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,91 @@ | ||
| package org.unilab.uniplan.lector; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.unilab.uniplan.faculty.Faculty; | ||
| import org.unilab.uniplan.lector.dto.LectorRequestDto; | ||
| import org.unilab.uniplan.lector.dto.LectorResponseDto; | ||
|
|
||
| class LectorMapperTest { | ||
|
|
||
| private final LectorMapper mapper = new LectorMapperImpl(); | ||
|
|
||
| private UUID facultyId; | ||
| private UUID newFacultyId; | ||
|
|
||
| private Lector lector; | ||
| private LectorRequestDto request; | ||
| private LectorRequestDto updateRequest; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| facultyId = UUID.randomUUID(); | ||
| newFacultyId = UUID.randomUUID(); | ||
|
|
||
| final Faculty faculty = new Faculty(); | ||
| faculty.setId(facultyId); | ||
|
|
||
| lector = new Lector(); | ||
| lector.setId(UUID.randomUUID()); | ||
| lector.setFaculty(faculty); | ||
| lector.setEmail("i.ivanov@gmail.com"); | ||
| lector.setFirstName("Ivan"); | ||
| lector.setLastName("Ivanov"); | ||
|
|
||
| request = new LectorRequestDto(facultyId, "i.ivanov@gmail.com", "Ivan", "Ivanov"); | ||
| updateRequest = new LectorRequestDto(newFacultyId, "p.petrov@gmail.com", "Petar", "Petrov"); | ||
| } | ||
|
|
||
| @Test | ||
| void toEntity_shouldMapAllFieldsAndIgnoreId_whenRequestDtoIsValid() { | ||
| final Lector result = mapper.toEntity(request); | ||
|
|
||
| assertThat(result.getFaculty().getId()).isEqualTo(facultyId); | ||
| assertThat(result.getEmail()).isEqualTo("i.ivanov@gmail.com"); | ||
| assertThat(result.getFirstName()).isEqualTo("Ivan"); | ||
| assertThat(result.getLastName()).isEqualTo("Ivanov"); | ||
| assertThat(result.getId()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void toResponseDto_shouldMapAllFields_whenLectorIsValid() { | ||
| final LectorResponseDto result = mapper.toResponseDto(lector); | ||
|
|
||
| assertThat(result.id()).isEqualTo(lector.getId()); | ||
| assertThat(result.facultyId()).isEqualTo(facultyId); | ||
| assertThat(result.email()).isEqualTo("i.ivanov@gmail.com"); | ||
| assertThat(result.firstName()).isEqualTo("Ivan"); | ||
| assertThat(result.lastName()).isEqualTo("Ivanov"); | ||
| } | ||
|
|
||
| @Test | ||
| void updateEntity_shouldReplaceFacultyReference_whenFacultyIdChanges() { | ||
| final Faculty originalFaculty = lector.getFaculty(); | ||
|
|
||
| mapper.updateEntity(updateRequest, lector); | ||
|
|
||
| assertThat(lector.getFaculty()).isNotSameAs(originalFaculty); | ||
| assertThat(lector.getFaculty().getId()).isEqualTo(newFacultyId); | ||
| } | ||
|
|
||
| @Test | ||
| void updateEntity_shouldUpdatePersonAndEmailFields_whenRequestDtoIsValid() { | ||
| mapper.updateEntity(updateRequest, lector); | ||
|
|
||
| assertThat(lector.getEmail()).isEqualTo("p.petrov@gmail.com"); | ||
| assertThat(lector.getFirstName()).isEqualTo("Petar"); | ||
| assertThat(lector.getLastName()).isEqualTo("Petrov"); | ||
| } | ||
|
|
||
| @Test | ||
| void updateEntity_shouldNotChangeId_whenUpdating() { | ||
| final UUID id = lector.getId(); | ||
|
|
||
| mapper.updateEntity(updateRequest, lector); | ||
|
|
||
| assertThat(lector.getId()).isEqualTo(id); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NotNull is not needed for @PathVariable