Skip to content

Commit

Permalink
[NguyenHCP] fix: error OrderDetailAmenity Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhcp2004 committed Oct 15, 2024
1 parent 014cb64 commit f738452
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 237 deletions.
Original file line number Diff line number Diff line change
@@ -1,66 +1,83 @@
package com.swp.PodBookingSystem.controller;
package com.swp.PodBookingSystem.controller;

import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse;
import com.swp.PodBookingSystem.service.OrderDetailService;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.swp.PodBookingSystem.dto.respone.ApiResponse;
import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailResponse;
import com.swp.PodBookingSystem.entity.OrderDetailAmenity;
import com.swp.PodBookingSystem.service.OrderDetailAmenityService;
import com.swp.PodBookingSystem.service.OrderDetailService;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.swp.PodBookingSystem.dto.respone.ApiResponse;

import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.*;

@RestController
@RequestMapping("/order-detail")
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@Slf4j
public class OrderDetailController {
@RestController
@RequestMapping("/order-detail")
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@Slf4j
public class OrderDetailController {

@Autowired
private OrderDetailService orderDetailService;
@Autowired
private OrderDetailService orderDetailService;
@Autowired
private OrderDetailAmenityService orderDetailAmenityService;

@GetMapping
public ApiResponse<List<OrderDetailResponse>> getAllOrders() {
try{
List<OrderDetailResponse> orders = orderDetailService.getAllOrders();
logOrders(orders);
return ApiResponse.<List<OrderDetailResponse>>builder()
.data(orders)
.build();
}
catch (Exception e){
log.error("Error creating order detail: ", e);
return ApiResponse.<List<OrderDetailResponse>>builder()
.message("Failed to create order detail: " + e.getMessage())
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build();
}
}

@GetMapping("/{customerId}")
public ApiResponse<List<OrderDetailResponse>> getOrdersByCustomerId(@PathVariable String customerId) {
List<OrderDetailResponse> orders = orderDetailService.getOrdersByCustomerId(customerId);
@GetMapping
public ApiResponse<List<OrderDetailResponse>> getAllOrders() {
try {
List<OrderDetailResponse> orders = orderDetailService.getAllOrders();
logOrders(orders);
return ApiResponse.<List<OrderDetailResponse>>builder()
.data(orders)
.build();
} catch (Exception e) {
log.error("Error creating order detail: ", e);
return ApiResponse.<List<OrderDetailResponse>>builder()
.message("Failed to create order detail: " + e.getMessage())
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build();
}
}

@GetMapping("/{customerId}")
public ApiResponse<List<OrderDetailResponse>> getOrdersByCustomerId(@PathVariable String customerId) {
List<OrderDetailResponse> orders = orderDetailService.getOrdersByCustomerId(customerId);
logOrders(orders);
return ApiResponse.<List<OrderDetailResponse>>builder()
.data(orders)
.build();
}

// @GetMapping("/order-amenity/{orderDetailId}")
// public ApiResponse<List<AmenityManagementResponse>> getAmenitiesByOrdersByCustomerId(@PathVariable String orderDetailId) {
// List<AmenityManagementResponse> orders = orderDetailAmenityService.getOrderDetailAmenitiesByOrderDetailId(orderDetailId);
// return ApiResponse.<List<AmenityManagementResponse>>builder()
// .data(orders)
// .build();
// }

// @GetMapping("/order-amenitysAll")
// public ApiResponse<List<OrderDetailAmenity>> getAll() {
// List<OrderDetailAmenity> orders = orderDetailAmenityService.getAll();
// return ApiResponse.<List<OrderDetailAmenity>>builder()
// .data(orders)
// .build();
// }


private void logOrders(List<OrderDetailResponse> orders) {
var authentication = SecurityContextHolder.getContext().getAuthentication();
log.info("Username: {}", authentication.getName());
log.info("Number of orders retrieved: {}", orders.size());
orders.forEach(order -> log.info("Order ID: {}, Customer ID: {}", order.getId(), order.getCustomerId()));
}
private void logOrders(List<OrderDetailResponse> orders) {
var authentication = SecurityContextHolder.getContext().getAuthentication();
log.info("Username: {}", authentication.getName());
log.info("Number of orders retrieved: {}", orders.size());
orders.forEach(order -> log.info("Order ID: {}, Customer ID: {}", order.getId(), order.getCustomerId()));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@

package com.swp.PodBookingSystem.dto.respone.OrderDetail;

import com.swp.PodBookingSystem.dto.respone.Amenity.AmenityManagementResponse;
import com.swp.PodBookingSystem.dto.respone.AmenityResponse;
import com.swp.PodBookingSystem.enums.OrderStatus;
import lombok.*;
import lombok.experimental.FieldDefaults;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@FieldDefaults(level = AccessLevel.PRIVATE)
public class OrderDetailResponse {
private String id;
private String customerId;
private int buildingId;
private int roomId;
private String orderId;
private int servicePackageId;
private String orderHandledId;
private double priceRoom;
private OrderStatus status;
private LocalDateTime startTime;
private LocalDateTime endTime;
private LocalDateTime createdAt;

String id;
String customerId;
int buildingId;
int roomId;
String roomName;
String orderId;
List<AmenityManagementResponse> amenities;
int servicePackageId;
String orderHandledId;
double priceRoom;
OrderStatus status;
LocalDateTime startTime;
LocalDateTime endTime;
LocalDateTime createdAt;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.swp.PodBookingSystem.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;
Expand All @@ -11,7 +12,7 @@
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "orderDetailAmentity")
@Table(name = "orderDetailAmenity")
public class OrderDetailAmenity {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
Expand All @@ -22,10 +23,12 @@ public class OrderDetailAmenity {
double price;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "orderDetailId", nullable = false)
OrderDetail orderDetail;

@ManyToOne
@JoinColumn(name = "amentityId", nullable = false)
@JsonIgnore
@JoinColumn(name = "amenityId", nullable = false)
Amenity amenity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public interface OrderDetailMapper {
@Mapping(target = "roomId", source = "room.id") // Map room ID to response
@Mapping(target = "servicePackageId", source = "servicePackage.id") // Map service package ID to response
@Mapping(target = "orderId", source = "order.id") // Map order ID to response
@Mapping(target = "orderHandledId", source = "orderHandler.id") // Map order handled ID to response
@Mapping(target = "orderHandledId", source = "orderHandler.id")
@Mapping(target = "roomName", source = "room.name")
// Map order handled ID to response
OrderDetailResponse toOrderDetailResponse(OrderDetail orderDetail);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class OrderDetailAmenityService {
// return orderDetailAmenityRepository.findByOrderDetailId(orderDetailId);
// }

public List<OrderDetailAmenity> getAll() {
return orderDetailAmenityRepository.findAll();
}

public List<AmenityManagementResponse> getOrderDetailAmenitiesByOrderDetailId(String orderDetailId) {
return orderDetailAmenityRepository.findByOrderDetailId(orderDetailId).stream().map(amenity -> {
return AmenityManagementResponse.builder()
Expand Down
Loading

0 comments on commit f738452

Please sign in to comment.