Skip to content

Commit 7d9169b

Browse files
committed
update
1 parent c4d067c commit 7d9169b

File tree

10 files changed

+108
-22
lines changed

10 files changed

+108
-22
lines changed

payment-service/src/main/java/com/hoangtien2k3/paymentservice/api/PaymentController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.hoangtien2k3.paymentservice.api;
22

3+
import com.hoangtien2k3.paymentservice.dto.OrderDto;
34
import com.hoangtien2k3.paymentservice.dto.PaymentDto;
45
import com.hoangtien2k3.paymentservice.service.PaymentService;
6+
import com.hoangtien2k3.paymentservice.service.impl.PaymentServiceImpl;
57
import io.swagger.annotations.ApiOperation;
68
import io.swagger.annotations.ApiResponse;
79
import io.swagger.annotations.ApiResponses;
@@ -30,6 +32,9 @@ public class PaymentController {
3032
@Autowired
3133
private final PaymentService paymentService;
3234

35+
@Autowired
36+
private final PaymentServiceImpl paymentServiceImpl;
37+
3338
@ApiOperation(value = "Get all payment", notes = "Retrieve a list of all payment.")
3439
@ApiResponses({
3540
@ApiResponse(code = 200, message = "Payments retrieved successfully", response = List.class),
@@ -74,6 +79,13 @@ public ResponseEntity<Mono<PaymentDto>> findById(@PathVariable("paymentId")
7479
return ResponseEntity.ok(paymentService.findById(Integer.parseInt(paymentId)));
7580
}
7681

82+
83+
@GetMapping("/getOrder/{orderId}")
84+
public ResponseEntity<Mono<OrderDto>> getOrderDto(@PathVariable("orderId") final Integer orderId) {
85+
return ResponseEntity.ok(paymentServiceImpl.getOrderDto(orderId));
86+
}
87+
88+
7789
@ApiOperation(value = "Save payment", notes = "Save a new payment.")
7890
@ApiResponses({
7991
@ApiResponse(code = 200, message = "Payment saved successfully", response = PaymentDto.class),

payment-service/src/main/java/com/hoangtien2k3/paymentservice/config/client/ClientConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public RestTemplate restTemplateBean() {
1414
return new RestTemplate();
1515
}
1616

17-
@LoadBalanced
17+
// @LoadBalanced
1818
@Bean
1919
public WebClient.Builder webClientBuilder() {
2020
return WebClient.builder();

payment-service/src/main/java/com/hoangtien2k3/paymentservice/dto/OrderDto.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.hoangtien2k3.paymentservice.dto;
22

33
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
47
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
58
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
69
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
@@ -31,8 +34,9 @@ public class OrderDto implements Serializable {
3134
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
3235
@JsonFormat(pattern = AppConstant.LOCAL_DATE_TIME_FORMAT, shape = JsonFormat.Shape.STRING)
3336
@DateTimeFormat(pattern = AppConstant.LOCAL_DATE_TIME_FORMAT)
34-
private LocalDateTime orderDate;
35-
private String orderDesc;
36-
private Double orderFee;
37+
@JsonInclude(Include.NON_NULL) private LocalDateTime orderDate;
38+
@JsonInclude(Include.NON_NULL) private String orderDesc;
39+
@JsonInclude(Include.NON_NULL) private Double orderFee;
40+
@JsonInclude(Include.NON_NULL) private Integer productId;
3741

3842
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.hoangtien2k3.paymentservice.dto;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.hoangtien2k3.paymentservice.entity.PaymentStatus;
6-
import lombok.AllArgsConstructor;
7-
import lombok.Builder;
8-
import lombok.Data;
9-
import lombok.NoArgsConstructor;
7+
import lombok.*;
108

119
import java.io.Serial;
1210
import java.io.Serializable;
1311

1412
@NoArgsConstructor
1513
@AllArgsConstructor
1614
@Data
15+
@Setter
16+
@Getter
1717
@Builder
1818
public class PaymentDto implements Serializable {
19+
1920
@Serial
2021
private static final long serialVersionUID = 1L;
2122

@@ -24,9 +25,14 @@ public class PaymentDto implements Serializable {
2425
private PaymentStatus paymentStatus;
2526

2627
private Integer orderId;
28+
private Long userId;
2729

2830
@JsonProperty("order")
29-
@JsonInclude(JsonInclude.Include.NON_NULL)
31+
@JsonInclude(Include.NON_NULL)
3032
private OrderDto orderDto;
3133

34+
@JsonProperty("user")
35+
@JsonInclude(Include.NON_NULL)
36+
private UserDto userDto;
37+
3238
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hoangtien2k3.paymentservice.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serial;
11+
import java.io.Serializable;
12+
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
@Data
16+
@Builder
17+
public class UserDto implements Serializable {
18+
19+
@Serial
20+
private static final long serialVersionUID = 1L;
21+
22+
private Long id;
23+
@JsonInclude(Include.NON_NULL) private String fullname;
24+
@JsonInclude(Include.NON_NULL) private String username;
25+
@JsonInclude(Include.NON_NULL) private String email;
26+
@JsonInclude(Include.NON_NULL) private String gender;
27+
@JsonInclude(Include.NON_NULL) private String phone;
28+
@JsonInclude(Include.NON_NULL) private String avatar;
29+
30+
}

payment-service/src/main/java/com/hoangtien2k3/paymentservice/entity/Payment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class Payment extends AbstractMappedEntity implements Serializable {
2626
@Column(name = "order_id")
2727
private Integer orderId;
2828

29+
@Column(name = "user_id")
30+
private Long userId;
31+
2932
@Column(name = "is_payed")
3033
private Boolean isPayed;
3134

payment-service/src/main/java/com/hoangtien2k3/paymentservice/helper/PaymentMappingHelper.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@
22

33
import com.hoangtien2k3.paymentservice.dto.OrderDto;
44
import com.hoangtien2k3.paymentservice.dto.PaymentDto;
5+
import com.hoangtien2k3.paymentservice.dto.UserDto;
56
import com.hoangtien2k3.paymentservice.entity.Payment;
67

78
public interface PaymentMappingHelper {
8-
public static PaymentDto map(final Payment payment) {
9+
static PaymentDto map(final Payment payment) {
910
return PaymentDto.builder()
1011
.paymentId(payment.getPaymentId())
1112
.isPayed(payment.getIsPayed())
1213
.paymentStatus(payment.getPaymentStatus())
14+
.orderId(payment.getOrderId())
15+
.userId(payment.getUserId())
1316
.orderDto(
1417
OrderDto.builder()
1518
.orderId(payment.getOrderId())
1619
.build())
20+
.userDto(UserDto.builder()
21+
.id(payment.getUserId())
22+
.build())
1723
.build();
1824
}
1925

20-
public static Payment map(final PaymentDto paymentDto) {
26+
static Payment map(final PaymentDto paymentDto) {
2127
return Payment.builder()
2228
.paymentId(paymentDto.getPaymentId())
23-
.orderId(paymentDto.getOrderDto().getOrderId())
29+
.orderId(paymentDto.getOrderId())
30+
.userId(paymentDto.getUserId())
2431
.isPayed(paymentDto.getIsPayed())
2532
.paymentStatus(paymentDto.getPaymentStatus())
2633
.build();

payment-service/src/main/java/com/hoangtien2k3/paymentservice/service/CallAPI.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hoangtien2k3.paymentservice.service;
22

33
import com.hoangtien2k3.paymentservice.dto.OrderDto;
4+
import com.hoangtien2k3.paymentservice.dto.UserDto;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.http.HttpHeaders;
67
import org.springframework.stereotype.Component;
@@ -25,4 +26,13 @@ public Mono<OrderDto> receiverPaymentDto(Integer orderId, String token) {
2526
.bodyToMono(OrderDto.class);
2627
}
2728

29+
public Mono<UserDto> receiverUserDto(Long userId, String token) {
30+
return webClientBuilder.baseUrl("http://localhost:8088").build()
31+
.get()
32+
.uri("/api/manager/user/" + userId)
33+
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
34+
.retrieve()
35+
.bodyToMono(UserDto.class);
36+
}
37+
2838
}

payment-service/src/main/java/com/hoangtien2k3/paymentservice/service/impl/PaymentServiceImpl.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.hoangtien2k3.paymentservice.service.impl;
22

3+
import com.hoangtien2k3.paymentservice.dto.OrderDto;
34
import com.hoangtien2k3.paymentservice.dto.PaymentDto;
5+
import com.hoangtien2k3.paymentservice.dto.UserDto;
46
import com.hoangtien2k3.paymentservice.exception.wrapper.PaymentNotFoundException;
57
import com.hoangtien2k3.paymentservice.helper.PaymentMappingHelper;
68
import com.hoangtien2k3.paymentservice.repository.PaymentRepository;
@@ -45,7 +47,7 @@ public Mono<List<PaymentDto>> findAll() {
4547
.flatMap(paymentDto ->
4648
callAPI.receiverPaymentDto(paymentDto.getOrderId(), JwtTokenFilter.getTokenFromRequest())
4749
.map(orderDto -> {
48-
paymentDto.setOrderDto(orderDto);
50+
paymentDto.setOrderDto(modelMapper.map(orderDto, OrderDto.class));
4951
return paymentDto;
5052
})
5153
.onErrorResume(throwable -> {
@@ -69,7 +71,7 @@ public Mono<Page<PaymentDto>> findAll(int page, int size, String sortBy, String
6971
.flatMap(paymentDto ->
7072
callAPI.receiverPaymentDto(paymentDto.getOrderId(), JwtTokenFilter.getTokenFromRequest())
7173
.map(orderDto -> {
72-
paymentDto.setOrderDto(orderDto);
74+
paymentDto.setOrderDto(modelMapper.map(orderDto, OrderDto.class));
7375
return paymentDto;
7476
})
7577
.onErrorResume(throwable -> {
@@ -90,18 +92,30 @@ public Mono<PaymentDto> findById(Integer paymentId) {
9092
.orElseThrow(() -> new PaymentNotFoundException(String.format("Order with id: %d not found", paymentId)))
9193
)
9294
.flatMap(paymentDto ->
93-
callAPI.receiverPaymentDto(paymentDto.getOrderId(), JwtTokenFilter.getTokenFromRequest())
94-
.map(orderDto -> {
95-
paymentDto.setOrderDto(orderDto);
96-
return paymentDto;
95+
callAPI.receiverPaymentDto(paymentDto.getOrderDto().getOrderId(), JwtTokenFilter.getTokenFromRequest())
96+
.flatMap(orderDto -> {
97+
paymentDto.setOrderDto(modelMapper.map(orderDto, OrderDto.class));
98+
99+
return callAPI.receiverUserDto(paymentDto.getUserId(), JwtTokenFilter.getTokenFromRequest())
100+
.map(userDto -> {
101+
paymentDto.setUserDto(modelMapper.map(userDto, UserDto.class));
102+
return paymentDto;
103+
})
104+
.switchIfEmpty(Mono.just(paymentDto));
105+
97106
})
98107
.onErrorResume(throwable -> {
99-
log.error("Error fetching order info: {}", throwable.getMessage());
108+
log.error("Error fetching order or user info: {}", throwable.getMessage());
100109
return Mono.just(paymentDto);
101110
})
102111
);
103112
}
104113

114+
public Mono<OrderDto> getOrderDto(Integer orderId) {
115+
return callAPI.receiverPaymentDto(orderId, JwtTokenFilter.getTokenFromRequest())
116+
.map(orderDto -> modelMapper.map(orderDto, OrderDto.class));
117+
}
118+
105119
@Override
106120
public Mono<PaymentDto> save(PaymentDto paymentDto) {
107121
log.info("PaymentDto, service; save order");

payment-service/src/main/resources/application.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ spring.datasource.username=root
1313
spring.datasource.password=12042003
1414

1515
# JPA configuration
16-
spring.jpa.show-sql=true
16+
spring.jpa.show-sql=false
1717
spring.jpa.hibernate.ddl-auto=update
1818
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
19-
spring.jpa.properties.hibernate.use_sql_comments=true
20-
spring.jpa.properties.hibernate.format_sql=true
19+
spring.jpa.properties.hibernate.use_sql_comments=false
20+
spring.jpa.properties.hibernate.format_sql=false
2121

2222
# Logging configuration
2323
logging.file.name=src/main/resources/script/prod_log.log

0 commit comments

Comments
 (0)