Skip to content

Commit

Permalink
Merge pull request #65 from Hoang-Nguyen-Huy/refactor/PBS-36/room-typ…
Browse files Browse the repository at this point in the history
…e-filter

[PBS-40][HoangHN] feat: 🍑 get filtered room types
  • Loading branch information
Hoang-Nguyen-Huy authored Oct 7, 2024
2 parents 5d84c8a + 0c5ea43 commit c8cc215
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -53,6 +55,25 @@ ApiResponse<Optional<RoomTypeResponse>> getRoomTypeById(@PathVariable("roomTypeI
.build();
}

@GetMapping("/filtered-room-type")
PaginationResponse<List<RoomType>> getFilteredRoomType(@RequestParam(required = false) String address,
@RequestParam(required = false) Integer capacity,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int take) {
RoomTypePaginationDTO dto = new RoomTypePaginationDTO(page, take);
Page<RoomType> roomTypePage = roomTypeService.getFilteredRoomTypes(address, capacity, startTime, endTime, dto.page, dto.take);

return PaginationResponse.<List<RoomType>>builder()
.data(roomTypePage.getContent())
.currentPage(roomTypePage.getNumber() + 1)
.totalPage(roomTypePage.getTotalPages())
.recordPerPage(roomTypePage.getNumberOfElements())
.totalRecord((int) roomTypePage.getTotalElements())
.build();
}

@PutMapping("/{roomTypeId}")
ApiResponse<RoomTypeResponse> updatedRoomType(@PathVariable("roomTypeId") int roomTypeId,
@RequestBody RoomTypeCreationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.swp.PodBookingSystem.repository;

import com.swp.PodBookingSystem.dto.request.Room.RoomAvailabilityDTO;
import com.swp.PodBookingSystem.dto.respone.ApiResponse;
import com.swp.PodBookingSystem.entity.OrderDetail;
import com.swp.PodBookingSystem.entity.Room;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package com.swp.PodBookingSystem.repository;

import com.swp.PodBookingSystem.entity.RoomType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;

public interface RoomTypeRepository extends JpaRepository<RoomType, Integer> {
@Query("SELECT rt FROM RoomType rt " +
"JOIN rt.building b " +
"LEFT JOIN Room r ON r.roomType.id = rt.id " +
"LEFT JOIN OrderDetail od ON r.id = od.room.id " +
"WHERE (:address IS NULL OR b.address LIKE %:address%) " +
"AND (:capacity IS NULL OR rt.capacity = :capacity) " +
"AND ((:startTime IS NULL AND :endTime IS NULL) " +
" OR NOT EXISTS (SELECT 1 FROM OrderDetail od2 " +
" WHERE od2.room.id = r.id " +
" AND ((od2.startTime BETWEEN :startTime AND :endTime) " +
" OR (od2.endTime BETWEEN :startTime AND :endTime)))) " +
"GROUP BY rt.id " +
"HAVING (:startTime IS NOT NULL AND :endTime IS NOT NULL AND COUNT(r.id) >= 1) " +
" OR (:startTime IS NULL AND :endTime IS NULL)")
Page<RoomType> findFilteredRoomTypes(@Param("address") String address,
@Param("capacity") Integer capacity,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -50,6 +51,14 @@ public Optional<RoomTypeResponse> getRoomTypeById(int roomTypeId) {
return roomTypeMapper.toRoomTypeResponse(roomTypeRepository.findById(roomTypeId));
}

/*
[GET]: /room-types/address&capacity&startTime&endTime&page&take
*/
public Page<RoomType> getFilteredRoomTypes(String address, Integer capacity, LocalDateTime startTime, LocalDateTime endTime, int page, int take) {
Pageable pageable = PageRequest.of(page - 1, take);
return roomTypeRepository.findFilteredRoomTypes(address, capacity, startTime, endTime, pageable);
}

/*
[PUT]: /room-types/roomTypeId
*/
Expand Down

0 comments on commit c8cc215

Please sign in to comment.