Skip to content

Commit

Permalink
Final Version
Browse files Browse the repository at this point in the history
  • Loading branch information
FadiFadhlaoui1212 committed Dec 30, 2024
1 parent 95c2fcb commit 7a90f2b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.microservices.order_service.domain;

public enum OrderStatus {
CREATED, AVAILABLE, PAID, DELIVERED, CANCELED;
CREATED, AVAILABLE, PAID, CANCELED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
public class DeliveryStatusMessage {
private UUID orderId;
private DeliveryStatus status;
private UUID cartId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.microservices.order_service.domain.PaymentStatus;
import com.microservices.order_service.dto.*;
import com.microservices.order_service.model.Client;
import com.microservices.order_service.model.Item;
import com.microservices.order_service.model.Order;
import com.microservices.order_service.outbox.OrderEventOutbox;
Expand All @@ -19,10 +20,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,6 +57,9 @@ public class CartConsumer {
@Autowired
DeliveryService deliveryService;

@Autowired
ClientRepository clientRepository;

@KafkaListener(topics = "cart-topic", groupId = "cartReceiver", containerFactory = "CartListenerContainerFactory")
public void listen(CartDTO cartDTO) {

Expand Down Expand Up @@ -103,6 +104,10 @@ public void listen(CartDTO cartDTO) {
orderEventDTO.setItems(items);
orderEventOutbox.setProcessed(false);
try{
Client client = new Client();
clientRepository.save(client);

order.setClient(client);
orderRepository.save(order);
orderEventOutbox.setOrderId(order.getId());
orderEventDTO.setOrderId(order.getId());
Expand All @@ -114,17 +119,22 @@ public void listen(CartDTO cartDTO) {
availabilityCheckDTO.setOrderId(order.getId());
availabilityCheckDTO.setItems(items);

Map<String, Object> AvailabilityCheckResponse = inventoryService.checkOrderAvailability(availabilityCheckDTO);
Map<String, Object> AvailabilityCheckResponse = new HashMap<>();
AvailabilityCheckResponse.putIfAbsent("status", "OK");

/*AvailabilityCheckResponse= inventoryService.checkOrderAvailability(availabilityCheckDTO); */ // this instruction to verify whether the cart items are available in inventory or not
if ("OK".equals(AvailabilityCheckResponse.get("status"))){
orderEventOutboxRepository.save(orderEventOutbox);

}
else {
OrderStatusOutbox orderStatusOutbox = new OrderStatusOutbox();
orderStatusOutbox.setOrderId(order.getId());
orderStatusOutbox.setStatus(CANCELED);
orderStatusOutbox.setProcessed(false);
orderStatusOutboxRepository.save(orderStatusOutbox);
orderRepository.delete(order);
order.setOrderStatus(CANCELED);
orderRepository.save(order);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.microservices.order_service.kafka;

import com.microservices.order_service.domain.DeliveryStatus;
import com.microservices.order_service.domain.OrderStatus;
import com.microservices.order_service.dto.DeliveryStatusMessage;
import com.microservices.order_service.events.OrderPaidEvent;
import com.microservices.order_service.model.Order;
import com.microservices.order_service.outbox.OrderPaidOutbox;
import com.microservices.order_service.outbox.OrderStatusOutbox;
import com.microservices.order_service.repository.OrderPaidOutboxRepository;
import com.microservices.order_service.repository.OrderRepository;
import com.microservices.order_service.repository.OrderStatusOutboxRepository;
import com.microservices.order_service.service.OrderService;
import com.microservices.order_service.service.OrderStatusOutboxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.annotation.KafkaListener;
Expand All @@ -14,18 +25,24 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import static com.microservices.order_service.domain.OrderStatus.CANCELED;

@Component
public class DeliveryEventConsumer {

private static final Logger logger = LoggerFactory.getLogger(DeliveryEventConsumer.class);

private final RestTemplate restTemplate;

@Value("${delivery.service.url}")
private String deliveryServiceUrl;
@Autowired
private OrderStatusOutboxRepository orderStatusOutboxRepository;

@Autowired
private OrderRepository orderRepository;

@Autowired
private OrderService orderService;

@Value("${shipping.service.url}")
private String shippingServiceUrl;



Expand All @@ -44,6 +61,19 @@ public void consume(@Payload DeliveryStatusMessage deliveryStatusMessage,
@Header(KafkaHeaders.OFFSET) long offset) {
logger.info("Received deliveryStatusMessage: {}", deliveryStatusMessage.getOrderId());
logger.info("Received deliveryStatusMessage: {}", deliveryStatusMessage.getStatus());
if(deliveryStatusMessage.getStatus()== DeliveryStatus.CANCELLED || deliveryStatusMessage.getStatus() == DeliveryStatus.RETURNED ){
OrderStatusOutbox orderStatusOutbox = new OrderStatusOutbox();
orderStatusOutbox.setOrderId(deliveryStatusMessage.getOrderId());
orderStatusOutbox.setStatus(CANCELED);
orderStatusOutbox.setProcessed(false);
orderStatusOutboxRepository.save(orderStatusOutbox);

Order order = orderRepository.findById(deliveryStatusMessage.getOrderId()).get();
order.setOrderStatus(CANCELED);

orderService.updateOrder(deliveryStatusMessage.getOrderId(), order);
}



}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Order {
private BigDecimal price; // Price field

@ManyToOne
@JoinColumn(name = "idClient", referencedColumnName = "idClient")
@JoinColumn(name = "idClient", referencedColumnName = "idClient", nullable = false)
private Client client; // Reference to the Client entity

@Column(name = "quantity")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class OrderEventOutboxService {
@Autowired
private OrderEventProducer orderEventProducer;

@Scheduled(fixedRate = 60000)
@Scheduled(fixedRate = 30000)
public void processOrderEventOutbox() {

List<OrderEventOutbox> events = orderEventOutboxRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OrderPaidOutboxService {
@Autowired
private OrderPaidEventProducer orderPaidEventProducer;

@Scheduled(fixedRate = 60000)
@Scheduled(fixedRate = 30000)
public void processOrderPaidOutbox() {

List<OrderPaidOutbox> events = orderPaidOutboxRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OrderStatusOutboxService {
@Autowired
private OrderStatusUpdateProducer orderStatusUpdateProducer;

@Scheduled(fixedRate = 60000)
@Scheduled(fixedRate = 30000)
public void processOrderStatusOutbox() {

List<OrderStatusOutbox> events = orderStatusOutboxRepository.findAll();
Expand Down

0 comments on commit 7a90f2b

Please sign in to comment.