Skip to content

Commit

Permalink
Merge pull request #101 from Hoang-Nguyen-Huy/PBS-84-Manage-Order-Ame…
Browse files Browse the repository at this point in the history
…nities

Pbs 84 manage order amenities
  • Loading branch information
nguyenhcp2004 authored Oct 24, 2024
2 parents 530ddc6 + dff46c3 commit 6fdf866
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
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.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;
import com.swp.PodBookingSystem.dto.respone.OrderDetailAmenity.OrderDetailAmenityResponse;
import com.swp.PodBookingSystem.entity.OrderDetailAmenity;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/order-detail-amenity")
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class OrderDetailAmenityController {
OrderDetailAmenityService orderDetailAmenityService;
OrderDetailService orderDetailService;
private final AccountService accountService;
private final OrderService orderService;

@GetMapping("/page")
public ApiResponse<PaginationResponse<List<OrderDetailAmenityListResponse>>> 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);
LocalDateTime startDateTime = orderService.parseDateTime(startDate);
LocalDateTime endDateTime = orderService.parseDateTime(endDate);
return ApiResponse.<PaginationResponse<List<OrderDetailAmenityListResponse>>>builder()
.data(orderDetailService.getPagedOrderDetails(user, startDateTime, endDateTime, page, size))
.message("get paging order detail successfully")
.build();
}

@PostMapping
public ApiResponse<OrderDetailAmenityResponse> createOrderDetailAmenity(@RequestBody OrderDetailAmenityCreationRequest request) {
Expand All @@ -28,4 +54,32 @@ public ApiResponse<OrderDetailAmenityResponse> createOrderDetailAmenity(@Request
.message("Tạo đơn hàng tiện ích thành công")
.build();
}

@PostMapping("/create")
public ApiResponse<String> createOrderDetailAmenity (@RequestBody OrderDetailAmenityRequest orderDetailAmenityRequest) {
try{
orderDetailAmenityService.createOrderDetailAmenity(orderDetailAmenityRequest);
return ApiResponse.<String>builder()
.message("Create order detail amenity successfully")
.build();
} catch (Exception e) {
return ApiResponse.<String>builder()
.message("Failed to create order detail amenity: " + e.getMessage())
.build();
}
}

@PutMapping
public ApiResponse<String> updateOrderDetailAmenity(@RequestBody OrderDetailAmenityUpdateReq request) {
try {
orderDetailAmenityService.updateOrderDetailAmenityStatus(request);
return ApiResponse.<String>builder()
.message("Update order detail amenity successfully")
.build();
} catch (Exception e) {
return ApiResponse.<String>builder()
.message("Failed to update order detail amenity: " + e.getMessage())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -21,7 +19,7 @@ public class OrderDetailAmenityListResponse {
int roomId;
String roomName;
String orderId;
private List<OrderDetailAmenity> amenities;
private List<OrderDetailAmenityResponseDTO> orderDetailAmenities;
int servicePackageId;
String orderHandledId;
double priceRoom;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
Expand All @@ -31,4 +34,25 @@ public class OrderDetailAmenity {
@JsonIgnore
@JoinColumn(name = "amenityId", nullable = false)
Amenity amenity;

@Enumerated(EnumType.STRING)
@Column(name = "status")
private OrderDetailAmenityStatus status;

@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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public interface OrderDetailRepository extends JpaRepository<OrderDetail, String

List<OrderDetail> findByEndTime(LocalDateTime endTime);

@Query(value = "SELECT od FROM OrderDetail od WHERE od.createdAt BETWEEN :startTime AND :endTime ORDER BY od.createdAt DESC")
Page<OrderDetail> 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<OrderDetail> findOrdersByBuildingNumberAndTimeRange(
@Param("buildingNumber") int buildingNumber,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
Pageable pageable);

Page<OrderDetail> findAll(Pageable pageable);

@Query("SELECT o FROM OrderDetail o WHERE FUNCTION('DATE', o.startTime) BETWEEN FUNCTION('DATE', :startOfDay) AND FUNCTION('DATE', :endOfDay)")
List<OrderDetail> findAllOrderDetailsByDay(@Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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.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;
Expand All @@ -15,11 +18,13 @@
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.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
Expand All @@ -42,6 +47,40 @@ public List<AmenityManagementResponse> getOrderDetailAmenitiesByOrderDetailId(St
.build()).collect(Collectors.toList());
}

public List<OrderDetailAmenity> getOrderDetailAmenitiesAllInfoByOrderDetailId(String orderDetailId) {
return orderDetailAmenityRepository.findByOrderDetailId(orderDetailId);
}

//CREATE:
public void createOrderDetailAmenity(OrderDetailAmenityRequest request) {
Optional<OrderDetail> orderDetail = orderDetailRepository.findById(request.getOrderDetailId());
Optional<Amenity> amenity = amenityRepository.findById(request.getAmenityId());
if(orderDetail.isEmpty() || amenity.isEmpty()) {
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());
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
*/
Expand Down Expand Up @@ -79,20 +118,15 @@ public OrderDetailAmenityResponse createOrderDetailAmenityApi(OrderDetailAmenity
.build();
}

//CREATE in orderDetail
public void createOrderDetailAmenity(OrderDetailAmenity orderDetailAmenity){
Optional<Amenity> amenity = amenityRepository.findById(orderDetailAmenity.getAmenity().getId());
if (amenity.isEmpty()) {
throw new RuntimeException("Amenity not found");
//UPDATE:
public void updateOrderDetailAmenityStatus(OrderDetailAmenityUpdateReq request) {
Optional<OrderDetailAmenity> orderDetailAmenity = orderDetailAmenityRepository.findById(request.getId());
if (orderDetailAmenity.isEmpty()) {
throw new RuntimeException("Order detail 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);
OrderDetailAmenity updateOrderDetailAmenity = orderDetailAmenity.get();
updateOrderDetailAmenity.setStatus(request.getStatus());
orderDetailAmenityRepository.save(updateOrderDetailAmenity);
}

//DELETE:
Expand Down
Loading

0 comments on commit 6fdf866

Please sign in to comment.