-
Notifications
You must be signed in to change notification settings - Fork 3
Enhancement/128 add facade layer to room #133
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
PIPetkova19
wants to merge
15
commits into
main
Choose a base branch
from
enhancement/128-add-facade-layer-to-room
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
15 commits
Select commit
Hold shift + click to select a range
5a2598a
Remove internal dto, update mapper
PIPetkova19 81bd802
Update roomService by implementing baseService
PIPetkova19 8527924
Add room facade layer
PIPetkova19 5217de6
Update controller to use facade layer
PIPetkova19 cd5b74c
Add tests for room service
PIPetkova19 93be8fa
Add tests for roomMapper
PIPetkova19 5745b77
Add tests for RoomWebFacade
PIPetkova19 f8fc9aa
Include category in room response
PIPetkova19 18a22b8
Change to left join instead of inner
PIPetkova19 2109a9e
Use Criteria API instead of jpql in room repository
PIPetkova19 01bbc6c
Update mapper test
PIPetkova19 c1969bc
Update room web facade tests
PIPetkova19 68610f8
Change findRoomResponseById signature
PIPetkova19 30d4d00
Merge branch 'main' into enhancement/128-add-facade-layer-to-room
PIPetkova19 3739522
Formatted the code in the room module
PIPetkova19 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
13 changes: 13 additions & 0 deletions
13
src/main/java/org/unilab/uniplan/room/CustomRoomRepository.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,13 @@ | ||
| package org.unilab.uniplan.room; | ||
|
|
||
| import org.unilab.uniplan.room.dto.RoomResponseDto; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public interface CustomRoomRepository { | ||
|
|
||
| List<RoomResponseDto> findAllRoomResponses(); | ||
|
|
||
| Optional<RoomResponseDto> findRoomResponseById(UUID id); | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/main/java/org/unilab/uniplan/room/CustomRoomRepositoryImpl.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,76 @@ | ||
| package org.unilab.uniplan.room; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
| import jakarta.persistence.PersistenceContext; | ||
| import org.hibernate.query.common.JoinType; | ||
| import org.hibernate.query.criteria.HibernateCriteriaBuilder; | ||
| import org.hibernate.query.criteria.JpaCriteriaQuery; | ||
| import org.hibernate.query.criteria.JpaEntityJoin; | ||
| import org.hibernate.query.criteria.JpaRoot; | ||
| import org.unilab.uniplan.category.Category; | ||
| import org.unilab.uniplan.room.dto.RoomResponseDto; | ||
| import org.unilab.uniplan.roomcategory.RoomCategory; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public class CustomRoomRepositoryImpl implements CustomRoomRepository { | ||
|
|
||
| @PersistenceContext | ||
| private EntityManager entityManager; | ||
|
|
||
| @Override | ||
| public List<RoomResponseDto> findAllRoomResponses() { | ||
| HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder(); | ||
|
|
||
| JpaCriteriaQuery<RoomResponseDto> query = cb.createQuery(RoomResponseDto.class); | ||
|
|
||
| JpaRoot<Room> room = query.from(Room.class); | ||
|
|
||
| JpaEntityJoin<Room, RoomCategory> roomCategory = | ||
| room.join(RoomCategory.class, JoinType.LEFT); | ||
| roomCategory.on(cb.equal(roomCategory.get("room"), room)); | ||
|
|
||
| JpaEntityJoin<RoomCategory, Category> category = | ||
| roomCategory.join(Category.class, JoinType.LEFT); | ||
| category.on(cb.equal(category, roomCategory.get("category"))); | ||
|
|
||
| query.select(cb.construct( | ||
| RoomResponseDto.class, | ||
| room.get("id"), | ||
| room.get("faculty").get("id"), | ||
| room.get("roomNumber"), | ||
| category.get("id") | ||
| )); | ||
|
|
||
| return entityManager.createQuery(query).getResultList(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<RoomResponseDto> findRoomResponseById(UUID id) { | ||
| HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder(); | ||
|
|
||
| JpaCriteriaQuery<RoomResponseDto> query = cb.createQuery(RoomResponseDto.class); | ||
|
|
||
| JpaRoot<Room> room = query.from(Room.class); | ||
| JpaEntityJoin<Room, RoomCategory> roomCategory = | ||
| room.join(RoomCategory.class, JoinType.LEFT); | ||
| roomCategory.on(cb.equal(roomCategory.get("room"), room)); | ||
|
|
||
| JpaEntityJoin<RoomCategory, Category> category = | ||
| roomCategory.join(Category.class, JoinType.LEFT); | ||
| category.on(cb.equal(category, roomCategory.get("category"))); | ||
|
|
||
| query.select(cb.construct( | ||
| RoomResponseDto.class, | ||
| room.get("id"), | ||
| room.get("faculty").get("id"), | ||
| room.get("roomNumber"), | ||
| category.get("id") | ||
| )).where(cb.equal(room.get("id"), id)); | ||
|
|
||
| return entityManager.createQuery(query) | ||
| .getResultStream() | ||
| .findFirst(); | ||
| } | ||
| } |
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
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,65 +1,44 @@ | ||
| package org.unilab.uniplan.room; | ||
|
|
||
| import static org.unilab.uniplan.utils.ErrorConstants.ROOM_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.room.dto.RoomDto; | ||
| import org.unilab.uniplan.common.model.BaseService; | ||
| import org.unilab.uniplan.room.dto.RoomResponseDto; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RoomService { | ||
| public class RoomService implements BaseService<Room> { | ||
|
|
||
| private final RoomRepository roomRepository; | ||
| private final RoomMapper roomMapper; | ||
|
|
||
| @Transactional | ||
| public RoomDto createRoom(final RoomDto roomDto) { | ||
| final Room room = roomMapper.toEntity(roomDto); | ||
|
|
||
| return saveEntityAndConvertToDto(room); | ||
| } | ||
|
|
||
| public List<RoomDto> getAllRooms() { | ||
| return roomMapper.toDtoList(roomRepository.findAll()); | ||
| @Override | ||
| public void save(final Room room) { | ||
| roomRepository.save(room); | ||
| } | ||
|
|
||
| public RoomDto getRoomById(final UUID id) { | ||
| return roomRepository.findById(id) | ||
| .map(roomMapper::toDto) | ||
| .orElseThrow(() -> new ResourceNotFoundException(ROOM_NOT_FOUND.getMessage( | ||
| String.valueOf(id)))); | ||
| @Override | ||
| public List<Room> getAll() { | ||
| return roomRepository.findAll(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public RoomDto updateRoom(final UUID id, final RoomDto roomDto) { | ||
| return roomRepository.findById(id) | ||
| .map(existingRoom -> updateEntityAndConvertToDto( | ||
| roomDto, | ||
| existingRoom)).orElseThrow(() -> new ResourceNotFoundException( | ||
| ROOM_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| @Override | ||
| public Optional<Room> getById(final UUID id) { | ||
| return roomRepository.findById(id); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteRoom(final UUID id) { | ||
| final Room room = roomRepository.findById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException( | ||
| ROOM_NOT_FOUND.getMessage(String.valueOf(id)))); | ||
| @Override | ||
| public void delete(final Room room) { | ||
| roomRepository.delete(room); | ||
| } | ||
|
|
||
| private RoomDto updateEntityAndConvertToDto(final RoomDto dto, | ||
| final Room entity) { | ||
| roomMapper.updateEntityFromDto(dto, entity); | ||
| return saveEntityAndConvertToDto(entity); | ||
| public List<RoomResponseDto> getAllRoomResponses() { | ||
| return roomRepository.findAllRoomResponses(); | ||
| } | ||
|
|
||
| private RoomDto saveEntityAndConvertToDto(final Room entity) { | ||
| final Room savedEntity = roomRepository.save(entity); | ||
| return roomMapper.toDto(savedEntity); | ||
| public Optional<RoomResponseDto> getRoomResponseById(final UUID id) { | ||
| return roomRepository.findRoomResponseById(id); | ||
| } | ||
| } |
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,63 @@ | ||
| package org.unilab.uniplan.room; | ||
|
|
||
| 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.room.dto.RoomRequestDto; | ||
| import org.unilab.uniplan.room.dto.RoomResponseDto; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import static org.unilab.uniplan.utils.ErrorConstants.ROOM_NOT_FOUND; | ||
|
|
||
| @Component | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class RoomWebFacade { | ||
|
|
||
| private final RoomMapper roomMapper; | ||
| private final RoomService roomService; | ||
|
|
||
| private Room getRoomOrThrow(final UUID id) { | ||
| return roomService.getById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException(ROOM_NOT_FOUND.getMessage( | ||
| String.valueOf(id)))); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void createRoom(RoomRequestDto roomRequestDto) { | ||
| Room room = roomMapper.toEntity(roomRequestDto); | ||
| roomService.save(room); | ||
| log.info("created room {} with ID: {}", | ||
| room.getRoomNumber(), | ||
| room.getId()); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<RoomResponseDto> getAllRooms() { | ||
| return roomService.getAllRoomResponses(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteRoom(final UUID id) { | ||
| final Room room = getRoomOrThrow(id); | ||
| roomService.delete(room); | ||
| log.info("deleted room with id {}", id); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public RoomResponseDto getRoomById(final UUID id) { | ||
| return roomService.getRoomResponseById(id) | ||
| .orElseThrow(() -> new ResourceNotFoundException(ROOM_NOT_FOUND.getMessage( | ||
| String.valueOf(id)))); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateRoom(final UUID id, final RoomRequestDto roomRequestDto) { | ||
| final Room room = getRoomOrThrow(id); | ||
| roomMapper.updateEntityFromDto(roomRequestDto, room); | ||
| roomService.save(room); | ||
| log.info("updated room with id {}", id); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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.
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.
Why this method is called getRoomOrThrow?
The idea of exception is that they happen unexpectedly there is no need to add that behavior in the method name.
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.
I was following the same naming convention that's already used in the other files. We can open another pr to refactor the naming across all the files?