From dcfa14c020c341d84b69fe0944e7e9f99ac92219 Mon Sep 17 00:00:00 2001 From: HuyDiCode Date: Thu, 17 Oct 2024 20:56:39 +0700 Subject: [PATCH 1/9] [PBS-84][HuyNBQ] feat: implement get all amenity orders api --- .../OrderDetailAmenityController.java | 38 +++++++++++++++--- .../OrderDetailAmenityCreationRequest.java | 4 ++ .../OrderDetailAmenityPaginationDTO.java | 9 +++++ .../OrderDetailAmenityResponse.java | 16 ++++++++ .../OrderDetailAmenityRepository.java | 4 +- .../service/OrderDetailAmenityService.java | 40 ++++++++++++++++--- 6 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityCreationRequest.java create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index 507471e1..360d3bcc 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -1,11 +1,39 @@ package com.swp.PodBookingSystem.controller; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityPaginationDTO; +import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; +import com.swp.PodBookingSystem.dto.respone.PaginationResponse; + import com.swp.PodBookingSystem.service.OrderDetailAmenityService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; -@Service +@RestController +@RequestMapping("/amenity-orders") +@RequiredArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class OrderDetailAmenityController { - @Autowired - private OrderDetailAmenityService orderDetailAmenityService; + OrderDetailAmenityService orderDetailAmenityService; + + @GetMapping + PaginationResponse> getOrderDetailAmenities(@RequestParam(defaultValue = "1", name = "page") int page, + @RequestParam(defaultValue = "10", name = "take") int take) { + OrderDetailAmenityPaginationDTO dto = new OrderDetailAmenityPaginationDTO(page, take); + Page orderDetailAmenityPage = orderDetailAmenityService.getOrderDetailAmenities(dto.page, dto.take); + return PaginationResponse.>builder() + .data(orderDetailAmenityPage.getContent()) + .currentPage(orderDetailAmenityPage.getNumber() + 1) + .totalPage(orderDetailAmenityPage.getTotalPages()) + .recordPerPage(orderDetailAmenityPage.getNumberOfElements()) + .totalRecord((int) orderDetailAmenityPage.getTotalElements()) + .build(); + } } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityCreationRequest.java b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityCreationRequest.java new file mode 100644 index 00000000..3328c1d3 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityCreationRequest.java @@ -0,0 +1,4 @@ +package com.swp.PodBookingSystem.dto.request.OrderDetailAmenity; + +public class OrderDetailAmenityCreationRequest { +} diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java new file mode 100644 index 00000000..748a9119 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java @@ -0,0 +1,9 @@ +package com.swp.PodBookingSystem.dto.request.OrderDetailAmenity; + +import com.swp.PodBookingSystem.dto.request.PaginationDTO; + +public class OrderDetailAmenityPaginationDTO extends PaginationDTO { + public OrderDetailAmenityPaginationDTO(int page, int take) { + super(page, take); + } +} \ No newline at end of file diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java new file mode 100644 index 00000000..d20d2bb2 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java @@ -0,0 +1,16 @@ +package com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity; + +import com.swp.PodBookingSystem.entity.Amenity; +import lombok.*; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OrderDetailAmenityResponse { + String id; + int quantity; + double price; + Amenity amenity; +} diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java index e6881824..d39b2dab 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java @@ -9,7 +9,6 @@ import java.util.List; import org.springframework.stereotype.Repository; -@Repository public interface OrderDetailAmenityRepository extends JpaRepository { List findByOrderDetailId(String orderDetailId); @@ -17,4 +16,7 @@ public interface OrderDetailAmenityRepository extends JpaRepository getOrderDetailAmenitiesByOrderDetailId(String orderDetailId) { @@ -30,13 +38,13 @@ public List getOrderDetailAmenitiesByOrderDetailId(St } //CREATE: - public void createOrderDetailAmenity(OrderDetailAmenity orderDetailAmenity){ + public void createOrderDetailAmenity(OrderDetailAmenity orderDetailAmenity) { Optional amenity = amenityRepository.findById(orderDetailAmenity.getAmenity().getId()); if (amenity.isEmpty()) { throw new RuntimeException("Amenity not found"); } Amenity updatedAmenity = amenity.get(); - if(updatedAmenity.getQuantity() < orderDetailAmenity.getQuantity()){ + if (updatedAmenity.getQuantity() < orderDetailAmenity.getQuantity()) { throw new RuntimeException("Not enough quantity"); } updatedAmenity.setQuantity(updatedAmenity.getQuantity() - orderDetailAmenity.getQuantity()); @@ -60,8 +68,8 @@ public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { //UTILS: public void restoreAmenityQuantity(String orderDetailId) { List orderDetailAmenities = orderDetailAmenityRepository.findByOrderDetailId(orderDetailId); - for(OrderDetailAmenity orderDetailAmenity : orderDetailAmenities){ - if(orderDetailAmenity.getAmenity().getType() == AmenityType.Office) { + for (OrderDetailAmenity orderDetailAmenity : orderDetailAmenities) { + if (orderDetailAmenity.getAmenity().getType() == AmenityType.Office) { Optional amenity = amenityRepository.findById(orderDetailAmenity.getAmenity().getId()); if (amenity.isEmpty()) { throw new RuntimeException("Amenity not found"); @@ -72,4 +80,24 @@ public void restoreAmenityQuantity(String orderDetailId) { } } } + + public Page getOrderDetailAmenities(int page, int take) { + Pageable pageable = PageRequest.of(page - 1, take); + Page orderDetailAmenityPage = orderDetailAmenityRepository.findAll(pageable); + + List responseList = orderDetailAmenityPage.getContent().stream() + .map(this::mapToResponse) + .collect(Collectors.toList()); + + return new PageImpl<>(responseList, pageable, orderDetailAmenityPage.getTotalElements()); + } + + private OrderDetailAmenityResponse mapToResponse(OrderDetailAmenity orderDetailAmenity) { + return OrderDetailAmenityResponse.builder() + .id(orderDetailAmenity.getId()) + .quantity(orderDetailAmenity.getQuantity()) + .price(orderDetailAmenity.getPrice()) + .amenity(orderDetailAmenity.getAmenity()) + .build(); + } } \ No newline at end of file From 2bbb9cc974b05c60e08c40a4d62fd17e7480d0ca Mon Sep 17 00:00:00 2001 From: HuyDiCode Date: Fri, 18 Oct 2024 14:31:51 +0700 Subject: [PATCH 2/9] [PBS-84][HuyNBQ] feat: api pagination get all amenity orders --- .../OrderDetailAmenityController.java | 21 +++++++++++++++---- .../OrderDetailAmenityResponse.java | 7 +++++++ .../entity/OrderDetailAmenity.java | 19 +++++++++++++++++ .../service/OrderDetailAmenityService.java | 14 +++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index 360d3bcc..f8c74c78 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -4,15 +4,13 @@ import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; import com.swp.PodBookingSystem.dto.respone.PaginationResponse; +import com.swp.PodBookingSystem.entity.OrderDetailAmenity; import com.swp.PodBookingSystem.service.OrderDetailAmenityService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -36,4 +34,19 @@ PaginationResponse> getOrderDetailAmenities(@Re .totalRecord((int) orderDetailAmenityPage.getTotalElements()) .build(); } + @PostMapping + public void createOrderDetailAmenity(@RequestBody OrderDetailAmenity orderDetailAmenity) { + orderDetailAmenityService.createOrderDetailAmenity(orderDetailAmenity); + } + + @PutMapping("/{id}") + public void updateOrderDetailAmenity(@PathVariable String id, @RequestBody OrderDetailAmenity orderDetailAmenity) { + orderDetailAmenityService.updateOrderDetailAmenity(id, orderDetailAmenity); + } + + @DeleteMapping("/{orderDetailId}") + public double deleteOrderDetailAmenity(@PathVariable String orderDetailId) { + return orderDetailAmenityService.deleteOrderDetailAmenityByOrderDetailId(orderDetailId); + } + } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java index d20d2bb2..3444238e 100644 --- a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponse.java @@ -3,6 +3,8 @@ import com.swp.PodBookingSystem.entity.Amenity; import lombok.*; +import java.time.LocalDateTime; + @Getter @Setter @Builder @@ -13,4 +15,9 @@ public class OrderDetailAmenityResponse { int quantity; double price; Amenity amenity; + String orderId; + LocalDateTime createdAt; + LocalDateTime updatedAt; + + } diff --git a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java index 7f569861..cf11e542 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java @@ -5,6 +5,8 @@ import lombok.*; import lombok.experimental.FieldDefaults; +import java.time.LocalDateTime; + @Getter @Setter @Builder @@ -31,4 +33,21 @@ public class OrderDetailAmenity { @JsonIgnore @JoinColumn(name = "amenityId", nullable = false) Amenity amenity; + + @Column(name = "createdAt") + LocalDateTime createdAt; + + @Column(name = "updatedAt") + LocalDateTime updatedAt; + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + this.updatedAt = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java index f511a0b5..15ab7dd5 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java @@ -98,6 +98,20 @@ private OrderDetailAmenityResponse mapToResponse(OrderDetailAmenity orderDetailA .quantity(orderDetailAmenity.getQuantity()) .price(orderDetailAmenity.getPrice()) .amenity(orderDetailAmenity.getAmenity()) + .orderId(orderDetailAmenity.getOrderDetail().getOrder().getId()) + .createdAt(orderDetailAmenity.getCreatedAt()) + .updatedAt(orderDetailAmenity.getUpdatedAt()) .build(); } + + public void updateOrderDetailAmenity(String id, OrderDetailAmenity orderDetailAmenity) { + Optional existingOrderDetailAmenity = orderDetailAmenityRepository.findById(id); + if (existingOrderDetailAmenity.isEmpty()) { + throw new RuntimeException("OrderDetailAmenity not found"); + } + OrderDetailAmenity updateOrderDetailAmenity = existingOrderDetailAmenity.get(); + updateOrderDetailAmenity.setQuantity(orderDetailAmenity.getQuantity()); + updateOrderDetailAmenity.setPrice(orderDetailAmenity.getPrice()); + orderDetailAmenityRepository.save(updateOrderDetailAmenity); + } } \ No newline at end of file From b231a87d418a60b1cfd192bd69bfd567b0af90cb Mon Sep 17 00:00:00 2001 From: toki-ai Date: Tue, 22 Oct 2024 20:26:12 +0700 Subject: [PATCH 3/9] [PBS-84][Anh Dao] merge develop --- src/main/java/com/swp/PodBookingSystem/entity/Amenity.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/entity/Amenity.java b/src/main/java/com/swp/PodBookingSystem/entity/Amenity.java index 0d08283b..9f011b1b 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/Amenity.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/Amenity.java @@ -39,8 +39,9 @@ public class Amenity { int isDeleted; - @Column(name = "buildingNumber") - int buildingNumber; + @ManyToOne + @JoinColumn(name = "building_id") + Building building; @PrePersist protected void onCreate() { From 6dce7e6a5feee7a7b7ce18534eded0c37fa7ddad Mon Sep 17 00:00:00 2001 From: toki-ai Date: Tue, 22 Oct 2024 21:50:17 +0700 Subject: [PATCH 4/9] [PBS-84][Anh Dao] feat: create and delete order detail amenity --- .../OrderDetailAmenityController.java | 38 +++++++++++++++--- .../OrderDetailAmenityRequest.java | 15 +++++++ .../OrderDetailAmenityRepository.java | 4 ++ .../service/OrderDetailAmenityService.java | 39 ++++++++++++++++++- .../service/OrderDetailService.java | 4 +- 5 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityRequest.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index 507471e1..8cabc802 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -1,11 +1,39 @@ package com.swp.PodBookingSystem.controller; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; +import com.swp.PodBookingSystem.dto.respone.ApiResponse; import com.swp.PodBookingSystem.service.OrderDetailAmenityService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.springframework.web.bind.annotation.*; -@Service +@RestController +@RequestMapping("/order-detail-amenity") +@RequiredArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class OrderDetailAmenityController { - @Autowired - private OrderDetailAmenityService orderDetailAmenityService; + OrderDetailAmenityService orderDetailAmenityService; + + @PostMapping + public ApiResponse createOrderDetailAmenity (@RequestBody OrderDetailAmenityRequest orderDetailAmenityRequest) { + try{ + orderDetailAmenityService.createOrderDetailAmenity(orderDetailAmenityRequest); + return ApiResponse.builder() + .message("Create order detail amenity successfully") + .build(); + } catch (Exception e) { + return ApiResponse.builder() + .message("Failed to create order detail amenity: " + e.getMessage()) + .build(); + } + } + + @DeleteMapping("/{id}") + public ApiResponse deleteOrderDetailAmenity (@PathVariable String id) { + return ApiResponse.builder() + .data(orderDetailAmenityService.deleteOrderDetailAmenityById(id)) + .message("Delete order successfully") + .build(); + } } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityRequest.java b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityRequest.java new file mode 100644 index 00000000..0729261e --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityRequest.java @@ -0,0 +1,15 @@ +package com.swp.PodBookingSystem.dto.request.OrderDetailAmenity; + +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +public class OrderDetailAmenityRequest { + private String orderDetailId; + private int quantity; + private int amenityId; +} diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java index e6881824..d574f2c7 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java @@ -17,4 +17,8 @@ public interface OrderDetailAmenityRepository extends JpaRepository getOrderDetailAmenitiesByOrderDetailId(String orderDetailId) { @@ -30,13 +35,31 @@ public List getOrderDetailAmenitiesByOrderDetailId(St } //CREATE: - public void createOrderDetailAmenity(OrderDetailAmenity orderDetailAmenity){ + public void createOrderDetailAmenity(OrderDetailAmenityRequest request) { + Optional orderDetail = orderDetailRepository.findById(request.getOrderDetailId()); + Optional amenity = amenityRepository.findById(request.getAmenityId()); + if(orderDetail.isEmpty() || amenity.isEmpty()) { + throw new RuntimeException("Order detail or amenity not found"); + } + OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity(); + //orderDetailAmenity.setCreatedAt(LocalDateTime.now()); + //orderDetailAmenity.setUpdatedAt(LocalDateTime.now()); + orderDetailAmenity.setId(UUID.randomUUID().toString()); + orderDetailAmenity.setQuantity(request.getQuantity()); + orderDetailAmenity.setPrice(amenity.get().getPrice()); + orderDetailAmenity.setOrderDetail(orderDetail.get()); + orderDetailAmenity.setAmenity(amenity.get()); + + updateAmenityQuantityAfterCreateODA(orderDetailAmenity); + } + + public void updateAmenityQuantityAfterCreateODA(OrderDetailAmenity orderDetailAmenity) { Optional amenity = amenityRepository.findById(orderDetailAmenity.getAmenity().getId()); if (amenity.isEmpty()) { throw new RuntimeException("Amenity not found"); } Amenity updatedAmenity = amenity.get(); - if(updatedAmenity.getQuantity() < orderDetailAmenity.getQuantity()){ + if (updatedAmenity.getQuantity() < orderDetailAmenity.getQuantity()) { throw new RuntimeException("Not enough quantity"); } updatedAmenity.setQuantity(updatedAmenity.getQuantity() - orderDetailAmenity.getQuantity()); @@ -57,6 +80,18 @@ public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { return total; } + @Transactional + public String deleteOrderDetailAmenityById(String orderDetailAmenityId) { + Optional oda = orderDetailAmenityRepository.findById(orderDetailAmenityId); + if (oda.isEmpty()) { + return("Order detail amenity not found"); + }else { + String odaId = oda.get().getId(); + orderDetailAmenityRepository.deleteById(orderDetailAmenityId); + return("Order detail amenity with id: " + odaId + " has been deleted"); + } + } + //UTILS: public void restoreAmenityQuantity(String orderDetailId) { List orderDetailAmenities = orderDetailAmenityRepository.findByOrderDetailId(orderDetailId); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index c8195767..c34b1345 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -175,11 +175,11 @@ private void createOrderDetailAmenities(OrderDetail orderDetail, List a OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity(); orderDetailAmenity.setId(UUID.randomUUID().toString()); orderDetailAmenity.setQuantity(amenity.getQuantity()); - orderDetailAmenity.setPrice(amenity.getPrice() * amenity.getQuantity()); + orderDetailAmenity.setPrice(amenity.getPrice()); orderDetailAmenity.setOrderDetail(orderDetail); orderDetailAmenity.setAmenity(amenity); - orderDetailAmenityService.createOrderDetailAmenity(orderDetailAmenity); + orderDetailAmenityService.updateAmenityQuantityAfterCreateODA(orderDetailAmenity); } } From 08c554c0761f8a55127194f5586b8d57316a3063 Mon Sep 17 00:00:00 2001 From: toki-ai Date: Tue, 22 Oct 2024 22:04:50 +0700 Subject: [PATCH 5/9] [PBS-84][Anh Dao] feat: delete and create order detail amenity --- .../OrderDetailAmenityController.java | 30 ++++++++-- .../service/OrderDetailAmenityService.java | 60 +++++++++++++------ 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index f93cd559..09923932 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -1,17 +1,13 @@ package com.swp.PodBookingSystem.controller; -import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; import com.swp.PodBookingSystem.dto.respone.ApiResponse; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; -import com.swp.PodBookingSystem.entity.OrderDetailAmenity; -import com.swp.PodBookingSystem.service.AccountService; import com.swp.PodBookingSystem.service.OrderDetailAmenityService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.*; @RestController @@ -28,4 +24,26 @@ public ApiResponse createOrderDetailAmenity(@Request .message("Tạo đơn hàng tiện ích thành công") .build(); } + + @PostMapping("/create") + public ApiResponse createOrderDetailAmenity (@RequestBody OrderDetailAmenityRequest orderDetailAmenityRequest) { + try{ + orderDetailAmenityService.createOrderDetailAmenity(orderDetailAmenityRequest); + return ApiResponse.builder() + .message("Create order detail amenity successfully") + .build(); + } catch (Exception e) { + return ApiResponse.builder() + .message("Failed to create order detail amenity: " + e.getMessage()) + .build(); + } + } + + @DeleteMapping("/{id}") + public ApiResponse deleteOrderDetailAmenity (@PathVariable String id) { + return ApiResponse.builder() + .data(orderDetailAmenityService.deleteOrderDetailAmenityById(id)) + .message("Delete order successfully") + .build(); + } } diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java index db5adeac..f1d03040 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java @@ -1,5 +1,6 @@ package com.swp.PodBookingSystem.service; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; @@ -15,11 +16,11 @@ import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; +import java.util.UUID; import java.util.stream.Collectors; @Service @@ -42,6 +43,35 @@ public List getOrderDetailAmenitiesByOrderDetailId(St .build()).collect(Collectors.toList()); } + //CREATE: + public void createOrderDetailAmenity(OrderDetailAmenityRequest request) { + Optional orderDetail = orderDetailRepository.findById(request.getOrderDetailId()); + Optional amenity = amenityRepository.findById(request.getAmenityId()); + if(orderDetail.isEmpty() || amenity.isEmpty()) { + throw new RuntimeException("Order detail or amenity not found"); + } + OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity(); + //orderDetailAmenity.setCreatedAt(LocalDateTime.now()); + //orderDetailAmenity.setUpdatedAt(LocalDateTime.now()); + orderDetailAmenity.setId(UUID.randomUUID().toString()); + orderDetailAmenity.setQuantity(request.getQuantity()); + orderDetailAmenity.setPrice(amenity.get().getPrice()); + orderDetailAmenity.setOrderDetail(orderDetail.get()); + orderDetailAmenity.setAmenity(amenity.get()); + + updateAmenityQuantityAfterCreateODA(orderDetailAmenity); + } + + public void updateAmenityQuantityAfterCreateODA(OrderDetailAmenity orderDetailAmenity) { + Amenity amenity = orderDetailAmenity.getAmenity(); + if(amenity.getQuantity() < orderDetailAmenity.getQuantity()) { + throw new RuntimeException("Not enough quantity"); + } + amenity.setQuantity(amenity.getQuantity() - orderDetailAmenity.getQuantity()); + amenityRepository.save(amenity); + orderDetailAmenityRepository.save(orderDetailAmenity); + } + /* [POST]: /order-detail-amenity */ @@ -79,22 +109,6 @@ public OrderDetailAmenityResponse createOrderDetailAmenityApi(OrderDetailAmenity .build(); } - //CREATE in orderDetail - public void createOrderDetailAmenity(OrderDetailAmenity orderDetailAmenity){ - Optional amenity = amenityRepository.findById(orderDetailAmenity.getAmenity().getId()); - if (amenity.isEmpty()) { - throw new RuntimeException("Amenity not found"); - } - Amenity updatedAmenity = amenity.get(); - if(updatedAmenity.getQuantity() < orderDetailAmenity.getQuantity()){ - throw new RuntimeException("Not enough quantity"); - } - updatedAmenity.setQuantity(updatedAmenity.getQuantity() - orderDetailAmenity.getQuantity()); - amenityRepository.save(updatedAmenity); - - orderDetailAmenityRepository.save(orderDetailAmenity); - } - //DELETE: @Transactional public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { @@ -107,6 +121,18 @@ public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { return total; } + @Transactional + public String deleteOrderDetailAmenityById(String orderDetailAmenityId) { + Optional oda = orderDetailAmenityRepository.findById(orderDetailAmenityId); + if (oda.isEmpty()) { + return("Order detail amenity not found"); + }else { + String odaId = oda.get().getId(); + orderDetailAmenityRepository.deleteById(orderDetailAmenityId); + return("Order detail amenity with id: " + odaId + " has been deleted"); + } + } + //UTILS: public void restoreAmenityQuantity(String orderDetailId) { List orderDetailAmenities = orderDetailAmenityRepository.findByOrderDetailId(orderDetailId); From 67e143c2d874628e131447506db80f5b933337eb Mon Sep 17 00:00:00 2001 From: toki-ai Date: Tue, 22 Oct 2024 23:32:06 +0700 Subject: [PATCH 6/9] [PBS-84][Anh Dao] feat: get page order detail and amenity, remove delete order detail amenity --- .../OrderDetailAmenityController.java | 28 +++++++++--- .../OrderDetailAmenityListResponse.java | 32 ++++++++++++++ .../OrderDetailAmenityRepository.java | 4 -- .../repository/OrderDetailRepository.java | 4 ++ .../service/OrderDetailAmenityService.java | 22 ++++------ .../service/OrderDetailService.java | 44 +++++++++++++++++++ 6 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index 09923932..e7492e06 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -3,12 +3,16 @@ import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; import com.swp.PodBookingSystem.dto.respone.ApiResponse; import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; +import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailAmenityListResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; +import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.service.OrderDetailAmenityService; +import com.swp.PodBookingSystem.service.OrderDetailService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import org.springframework.web.bind.annotation.*; +import java.util.List; @RestController @RequestMapping("/order-detail-amenity") @@ -16,6 +20,22 @@ @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class OrderDetailAmenityController { OrderDetailAmenityService orderDetailAmenityService; + OrderDetailService orderDetailService; + + @GetMapping("/page") + public ApiResponse>> getOrderDetailAndAmenity( + //@RequestHeader("Authorization") String token, + //@RequestParam String startDate, + //@RequestParam String endDate, + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size) { + //String accountId = accountService.extractAccountIdFromToken(token); + // Account user = accountService.getAccountById(accountId); + return ApiResponse.>>builder() + .data(orderDetailService.getPagedOrderDetails(page, size)) + .message("get paging order detail successfully") + .build(); + } @PostMapping public ApiResponse createOrderDetailAmenity(@RequestBody OrderDetailAmenityCreationRequest request) { @@ -39,11 +59,5 @@ public ApiResponse createOrderDetailAmenity (@RequestBody OrderDetailAme } } - @DeleteMapping("/{id}") - public ApiResponse deleteOrderDetailAmenity (@PathVariable String id) { - return ApiResponse.builder() - .data(orderDetailAmenityService.deleteOrderDetailAmenityById(id)) - .message("Delete order successfully") - .build(); - } + //Update } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java new file mode 100644 index 00000000..62345cb7 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java @@ -0,0 +1,32 @@ +package com.swp.PodBookingSystem.dto.respone.OrderDetail; + +import com.swp.PodBookingSystem.entity.OrderDetail; +import com.swp.PodBookingSystem.entity.OrderDetailAmenity; +import com.swp.PodBookingSystem.entity.Room; +import com.swp.PodBookingSystem.enums.OrderStatus; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OrderDetailAmenityListResponse { + String id; + String customerId; + int buildingId; + int roomId; + String roomName; + String orderId; + private List amenities; + int servicePackageId; + String orderHandledId; + double priceRoom; + OrderStatus status; + LocalDateTime startTime; + LocalDateTime endTime; + LocalDateTime createdAt; +} diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java index d574f2c7..e6881824 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailAmenityRepository.java @@ -17,8 +17,4 @@ public interface OrderDetailAmenityRepository extends JpaRepository findByEndTime(LocalDateTime endTime); + Page findAll(Pageable pageable); + @Query("SELECT o FROM OrderDetail o WHERE FUNCTION('DATE', o.startTime) BETWEEN FUNCTION('DATE', :startOfDay) AND FUNCTION('DATE', :endOfDay)") List findAllOrderDetailsByDay(@Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java index f1d03040..5116cbd4 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java @@ -18,6 +18,8 @@ import lombok.experimental.FieldDefaults; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -43,6 +45,10 @@ public List getOrderDetailAmenitiesByOrderDetailId(St .build()).collect(Collectors.toList()); } + public List getOrderDetailAmenitiesAllInfoByOrderDetailId(String orderDetailId) { + return orderDetailAmenityRepository.findByOrderDetailId(orderDetailId); + } + //CREATE: public void createOrderDetailAmenity(OrderDetailAmenityRequest request) { Optional orderDetail = orderDetailRepository.findById(request.getOrderDetailId()); @@ -51,8 +57,8 @@ public void createOrderDetailAmenity(OrderDetailAmenityRequest request) { throw new RuntimeException("Order detail or amenity not found"); } OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity(); - //orderDetailAmenity.setCreatedAt(LocalDateTime.now()); - //orderDetailAmenity.setUpdatedAt(LocalDateTime.now()); + orderDetailAmenity.setCreatedAt(LocalDateTime.now()); + orderDetailAmenity.setUpdatedAt(LocalDateTime.now()); orderDetailAmenity.setId(UUID.randomUUID().toString()); orderDetailAmenity.setQuantity(request.getQuantity()); orderDetailAmenity.setPrice(amenity.get().getPrice()); @@ -121,18 +127,6 @@ public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { return total; } - @Transactional - public String deleteOrderDetailAmenityById(String orderDetailAmenityId) { - Optional oda = orderDetailAmenityRepository.findById(orderDetailAmenityId); - if (oda.isEmpty()) { - return("Order detail amenity not found"); - }else { - String odaId = oda.get().getId(); - orderDetailAmenityRepository.deleteById(orderDetailAmenityId); - return("Order detail amenity with id: " + odaId + " has been deleted"); - } - } - //UTILS: public void restoreAmenityQuantity(String orderDetailId) { List orderDetailAmenities = orderDetailAmenityRepository.findByOrderDetailId(orderDetailId); diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index c34b1345..fc3a94f4 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -5,8 +5,10 @@ import com.swp.PodBookingSystem.dto.request.OrderDetail.OrderDetailUpdateRoomRequest; import com.swp.PodBookingSystem.dto.request.Room.RoomWithAmenitiesDTO; import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse; +import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailAmenityListResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse; +import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.entity.*; import com.swp.PodBookingSystem.enums.AccountRole; import com.swp.PodBookingSystem.enums.OrderStatus; @@ -17,6 +19,8 @@ import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -43,6 +47,7 @@ public class OrderDetailService { private final ServicePackageService servicePackageService; private final RoomService roomService; private final RoomRepository roomRepository; + private final OrderDetailAmenityRepository orderDetailAmenityRepository; //GET: public List getAllOrders() { @@ -91,6 +96,45 @@ public List getOrdersByCustomerId(String customerId) { return orderDetailResponses; } + public PaginationResponse> getPagedOrderDetails(int page, int size) { + Page orderDetailPage = orderDetailRepository.findAll(PageRequest.of(page, size)); + List orderDetailResponses = orderDetailPage.getContent().stream() + .map(orderDetail -> { + List amenities = + orderDetailAmenityService.getOrderDetailAmenitiesAllInfoByOrderDetailId(orderDetail.getId()); + OrderDetailAmenityListResponse response = OrderDetailAmenityListResponse.builder() + .id(orderDetail.getId()) + .customerId(Optional.ofNullable(orderDetail.getCustomer()) + .map(Account::getId) + .orElse(null)) + .buildingId(orderDetail.getBuilding().getId()) + .roomId(orderDetail.getRoom().getId()) + .roomName(orderDetail.getRoom().getName()) + .orderId(orderDetail.getOrder().getId()) + .amenities(amenities) + .servicePackageId(orderDetail.getServicePackage().getId()) + .orderHandledId(Optional.ofNullable(orderDetail.getOrderHandler()) + .map(Account::getId) + .orElse(null)) + .priceRoom(orderDetail.getPriceRoom()) + .status(orderDetail.getStatus()) + .startTime(orderDetail.getStartTime()) + .endTime(orderDetail.getEndTime()) + .createdAt(orderDetail.getCreatedAt()) + .build(); + return response; + }) + .collect(Collectors.toList()); + + return PaginationResponse.>builder() + .data(orderDetailResponses) + .currentPage(orderDetailPage.getNumber()) + .totalPage(orderDetailPage.getTotalPages()) + .recordPerPage(orderDetailPage.getSize()) + .totalRecord((int) orderDetailPage.getTotalElements()) + .build(); + } + //CREATE: public boolean processOrderDetails(OrderDetailCreationRequest request, Order order, Account account) { if(account.getRole() != AccountRole.Customer){ From 2f9c24e9f6baa5ef785d8fe1aecb867df81108be Mon Sep 17 00:00:00 2001 From: toki-ai Date: Tue, 22 Oct 2024 23:55:53 +0700 Subject: [PATCH 7/9] [PBS-84][Anh Dao] fix: get page order detail and amenity by role and date --- .../OrderDetailAmenityController.java | 21 +++++++++++++------ .../OrderDetailAmenityPaginationDTO.java | 9 -------- .../repository/OrderDetailRepository.java | 12 +++++++++++ .../service/OrderDetailService.java | 12 +++++++++-- 4 files changed, 37 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index e7492e06..eb124003 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -6,12 +6,17 @@ import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailAmenityListResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; import com.swp.PodBookingSystem.dto.respone.PaginationResponse; +import com.swp.PodBookingSystem.entity.Account; +import com.swp.PodBookingSystem.service.AccountService; import com.swp.PodBookingSystem.service.OrderDetailAmenityService; import com.swp.PodBookingSystem.service.OrderDetailService; +import com.swp.PodBookingSystem.service.OrderService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; import java.util.List; @RestController @@ -21,18 +26,22 @@ public class OrderDetailAmenityController { OrderDetailAmenityService orderDetailAmenityService; OrderDetailService orderDetailService; + private final AccountService accountService; + private final OrderService orderService; @GetMapping("/page") public ApiResponse>> getOrderDetailAndAmenity( - //@RequestHeader("Authorization") String token, - //@RequestParam String startDate, - //@RequestParam String endDate, + @RequestHeader("Authorization") String token, + @RequestParam String startDate, + @RequestParam String endDate, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { - //String accountId = accountService.extractAccountIdFromToken(token); - // Account user = accountService.getAccountById(accountId); + String accountId = accountService.extractAccountIdFromToken(token); + Account user = accountService.getAccountById(accountId); + LocalDateTime startDateTime = orderService.parseDateTime(startDate); + LocalDateTime endDateTime = orderService.parseDateTime(endDate); return ApiResponse.>>builder() - .data(orderDetailService.getPagedOrderDetails(page, size)) + .data(orderDetailService.getPagedOrderDetails(user, startDateTime, endDateTime, page, size)) .message("get paging order detail successfully") .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java deleted file mode 100644 index 748a9119..00000000 --- a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityPaginationDTO.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.swp.PodBookingSystem.dto.request.OrderDetailAmenity; - -import com.swp.PodBookingSystem.dto.request.PaginationDTO; - -public class OrderDetailAmenityPaginationDTO extends PaginationDTO { - public OrderDetailAmenityPaginationDTO(int page, int take) { - super(page, take); - } -} \ No newline at end of file diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index 7075c9af..e7909d18 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -21,6 +21,18 @@ public interface OrderDetailRepository extends JpaRepository findByEndTime(LocalDateTime endTime); + @Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime ORDER BY od.createdAt DESC") + Page findAllWithTimeRange(@Param("startTime") LocalDateTime startTime, + @Param("endTime") LocalDateTime endTime, + Pageable pageable); + + @Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime AND od.building.id = :buildingNumber ORDER BY od.createdAt DESC") + Page findOrdersByBuildingNumberAndTimeRange( + @Param("buildingNumber") int buildingNumber, + @Param("startTime") LocalDateTime startTime, + @Param("endTime") LocalDateTime endTime, + Pageable pageable); + Page findAll(Pageable pageable); @Query("SELECT o FROM OrderDetail o WHERE FUNCTION('DATE', o.startTime) BETWEEN FUNCTION('DATE', :startOfDay) AND FUNCTION('DATE', :endOfDay)") diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index fc3a94f4..251b5b08 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -96,8 +96,16 @@ public List getOrdersByCustomerId(String customerId) { return orderDetailResponses; } - public PaginationResponse> getPagedOrderDetails(int page, int size) { - Page orderDetailPage = orderDetailRepository.findAll(PageRequest.of(page, size)); + public PaginationResponse> getPagedOrderDetails(Account user, LocalDateTime startDate, LocalDateTime endDate, int page, int size) { + Page orderDetailPage = null; + if(user.getRole() == AccountRole.Admin){ + orderDetailPage = orderDetailRepository.findAllWithTimeRange(startDate, endDate,PageRequest.of(page, size)); + }else if(user.getRole() == AccountRole.Staff || user.getRole() == AccountRole.Manager) { + orderDetailPage = orderDetailRepository.findOrdersByBuildingNumberAndTimeRange(user.getBuildingNumber(), startDate, endDate, PageRequest.of(page, size)); + }else{ + throw new RuntimeException("Only admin, staff and manager can access this API"); + } + List orderDetailResponses = orderDetailPage.getContent().stream() .map(orderDetail -> { List amenities = From f4c5deb3b27603b9cad320f9abbad11b6228a440 Mon Sep 17 00:00:00 2001 From: Huynh Nguyen Date: Wed, 23 Oct 2024 21:07:35 +0700 Subject: [PATCH 8/9] [NguyenHCP] chore: merge develop --- .../PodBookingSystem/service/OrderDetailService.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index 03e6d603..b2569dd8 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -103,11 +103,11 @@ public Page getOrdersByCustomerId(String customerId, int pa public PaginationResponse> getPagedOrderDetails(Account user, LocalDateTime startDate, LocalDateTime endDate, int page, int size) { Page orderDetailPage = null; - if(user.getRole() == AccountRole.Admin){ - orderDetailPage = orderDetailRepository.findAllWithTimeRange(startDate, endDate,PageRequest.of(page, size)); - }else if(user.getRole() == AccountRole.Staff || user.getRole() == AccountRole.Manager) { + if (user.getRole() == AccountRole.Admin) { + orderDetailPage = orderDetailRepository.findAllWithTimeRange(startDate, endDate, PageRequest.of(page, size)); + } else if (user.getRole() == AccountRole.Staff || user.getRole() == AccountRole.Manager) { orderDetailPage = orderDetailRepository.findOrdersByBuildingNumberAndTimeRange(user.getBuildingNumber(), startDate, endDate, PageRequest.of(page, size)); - }else{ + } else { throw new RuntimeException("Only admin, staff and manager can access this API"); } @@ -115,7 +115,7 @@ public PaginationResponse> getPagedOrderDet .map(orderDetail -> { List amenities = orderDetailAmenityService.getOrderDetailAmenitiesAllInfoByOrderDetailId(orderDetail.getId()); - OrderDetailAmenityListResponse response = OrderDetailAmenityListResponse.builder() + return OrderDetailAmenityListResponse.builder() .id(orderDetail.getId()) .customerId(Optional.ofNullable(orderDetail.getCustomer()) .map(Account::getId) @@ -135,7 +135,6 @@ public PaginationResponse> getPagedOrderDet .endTime(orderDetail.getEndTime()) .createdAt(orderDetail.getCreatedAt()) .build(); - return response; }) .collect(Collectors.toList()); From dff46c3805f8d8631c19624dfdebb90f3f13052b Mon Sep 17 00:00:00 2001 From: toki-ai Date: Wed, 23 Oct 2024 22:56:41 +0700 Subject: [PATCH 9/9] [PBS-84][Anh Dao] feat: update status of order detail amenity --- .../OrderDetailAmenityController.java | 15 +++++++++- .../OrderDetailAmenityUpdateReq.java | 14 +++++++++ .../OrderDetailAmenityListResponse.java | 6 ++-- .../OrderDetailAmenityResponseDTO.java | 29 +++++++++++++++++++ .../entity/OrderDetailAmenity.java | 5 ++++ .../enums/OrderDetailAmenityStatus.java | 15 ++++++++++ .../service/OrderDetailAmenityService.java | 14 +++++++++ .../service/OrderDetailService.java | 28 ++++++++++++++---- 8 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityUpdateReq.java create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponseDTO.java create mode 100644 src/main/java/com/swp/PodBookingSystem/enums/OrderDetailAmenityStatus.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index eb124003..c695b08a 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -1,6 +1,7 @@ package com.swp.PodBookingSystem.controller; import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityUpdateReq; import com.swp.PodBookingSystem.dto.respone.ApiResponse; import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailAmenityListResponse; @@ -68,5 +69,17 @@ public ApiResponse createOrderDetailAmenity (@RequestBody OrderDetailAme } } - //Update + @PutMapping + public ApiResponse updateOrderDetailAmenity(@RequestBody OrderDetailAmenityUpdateReq request) { + try { + orderDetailAmenityService.updateOrderDetailAmenityStatus(request); + return ApiResponse.builder() + .message("Update order detail amenity successfully") + .build(); + } catch (Exception e) { + return ApiResponse.builder() + .message("Failed to update order detail amenity: " + e.getMessage()) + .build(); + } + } } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityUpdateReq.java b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityUpdateReq.java new file mode 100644 index 00000000..6548e7fc --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/OrderDetailAmenity/OrderDetailAmenityUpdateReq.java @@ -0,0 +1,14 @@ +package com.swp.PodBookingSystem.dto.request.OrderDetailAmenity; +import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +public class OrderDetailAmenityUpdateReq { + String id; + OrderDetailAmenityStatus status; +} diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java index 62345cb7..1729507b 100644 --- a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetail/OrderDetailAmenityListResponse.java @@ -1,8 +1,6 @@ package com.swp.PodBookingSystem.dto.respone.OrderDetail; -import com.swp.PodBookingSystem.entity.OrderDetail; -import com.swp.PodBookingSystem.entity.OrderDetailAmenity; -import com.swp.PodBookingSystem.entity.Room; +import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponseDTO; import com.swp.PodBookingSystem.enums.OrderStatus; import lombok.*; @@ -21,7 +19,7 @@ public class OrderDetailAmenityListResponse { int roomId; String roomName; String orderId; - private List amenities; + private List orderDetailAmenities; int servicePackageId; String orderHandledId; double priceRoom; diff --git a/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponseDTO.java b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponseDTO.java new file mode 100644 index 00000000..9628f459 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/respone/OrderDetailAmenity/OrderDetailAmenityResponseDTO.java @@ -0,0 +1,29 @@ +package com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity; + +import com.swp.PodBookingSystem.entity.Amenity; +import com.swp.PodBookingSystem.enums.AmenityType; +import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; +import lombok.*; +import lombok.experimental.FieldDefaults; + +import java.time.LocalDateTime; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public class OrderDetailAmenityResponseDTO { + String id; + int quantity; + double price; + String orderDetailId; + int amenityId; + String amenityName; + AmenityType amenityType; + OrderDetailAmenityStatus status; + String statusDescription; + LocalDateTime createdAt; + LocalDateTime updatedAt; +} \ No newline at end of file diff --git a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java index cf11e542..956651aa 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/OrderDetailAmenity.java @@ -1,6 +1,7 @@ package com.swp.PodBookingSystem.entity; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; import jakarta.persistence.*; import lombok.*; import lombok.experimental.FieldDefaults; @@ -34,6 +35,10 @@ public class OrderDetailAmenity { @JoinColumn(name = "amenityId", nullable = false) Amenity amenity; + @Enumerated(EnumType.STRING) + @Column(name = "status") + private OrderDetailAmenityStatus status; + @Column(name = "createdAt") LocalDateTime createdAt; diff --git a/src/main/java/com/swp/PodBookingSystem/enums/OrderDetailAmenityStatus.java b/src/main/java/com/swp/PodBookingSystem/enums/OrderDetailAmenityStatus.java new file mode 100644 index 00000000..2a95098a --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/enums/OrderDetailAmenityStatus.java @@ -0,0 +1,15 @@ +package com.swp.PodBookingSystem.enums; +import lombok.Getter; + +@Getter +public enum OrderDetailAmenityStatus { + Booked("Đã đặt"), + Delivered("Đã giao"), + Canceled("Đã xóa"); + + private final String description; + + OrderDetailAmenityStatus(String description) { + this.description = description; + } +} diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java index 5116cbd4..48e3c0e5 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailAmenityService.java @@ -2,12 +2,14 @@ import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityRequest; import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityCreationRequest; +import com.swp.PodBookingSystem.dto.request.OrderDetailAmenity.OrderDetailAmenityUpdateReq; import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse; import com.swp.PodBookingSystem.entity.Amenity; import com.swp.PodBookingSystem.entity.OrderDetail; import com.swp.PodBookingSystem.entity.OrderDetailAmenity; import com.swp.PodBookingSystem.enums.AmenityType; +import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; import com.swp.PodBookingSystem.mapper.AmenityMapper; import com.swp.PodBookingSystem.mapper.OrderDetailAmenityMapper; import com.swp.PodBookingSystem.repository.AmenityRepository; @@ -57,6 +59,7 @@ public void createOrderDetailAmenity(OrderDetailAmenityRequest request) { throw new RuntimeException("Order detail or amenity not found"); } OrderDetailAmenity orderDetailAmenity = new OrderDetailAmenity(); + orderDetailAmenity.setStatus(OrderDetailAmenityStatus.Booked); orderDetailAmenity.setCreatedAt(LocalDateTime.now()); orderDetailAmenity.setUpdatedAt(LocalDateTime.now()); orderDetailAmenity.setId(UUID.randomUUID().toString()); @@ -115,6 +118,17 @@ public OrderDetailAmenityResponse createOrderDetailAmenityApi(OrderDetailAmenity .build(); } + //UPDATE: + public void updateOrderDetailAmenityStatus(OrderDetailAmenityUpdateReq request) { + Optional orderDetailAmenity = orderDetailAmenityRepository.findById(request.getId()); + if (orderDetailAmenity.isEmpty()) { + throw new RuntimeException("Order detail amenity not found"); + } + OrderDetailAmenity updateOrderDetailAmenity = orderDetailAmenity.get(); + updateOrderDetailAmenity.setStatus(request.getStatus()); + orderDetailAmenityRepository.save(updateOrderDetailAmenity); + } + //DELETE: @Transactional public double deleteOrderDetailAmenityByOrderDetailId(String orderDetailId) { diff --git a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java index b2569dd8..f3c8bb22 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/OrderDetailService.java @@ -8,9 +8,11 @@ import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailAmenityListResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailManagementResponse; import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse; +import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponseDTO; import com.swp.PodBookingSystem.dto.respone.PaginationResponse; import com.swp.PodBookingSystem.entity.*; import com.swp.PodBookingSystem.enums.AccountRole; +import com.swp.PodBookingSystem.enums.OrderDetailAmenityStatus; import com.swp.PodBookingSystem.enums.OrderStatus; import com.swp.PodBookingSystem.exception.AppException; import com.swp.PodBookingSystem.exception.ErrorCode; @@ -49,7 +51,6 @@ public class OrderDetailService { private final ServicePackageService servicePackageService; private final RoomService roomService; private final RoomRepository roomRepository; - private final OrderDetailAmenityRepository orderDetailAmenityRepository; //GET: public List getAllOrders() { @@ -102,7 +103,7 @@ public Page getOrdersByCustomerId(String customerId, int pa } public PaginationResponse> getPagedOrderDetails(Account user, LocalDateTime startDate, LocalDateTime endDate, int page, int size) { - Page orderDetailPage = null; + Page orderDetailPage; if (user.getRole() == AccountRole.Admin) { orderDetailPage = orderDetailRepository.findAllWithTimeRange(startDate, endDate, PageRequest.of(page, size)); } else if (user.getRole() == AccountRole.Staff || user.getRole() == AccountRole.Manager) { @@ -113,8 +114,25 @@ public PaginationResponse> getPagedOrderDet List orderDetailResponses = orderDetailPage.getContent().stream() .map(orderDetail -> { - List amenities = - orderDetailAmenityService.getOrderDetailAmenitiesAllInfoByOrderDetailId(orderDetail.getId()); + List amenities = + orderDetailAmenityService.getOrderDetailAmenitiesAllInfoByOrderDetailId(orderDetail.getId()).stream() + .map(oda -> OrderDetailAmenityResponseDTO.builder() + .id(oda.getId()) + .quantity(oda.getQuantity()) + .price(oda.getPrice()) + .orderDetailId(oda.getOrderDetail().getId()) + .amenityId(oda.getAmenity().getId()) + .amenityName(oda.getAmenity().getName()) + .amenityType(oda.getAmenity().getType()) + .status(Optional.ofNullable(oda.getStatus()) + .orElse(null)) + .statusDescription(Optional.ofNullable(oda.getStatus()) + .map(OrderDetailAmenityStatus::getDescription) + .orElse(null)) + .createdAt(oda.getCreatedAt()) + .updatedAt(oda.getUpdatedAt()) + .build()) + .collect(Collectors.toList()); return OrderDetailAmenityListResponse.builder() .id(orderDetail.getId()) .customerId(Optional.ofNullable(orderDetail.getCustomer()) @@ -124,7 +142,7 @@ public PaginationResponse> getPagedOrderDet .roomId(orderDetail.getRoom().getId()) .roomName(orderDetail.getRoom().getName()) .orderId(orderDetail.getOrder().getId()) - .amenities(amenities) + .orderDetailAmenities(amenities) .servicePackageId(orderDetail.getServicePackage().getId()) .orderHandledId(Optional.ofNullable(orderDetail.getOrderHandler()) .map(Account::getId)