Skip to content

Commit 28831de

Browse files
committed
payment-service update
1 parent 7d9169b commit 28831de

39 files changed

+1022
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ spring.datasource.password=12042003
1313
spring.jpa.show-sql=false
1414
spring.jpa.hibernate.ddl-auto=update
1515
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
16-
spring.jpa.properties.hibernate.use_sql_comments=true
17-
spring.jpa.properties.hibernate.format_sql=true
16+
spring.jpa.properties.hibernate.use_sql_comments=false
17+
spring.jpa.properties.hibernate.format_sql=false
1818

1919
# log api for resources
2020
logging.file.name=src/main/resources/script/prod_log.log

notification-service/src/main/java/com/hoangtien2k3/notificationservice/api/NotificationController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public ResponseEntity<Notification> saveNotification(@RequestBody Notification n
4040
}
4141

4242
@DeleteMapping("/{id}")
43-
public ResponseEntity<Void> deleteNotification(@PathVariable String id) {
43+
public ResponseEntity<Boolean> deleteNotification(@PathVariable String id) {
4444
notificationService.deleteNotificationById(id);
45-
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
45+
return new ResponseEntity<>(true, HttpStatus.OK);
4646
}
4747

4848
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.hoangtien2k3.notificationservice.api;
2+
3+
import com.hoangtien2k3.notificationservice.dto.PaymentDto;
4+
import com.hoangtien2k3.notificationservice.entity.Payment;
5+
import com.hoangtien2k3.notificationservice.service.PaymentService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("/api/payments")
14+
public class PaymentController {
15+
16+
private final PaymentService paymentService;
17+
18+
@Autowired
19+
public PaymentController(PaymentService paymentService) {
20+
this.paymentService = paymentService;
21+
}
22+
23+
@PostMapping
24+
public Mono<Payment> savePayment(@RequestBody PaymentDto paymentDto) {
25+
return paymentService.savePayment(paymentDto);
26+
}
27+
28+
@GetMapping("/{paymentId}")
29+
public Mono<Payment> getPayment(@PathVariable Integer paymentId) {
30+
return paymentService.getPayment(paymentId);
31+
}
32+
33+
@GetMapping
34+
public Mono<List<Payment>> getAllPayments() {
35+
return paymentService.getAllPayments();
36+
}
37+
38+
@DeleteMapping("/{paymentId}")
39+
public Mono<Void> deletePayment(@PathVariable Integer paymentId) {
40+
return paymentService.deletePayment(paymentId);
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.hoangtien2k3.notificationservice.constant;
2+
3+
public class KafkaConstant {
4+
public static final String PROFILE_ONBOARDING_TOPIC = "profileOnboarding";
5+
public static final String PROFILE_ONBOARDED_TOPIC = "profileOnboarded";
6+
7+
public static final String STATUS_PROFILE_PENDING = "PENDING";
8+
public static final String STATUS_PROFILE_ACTIVE = "ACTIVE";
9+
10+
public static final String STATUS_PAYMENT_CREATING = "CREATING";
11+
public static final String STATUS_PAYMENT_REJECTED = "REJECTED";
12+
public static final String STATUS_PAYMENT_PROCESSING = "PROCESSING";
13+
public static final String STATUS_PAYMENT_SUCCESSFUL = "SUCCESSFUL";
14+
15+
public static final String PAYMENT_REQUEST_TOPIC = "paymentRequest";
16+
public static final String PAYMENT_CREATED_TOPIC = "paymentCreated";
17+
public static final String PAYMENT_COMPLETED_TOPIC = "paymentCompleted";
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.hoangtien2k3.notificationservice.dto;
2+
3+
import com.hoangtien2k3.notificationservice.entity.PaymentStatus;
4+
import lombok.*;
5+
6+
import java.io.Serial;
7+
import java.io.Serializable;
8+
9+
@NoArgsConstructor
10+
@AllArgsConstructor
11+
@Data
12+
@Setter
13+
@Getter
14+
@Builder
15+
public class PaymentDto implements Serializable {
16+
17+
@Serial
18+
private static final long serialVersionUID = 1L;
19+
20+
private Integer paymentId;
21+
private Boolean isPayed;
22+
private PaymentStatus paymentStatus;
23+
24+
private Integer orderId;
25+
private Long userId;
26+
27+
}

notification-service/src/main/java/com/hoangtien2k3/notificationservice/entity/Notification.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
56
import lombok.Setter;
67
import org.springframework.data.annotation.Id;
78
import org.springframework.data.mongodb.core.mapping.Document;
89
import org.springframework.data.mongodb.core.mapping.Field;
910

1011
import java.time.LocalDateTime;
1112

12-
//@Entity
1313
@Setter
14-
@Getter@AllArgsConstructor
14+
@Getter
15+
@AllArgsConstructor
1516
@Document(collection = "notifications")
1617
public class Notification {
1718

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hoangtien2k3.notificationservice.entity;
2+
3+
import lombok.*;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
7+
@Setter
8+
@Getter
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@Builder
12+
@Document(collection = "payments")
13+
public class Payment {
14+
15+
@Id
16+
private String id;
17+
18+
private Integer paymentId;
19+
private Boolean isPayed;
20+
private PaymentStatus paymentStatus;
21+
22+
private Integer orderId;
23+
private Long userId;
24+
25+
}
26+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hoangtien2k3.notificationservice.entity;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@RequiredArgsConstructor
7+
@Getter
8+
public enum PaymentStatus {
9+
10+
NOT_STARTED("not_started"),
11+
IN_PROGRESS("in_progress"),
12+
COMPLETED("completed");
13+
14+
private final String status;
15+
16+
}
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.hoangtien2k3.notificationservice.event;
22

33
import com.google.gson.Gson;
4-
import com.hoangtien2k3.notificationservice.config.KafkaConstant;
4+
import com.hoangtien2k3.notificationservice.constant.KafkaConstant;
55
import com.hoangtien2k3.notificationservice.dto.EmailDetails;
6+
import com.hoangtien2k3.notificationservice.dto.PaymentDto;
67
import com.hoangtien2k3.notificationservice.service.EmailService;
8+
import com.hoangtien2k3.notificationservice.service.NotificationService;
9+
import com.hoangtien2k3.notificationservice.service.PaymentService;
710
import lombok.extern.slf4j.Slf4j;
811
import org.springframework.beans.factory.annotation.Autowired;
912
import org.springframework.stereotype.Service;
@@ -12,24 +15,44 @@
1215
import reactor.kafka.receiver.ReceiverRecord;
1316

1417
import java.util.Collections;
18+
import java.util.function.Consumer;
1519

1620
@Service
1721
@Slf4j
1822
public class EventConsumer {
23+
1924
Gson gson = new Gson(); // convert Json -> DTO
2025

2126
@Autowired
2227
private EmailService emailService;
2328

29+
@Autowired
30+
private PaymentService paymentService;
31+
2432
@Autowired
2533
private EventProducer eventProducer;
2634

35+
// public EventConsumer(ReceiverOptions<String, String> receiverOptions) {
36+
// KafkaReceiver.create(receiverOptions.subscription(Collections.singleton(KafkaConstant.PROFILE_ONBOARDING_TOPIC)))
37+
// .receive()
38+
// .subscribe(this::sendEmailKafkaOnboarding);
39+
// }
40+
41+
2742
public EventConsumer(ReceiverOptions<String, String> receiverOptions) {
28-
KafkaReceiver.create(receiverOptions.subscription(Collections.singleton(KafkaConstant.PROFILE_ONBOARDING_TOPIC)))
43+
subscribeToTopic(receiverOptions, KafkaConstant.PROFILE_ONBOARDING_TOPIC, this::sendEmailKafkaOnboarding);
44+
subscribeToTopic(receiverOptions, KafkaConstant.STATUS_PAYMENT_SUCCESSFUL, this::paymentOrderKafkaOnboarding);
45+
46+
}
47+
48+
private void subscribeToTopic(ReceiverOptions<String, String> receiverOptions, String topic, Consumer<ReceiverRecord<String, String>> handler) {
49+
KafkaReceiver.create(receiverOptions.subscription(Collections.singleton(topic)))
2950
.receive()
30-
.subscribe(this::sendEmailKafkaOnboarding);
51+
.subscribe(handler);
52+
log.info("Subscribed to Kafka topic: {}", topic);
3153
}
3254

55+
3356
public void sendEmailKafkaOnboarding(ReceiverRecord<String, String> receiverRecord) {
3457
log.info("USER-SERVICE Onboarding event send email on notification service.");
3558
EmailDetails emailDetails = gson.fromJson(receiverRecord.value(), EmailDetails.class);
@@ -39,4 +62,18 @@ public void sendEmailKafkaOnboarding(ReceiverRecord<String, String> receiverReco
3962
eventProducer.send(KafkaConstant.PROFILE_ONBOARDED_TOPIC, gson.toJson(email)).subscribe();
4063
});
4164
}
65+
66+
67+
public void paymentOrderKafkaOnboarding(ReceiverRecord<String, String> receiverRecord) {
68+
log.info("Profile Onboarding event");
69+
70+
PaymentDto paymentDto = gson.fromJson(receiverRecord.value(), PaymentDto.class);
71+
paymentService.savePayment(paymentDto).subscribe(res -> {
72+
73+
eventProducer.send(KafkaConstant.PROFILE_ONBOARDED_TOPIC, gson.toJson(paymentDto)).subscribe();
74+
});
75+
76+
}
77+
78+
4279
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hoangtien2k3.notificationservice.helper;
2+
3+
import com.hoangtien2k3.notificationservice.dto.PaymentDto;
4+
import com.hoangtien2k3.notificationservice.entity.Payment;
5+
6+
public interface PaymentMappingHelper {
7+
8+
static PaymentDto map(Payment payment) {
9+
if (payment == null) return null;
10+
return PaymentDto.builder()
11+
.paymentId(payment.getPaymentId())
12+
.isPayed(payment.getIsPayed())
13+
.paymentStatus(payment.getPaymentStatus())
14+
.orderId(payment.getOrderId())
15+
.userId(payment.getUserId())
16+
.build();
17+
}
18+
19+
static Payment map(PaymentDto paymentDto) {
20+
if (paymentDto == null) return null;
21+
return Payment.builder()
22+
.paymentId(paymentDto.getPaymentId())
23+
.isPayed(paymentDto.getIsPayed())
24+
.paymentStatus(paymentDto.getPaymentStatus())
25+
.orderId(paymentDto.getOrderId())
26+
.userId(paymentDto.getUserId())
27+
.build();
28+
}
29+
30+
}

0 commit comments

Comments
 (0)