diff --git a/cart/src/main/java/org/soa/dto/PriceEvent.java b/cart/src/main/java/org/soa/dto/PriceEvent.java new file mode 100644 index 00000000..2ed27d5a --- /dev/null +++ b/cart/src/main/java/org/soa/dto/PriceEvent.java @@ -0,0 +1,32 @@ +package org.soa.dto; + +public class PriceEvent { + private String productId; + private double price; + + // Getters et setters + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + @Override + public String toString() { + return "PriceEvent{" + + "productId='" + productId + '\'' + + ", price=" + price + + '}'; + } +} + diff --git a/cart/src/main/java/org/soa/messaging/PricingEventConsumer.java b/cart/src/main/java/org/soa/messaging/PricingEventConsumer.java new file mode 100644 index 00000000..d61bc2cc --- /dev/null +++ b/cart/src/main/java/org/soa/messaging/PricingEventConsumer.java @@ -0,0 +1,31 @@ +package org.soa.messaging; + +import org.eclipse.microprofile.reactive.messaging.Incoming; +import jakarta.enterprise.context.ApplicationScoped; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.soa.dto.PriceEvent; + +@ApplicationScoped +public class PricingEventConsumer { + + private final Map productPrices = new HashMap<>(); // Utilisation de UUID comme clé + + @Incoming("pricing-price-channel") // Le canal Kafka configuré dans application.properties + public void consumePriceEvent(PriceEvent priceEvent) { + // Enregistrer le prix de l'article avec l'UUID comme clé + UUID productId = UUID.fromString(priceEvent.getProductId()); // Conversion de String en UUID + productPrices.put(productId, priceEvent.getPrice()); + System.out.println("Prix reçu pour l'article : " + priceEvent); + } + + public double getPrice(UUID productId) { + return productPrices.getOrDefault(productId, -1.0); // Retourne -1.0 si le produit n'est pas trouvé + } + + public Map getAllPrices() { + return productPrices; + } +} diff --git a/cart/src/main/java/org/soa/model/Cart.java b/cart/src/main/java/org/soa/model/Cart.java index 17c018b6..daba189c 100644 --- a/cart/src/main/java/org/soa/model/Cart.java +++ b/cart/src/main/java/org/soa/model/Cart.java @@ -31,4 +31,10 @@ public Map getItems() { public void setItems(Map items) { this.items = items; } + + public double calculateTotalPrice() { + return items.values().stream() + .mapToDouble(Item::getTotalPrice) + .sum(); + } } diff --git a/cart/src/main/java/org/soa/serialization/CartDTOSerializer.java b/cart/src/main/java/org/soa/serialization/CartDTOSerializer.java new file mode 100644 index 00000000..9dd137ca --- /dev/null +++ b/cart/src/main/java/org/soa/serialization/CartDTOSerializer.java @@ -0,0 +1,15 @@ +package org.soa.serialization; + +import io.quarkus.kafka.client.serialization.ObjectMapperSerializer; +import org.soa.dto.CartDTO; + +public class CartDTOSerializer extends ObjectMapperSerializer { + + /** + * Constructor for CartDTOSerializer. + * This ensures the ObjectMapper provided by Quarkus is used to serialize CartDTO objects. + */ + public CartDTOSerializer() { + super(); + } +} diff --git a/cart/src/main/java/org/soa/serialization/PriceEventDeserializer.java b/cart/src/main/java/org/soa/serialization/PriceEventDeserializer.java new file mode 100644 index 00000000..a0ba335f --- /dev/null +++ b/cart/src/main/java/org/soa/serialization/PriceEventDeserializer.java @@ -0,0 +1,31 @@ +package org.soa.serialization; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.kafka.common.serialization.Deserializer; + +import java.util.Map; +import org.soa.dto.PriceEvent; + + +public class PriceEventDeserializer implements Deserializer { + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public void configure(Map configs, boolean isKey) { + // Configuration si nécessaire + } + + @Override + public PriceEvent deserialize(String topic, byte[] data) { + try { + return objectMapper.readValue(data, PriceEvent.class); + } catch (Exception e) { + throw new RuntimeException("Erreur lors de la désérialisation de l'événement de prix", e); + } + } + + @Override + public void close() { + // Nettoyage si nécessaire + } +} diff --git a/cart/src/main/java/org/soa/service/CartService.java b/cart/src/main/java/org/soa/service/CartService.java index d12bbfb3..b8c21251 100644 --- a/cart/src/main/java/org/soa/service/CartService.java +++ b/cart/src/main/java/org/soa/service/CartService.java @@ -10,6 +10,8 @@ import org.soa.model.Cart; import org.soa.model.Item; import org.soa.messaging.CartPublisher; +import org.soa.messaging.PricingEventConsumer; +import java.util.Map; import java.util.List; import java.util.UUID; @@ -65,11 +67,13 @@ public Cart getCart(UUID userId) { throw new RuntimeException("An unexpected error occurred while fetching the cart.", e); } } - - // Ajouter un item au panier + @Inject + PricingEventConsumer PricingEventConsumer; public void addItem(UUID userId, Item item) { try { logger.info("Adding item to cart for userId: " + userId); + + // Récupérer la carte des paniers RMap carts = getCartsMap(); Cart cart = carts.get(userId); @@ -78,6 +82,21 @@ public void addItem(UUID userId, Item item) { throw new IllegalArgumentException("Cart not found for userId: " + userId); } + // Récupérer tous les prix depuis PricingEventConsumer + Map allPrices = PricingEventConsumer.getAllPrices(); + + // Récupérer le prix de l'article à partir de la map des prix + Double price = allPrices.get(item.getItemId()); + + if (price == null) { + logger.error("Price not found for itemId: " + item.getItemId()); + throw new IllegalArgumentException("Price not found for itemId: " + item.getItemId()); + } + + // Mettre à jour l'article avec le prix récupéré + item.setPrice(price); + + // Ajouter l'article au panier, en mettant à jour la quantité si l'article existe déjà cart.getItems().compute(item.getItemId(), (key, existingItem) -> { if (existingItem != null) { existingItem.setQuantity(existingItem.getQuantity() + item.getQuantity()); @@ -86,6 +105,7 @@ public void addItem(UUID userId, Item item) { return item; }); + // Mettre à jour le panier dans la carte carts.put(userId, cart); logger.info("Item added successfully to cart for userId: " + userId); } catch (Exception e) { @@ -94,6 +114,7 @@ public void addItem(UUID userId, Item item) { } } + // Mettre à jour un item dans le panier public void updateItem(UUID userId, Item cartItem) { try { diff --git a/cart/src/main/resources/application.properties b/cart/src/main/resources/application.properties index d6d33a44..807178b4 100644 --- a/cart/src/main/resources/application.properties +++ b/cart/src/main/resources/application.properties @@ -4,11 +4,16 @@ quarkus.redis.hosts=redis://localhost:6379 mp.messaging.outgoing.cart-topic.connector=smallrye-kafka mp.messaging.outgoing.cart-topic.topic=cart-topic -mp.messaging.outgoing.cart-topic.value.serializer=org.soa.serialization.CartEventSerializer +#mp.messaging.outgoing.cart-topic.value.serializer=org.soa.serialization.CartEventSerializer +mp.messaging.outgoing.cart-topic.value.serializer=org.soa.serialization.CartDTOSerializer mp.messaging.outgoing.cart-topic.bootstrap.servers=localhost:9093 quarkus.kafka.devservices.enabled=false # Kafka broker address +mp.messaging.incoming.pricing-price-channel.connector=smallrye-kafka +mp.messaging.incoming.pricing-price-channel.topic=pricing-prices +mp.messaging.incoming.pricing-price-channel.value.deserializer=org.soa.serialization.StringDeserializer +