Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 151a392

Browse files
authored
Merge pull request #66 from Zenfulcode/65-fix-order-customer-details
65 fix order customer details
2 parents 5497993 + 12ca287 commit 151a392

13 files changed

Lines changed: 134 additions & 10 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<dependency>
136136
<groupId>org.liquibase</groupId>
137137
<artifactId>liquibase-core</artifactId>
138+
<version>4.30.0</version>
138139
</dependency>
139140
<dependency>
140141
<groupId>org.springframework.retry</groupId>

src/main/java/com/zenfulcode/commercify/commercify/api/requests/orders/CreateOrderRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.zenfulcode.commercify.commercify.api.requests.orders;
22

33
import com.zenfulcode.commercify.commercify.dto.AddressDTO;
4+
import com.zenfulcode.commercify.commercify.dto.CustomerDetailsDTO;
45

56
import java.util.List;
67

78
public record CreateOrderRequest(
89
String currency,
10+
CustomerDetailsDTO customerDetails,
911
List<CreateOrderLineRequest> orderLines,
1012
AddressDTO shippingAddress,
1113
AddressDTO billingAddress

src/main/java/com/zenfulcode/commercify/commercify/controller/OrderController.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public class OrderController {
3737
"id", "userId", "status", "currency", "totalAmount", "createdAt", "updatedAt"
3838
);
3939

40-
@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id")
4140
@PostMapping("/{userId}")
42-
public ResponseEntity<?> createOrder(@PathVariable Long userId, @Validated @RequestBody CreateOrderRequest orderRequest) {
41+
public ResponseEntity<?> createOrder(@PathVariable Long userId, @RequestBody CreateOrderRequest orderRequest) {
4342
try {
4443
OrderDTO orderDTO = orderService.createOrder(userId, orderRequest);
4544
return ResponseEntity.ok(CreateOrderResponse.from(OrderViewModel.fromDTO(orderDTO)));
@@ -62,6 +61,30 @@ public ResponseEntity<?> createOrder(@PathVariable Long userId, @Validated @Requ
6261
}
6362
}
6463

64+
@PostMapping
65+
public ResponseEntity<?> createOrder(@RequestBody CreateOrderRequest orderRequest) {
66+
try {
67+
OrderDTO orderDTO = orderService.createOrder(orderRequest);
68+
return ResponseEntity.ok(CreateOrderResponse.from(OrderViewModel.fromDTO(orderDTO)));
69+
} catch (IllegalArgumentException e) {
70+
return ResponseEntity.badRequest()
71+
.body(CreateOrderResponse.from("Invalid request: " + e.getMessage()));
72+
} catch (ProductNotFoundException e) {
73+
return ResponseEntity.badRequest()
74+
.body(CreateOrderResponse.from("Product not found: " + e.getMessage()));
75+
} catch (InsufficientStockException e) {
76+
return ResponseEntity.badRequest()
77+
.body(CreateOrderResponse.from("Insufficient stock: " + e.getMessage()));
78+
} catch (OrderValidationException e) {
79+
return ResponseEntity.badRequest()
80+
.body(CreateOrderResponse.from(e.getMessage()));
81+
} catch (Exception e) {
82+
log.error("Error creating order", e);
83+
return ResponseEntity.internalServerError()
84+
.body(CreateOrderResponse.from("Error creating order: " + e.getMessage()));
85+
}
86+
}
87+
6588
@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id or hasRole('ADMIN')")
6689
@GetMapping("/user/{userId}")
6790
public ResponseEntity<?> getOrdersByUserId(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.zenfulcode.commercify.commercify.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
@Builder
8+
@Data
9+
@AllArgsConstructor
10+
public class CustomerDetailsDTO {
11+
private String firstName;
12+
private String lastName;
13+
private String email;
14+
private String phone;
15+
}

src/main/java/com/zenfulcode/commercify/commercify/dto/OrderDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
public class OrderDTO {
1414
private Long id;
1515
private Long userId;
16-
private OrderStatus orderStatus;
1716
private String currency;
1817
private Double totalAmount;
18+
private OrderStatus orderStatus;
1919
private Instant createdAt;
2020
private Instant updatedAt;
2121
}

src/main/java/com/zenfulcode/commercify/commercify/dto/OrderDetailsDTO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
public class OrderDetailsDTO {
1313
private OrderDTO order;
1414
private List<OrderLineDTO> orderLines;
15+
private CustomerDetailsDTO customerDetails;
16+
private AddressDTO shippingAddress;
17+
private AddressDTO billingAddress;
1518
}

src/main/java/com/zenfulcode/commercify/commercify/dto/mapper/OrderMapper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ public OrderDTO apply(OrderEntity order) {
2323
.totalAmount(order.getTotalAmount())
2424
.build();
2525
}
26-
2726
}

src/main/java/com/zenfulcode/commercify/commercify/entity/OrderEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class OrderEntity {
3030
@Column(name = "id", nullable = false)
3131
private Long id;
3232

33-
@Column(name = "user_id", nullable = false)
33+
@Column(name = "user_id")
3434
private Long userId;
3535

3636
@ToString.Exclude

src/main/java/com/zenfulcode/commercify/commercify/entity/OrderShippingInfo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class OrderShippingInfo {
1717
@Column(name = "id", nullable = false)
1818
private Long id;
1919

20+
@Column(name = "customer_first_name")
21+
private String customerFirstName;
22+
@Column(name = "customer_last_name")
23+
private String customerLastName;
24+
@Column(name = "customer_email")
25+
private String customerEmail;
26+
@Column(name = "customer_phone")
27+
private String customerPhone;
28+
2029
@Column(name = "shipping_street", nullable = false)
2130
private String shippingStreet;
2231
@Column(name = "shipping_city", nullable = false)

src/main/java/com/zenfulcode/commercify/commercify/service/order/OrderService.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import com.zenfulcode.commercify.commercify.OrderStatus;
44
import com.zenfulcode.commercify.commercify.api.requests.orders.CreateOrderLineRequest;
55
import com.zenfulcode.commercify.commercify.api.requests.orders.CreateOrderRequest;
6-
import com.zenfulcode.commercify.commercify.dto.AddressDTO;
7-
import com.zenfulcode.commercify.commercify.dto.OrderDTO;
8-
import com.zenfulcode.commercify.commercify.dto.OrderDetailsDTO;
9-
import com.zenfulcode.commercify.commercify.dto.OrderLineDTO;
6+
import com.zenfulcode.commercify.commercify.dto.*;
107
import com.zenfulcode.commercify.commercify.dto.mapper.OrderMapper;
118
import com.zenfulcode.commercify.commercify.dto.mapper.ProductMapper;
129
import com.zenfulcode.commercify.commercify.dto.mapper.ProductVariantMapper;
@@ -44,6 +41,11 @@ public class OrderService {
4441
private final ProductVariantMapper productVariantMapper;
4542
private final OrderShippingInfoRepository orderShippingInfoRepository;
4643

44+
@Transactional
45+
public OrderDTO createOrder(CreateOrderRequest request) {
46+
return createOrder(null, request);
47+
}
48+
4749
@Transactional
4850
public OrderDTO createOrder(Long userId, CreateOrderRequest request) {
4951
// Validate request and check stock
@@ -83,6 +85,11 @@ private OrderShippingInfo getShippingInformation(CreateOrderRequest request) {
8385
.billingCountry(billingAddress.getCountry());
8486
}
8587

88+
shippingInfo.customerEmail(request.customerDetails().getEmail())
89+
.customerFirstName(request.customerDetails().getFirstName())
90+
.customerLastName(request.customerDetails().getLastName())
91+
.customerPhone(request.customerDetails().getPhone());
92+
8693
return shippingInfo.build();
8794
}
8895

@@ -250,6 +257,34 @@ private OrderDetailsDTO buildOrderDetailsDTO(OrderEntity order) {
250257
})
251258
.collect(Collectors.toList());
252259

253-
return new OrderDetailsDTO(orderMapper.apply(order), orderLines);
260+
OrderDTO orderDTO = orderMapper.apply(order);
261+
262+
OrderShippingInfo shippingInfo = order.getOrderShippingInfo();
263+
264+
System.out.println("shippingInfo = " + shippingInfo);
265+
266+
CustomerDetailsDTO customerDetails = CustomerDetailsDTO.builder()
267+
.email(shippingInfo.getCustomerEmail())
268+
.firstName(shippingInfo.getCustomerFirstName())
269+
.lastName(shippingInfo.getCustomerLastName())
270+
.build();
271+
272+
AddressDTO shippingAddress = AddressDTO.builder()
273+
.city(shippingInfo.getShippingCity())
274+
.country(shippingInfo.getShippingCountry())
275+
.state(shippingInfo.getShippingState())
276+
.street(shippingInfo.getShippingStreet())
277+
.zipCode(shippingInfo.getShippingZip())
278+
.build();
279+
280+
AddressDTO billingAddress = AddressDTO.builder()
281+
.city(shippingInfo.getBillingCity())
282+
.country(shippingInfo.getBillingCountry())
283+
.state(shippingInfo.getBillingState())
284+
.street(shippingInfo.getBillingStreet())
285+
.zipCode(shippingInfo.getBillingZip())
286+
.build();
287+
288+
return new OrderDetailsDTO(orderDTO, orderLines, customerDetails, shippingAddress, billingAddress);
254289
}
255290
}

0 commit comments

Comments
 (0)