diff --git a/src/main/java/com/swp/PodBookingSystem/controller/RoomController.java b/src/main/java/com/swp/PodBookingSystem/controller/RoomController.java index a2eb4978..ce1ac17a 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/RoomController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/RoomController.java @@ -41,10 +41,11 @@ ApiResponse createRoom(@RequestBody RoomCreationRequest request) { @GetMapping PaginationResponse> getRooms(@RequestParam(required = false) String searchParams, + @RequestParam(required = false,defaultValue = "0") int buildingId, @RequestParam(defaultValue = "1", name = "page") int page, @RequestParam(defaultValue = "10", name = "take") int take) { RoomPaginationDTO dto = new RoomPaginationDTO(page, take); - Page roomPage = roomService.getRooms(searchParams, dto.page, dto.take); + Page roomPage = roomService.getRooms(buildingId,searchParams, dto.page, dto.take); return PaginationResponse.>builder() .data(roomPage.getContent()) diff --git a/src/main/java/com/swp/PodBookingSystem/repository/RoomRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/RoomRepository.java index 7460939b..2e06c8ff 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/RoomRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/RoomRepository.java @@ -98,4 +98,13 @@ List getSlotsByRoomAndDate(@Param("roomId")Integer roomId, @Query("SELECT r FROM Room r WHERE r.name LIKE %:searchParams% OR r.roomType.name LIKE %:searchParams% " + "ORDER BY r.createdAt DESC") Page findFilteredManagementRoom(@Param("searchParams") String searchParams, Pageable pageable); + + @Query ("SELECT r FROM Room r " + + "JOIN r.roomType rt " + + "JOIN rt.building b " + + "WHERE b.id = :buildingId " + + "AND (r.name LIKE %:searchParams% OR r.roomType.name LIKE %:searchParams%) " + + "ORDER BY r.createdAt DESC") + Page findFilteredManagementRoomByBuildingId(@Param("buildingId") Integer buildingId, + @Param("searchParams") String searchParams, Pageable pageable); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/RoomService.java b/src/main/java/com/swp/PodBookingSystem/service/RoomService.java index 577989c2..b7bb7cfe 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/RoomService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/RoomService.java @@ -57,9 +57,12 @@ public RoomResponse createRoom(RoomCreationRequest request) { /* [GET]: /rooms/page&take */ - public Page getRooms(String searchParams, int page, int take) { + public Page getRooms(int buildingId, String searchParams, int page, int take) { Pageable pageable = PageRequest.of(page - 1, take); - return roomRepository.findFilteredManagementRoom(searchParams, pageable); + if(buildingId == 0) { + return roomRepository.findFilteredManagementRoom(searchParams, pageable); + } + return roomRepository.findFilteredManagementRoomByBuildingId(buildingId,searchParams, pageable); } public List getRoomsByType(int typeId) {