diff --git a/cart/docker-compose.yml b/cart/docker-compose.yml index b58f82d2..fc75f7cc 100644 --- a/cart/docker-compose.yml +++ b/cart/docker-compose.yml @@ -1,18 +1,27 @@ version: '3.8' services: zookeeper: - image: confluentinc/cp-zookeeper:latest - environment: - ZOOKEEPER_CLIENT_PORT: 2181 - + image: strimzi/kafka:0.19.0-kafka-2.5.0 + command: [ + "sh", "-c", + "bin/zookeeper-server-start.sh config/zookeeper.properties" + ] + ports: + - "2181:2181" + environment: + LOG_DIR: /tmp/logs kafka: - image: confluentinc/cp-kafka:latest - ports: - - "9092:9092" - environment: - KAFKA_BROKER_ID: 1 - KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 - KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092 - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 + image: strimzi/kafka:0.19.0-kafka-2.5.0 + command: [ + "sh", "-c", + "bin/kafka-server-start.sh config/server.properties --override listeners=$${KAFKA_LISTENERS} --override advertised.listeners=$${KAFKA_ADVERTISED_LISTENERS} --override zookeeper.connect=$${KAFKA_ZOOKEEPER_CONNECT}" + ] depends_on: - zookeeper + ports: + - "9093:9093" + environment: + LOG_DIR: "/tmp/logs" + KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9093 + KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093 + KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 \ No newline at end of file diff --git a/cart/pom.xml b/cart/pom.xml index d4f2c404..b0de9699 100644 --- a/cart/pom.xml +++ b/cart/pom.xml @@ -46,6 +46,10 @@ io.quarkus quarkus-rest-jackson + + io.quarkus + quarkus-redis-client + io.quarkus quarkus-junit5 @@ -72,11 +76,11 @@ com.fasterxml.jackson.core jackson-databind - + io.quarkus quarkus-smallrye-jwt - + io.quarkus quarkus-messaging-kafka diff --git a/cart/src/main/java/org/soa/GreetingResource.java b/cart/src/main/java/org/soa/GreetingResource.java deleted file mode 100644 index e546d34d..00000000 --- a/cart/src/main/java/org/soa/GreetingResource.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.soa; - -import jakarta.ws.rs.GET; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.core.MediaType; - -@Path("/hello") -public class GreetingResource { - - @GET - @Produces(MediaType.TEXT_PLAIN) - public String hello() { - return "Hello from Quarkus REST"; - } -} diff --git a/cart/src/main/java/org/soa/api/CartResource.java b/cart/src/main/java/org/soa/api/CartResource.java index d7880f8e..7e00929a 100644 --- a/cart/src/main/java/org/soa/api/CartResource.java +++ b/cart/src/main/java/org/soa/api/CartResource.java @@ -133,6 +133,13 @@ public Response removeItem(@PathParam("userId") UUID userId, @PathParam("product } } + @POST + @Path("/{userId}/validate") + public Response validateCart(@PathParam("userId") UUID userId) { + cartService.validateCart(userId); + return Response.status(Status.OK).entity("Enfin, publié!!!!").build(); + } + // Vider le panier d'un utilisateur //@DELETE //@Path("/{userId}/clear") diff --git a/cart/src/main/java/org/soa/dto/ItemDTO.java b/cart/src/main/java/org/soa/dto/ItemDTO.java index 50d76782..ac7cc01c 100644 --- a/cart/src/main/java/org/soa/dto/ItemDTO.java +++ b/cart/src/main/java/org/soa/dto/ItemDTO.java @@ -6,17 +6,19 @@ public class ItemDTO { private UUID itemId; // Identifiant de l'item private String name; // Nom de l'item - private int quantity; // Quantité de l'item + private int quantity; + private double price; // Quantité de l'item // Constructeur sans paramètres public ItemDTO() { } // Constructeur avec paramètres - public ItemDTO(UUID itemId, String name, int quantity) { + public ItemDTO(UUID itemId, String name, int quantity, double price) { this.itemId = itemId; this.name = name; this.quantity = quantity; + this.price = price; } public UUID getItemId() { @@ -42,4 +44,14 @@ public int getQuantity() { public void setQuantity(int quantity) { this.quantity = quantity; } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + } diff --git a/cart/src/main/java/org/soa/service/CartService.java b/cart/src/main/java/org/soa/service/CartService.java index bb72ffe8..d12bbfb3 100644 --- a/cart/src/main/java/org/soa/service/CartService.java +++ b/cart/src/main/java/org/soa/service/CartService.java @@ -6,8 +6,10 @@ import org.jboss.logging.Logger; import org.redisson.api.RMap; import org.redisson.api.RedissonClient; +import org.soa.dto.ItemDTO; import org.soa.model.Cart; import org.soa.model.Item; +import org.soa.messaging.CartPublisher; import java.util.List; import java.util.UUID; @@ -25,7 +27,6 @@ private RMap getCartsMap() { } // Créer un nouveau panier - @Transactional public Cart createCart(UUID userId) { try { logger.info("Creating cart for userId: " + userId); @@ -66,7 +67,6 @@ public Cart getCart(UUID userId) { } // Ajouter un item au panier - @Transactional public void addItem(UUID userId, Item item) { try { logger.info("Adding item to cart for userId: " + userId); @@ -95,7 +95,6 @@ public void addItem(UUID userId, Item item) { } // Mettre à jour un item dans le panier - @Transactional public void updateItem(UUID userId, Item cartItem) { try { logger.info("Updating item in cart for userId: " + userId); @@ -122,7 +121,6 @@ public void updateItem(UUID userId, Item cartItem) { } // Supprimer un item du panier - @Transactional public void removeItem(UUID userId, UUID itemId) { try { logger.info("Removing item from cart for userId: " + userId); @@ -168,7 +166,6 @@ public List getCartItems(UUID userId) { } // Supprimer le panier - @Transactional public void deleteCart(UUID userId) { try { logger.info("Deleting cart for userId: " + userId); @@ -186,4 +183,36 @@ public void deleteCart(UUID userId) { throw new RuntimeException("An unexpected error occurred while deleting the cart.", e); } } + + @Inject + CartPublisher cartPublisher; + + public void validateCart(UUID cartId) { + // Récupérer le panier à partir de son ID + Cart cart = getCart(cartId); + if (cart == null) { + throw new IllegalArgumentException("Cart not found with ID: " + cartId); + } + + // Vérifier si le panier est vide + if (cart.getItems() == null || cart.getItems().isEmpty()) { + throw new IllegalArgumentException("Cannot validate an empty cart."); + } + + // Convertir les items du panier en objets ItemDTO + List itemDTOs = cart.getItems().entrySet().stream() + .map(entry -> new ItemDTO(entry.getKey(), entry.getValue().getName(), + entry.getValue().getQuantity(), entry.getValue().getPrice())) + .toList(); + + // Publier le panier dans le broker + cartPublisher.publishCart(cartId, itemDTOs); + + // Optionnel : journaliser l'action + logger.info("Cart with ID: " + cartId + " successfully published to the broker."); + } + + + + } diff --git a/cart/src/main/resources/application.properties b/cart/src/main/resources/application.properties index 8c32a60b..d6d33a44 100644 --- a/cart/src/main/resources/application.properties +++ b/cart/src/main/resources/application.properties @@ -4,10 +4,11 @@ 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.apache.kafka.common.serialization.JsonSerializer +mp.messaging.outgoing.cart-topic.value.serializer=org.soa.serialization.CartEventSerializer +mp.messaging.outgoing.cart-topic.bootstrap.servers=localhost:9093 +quarkus.kafka.devservices.enabled=false # Kafka broker address -kafka.bootstrap.servers=localhost:9092 diff --git a/cart/src/test/java/org/soa/GreetingResourceIT.java b/cart/src/test/java/org/soa/GreetingResourceIT.java deleted file mode 100644 index c51ed33c..00000000 --- a/cart/src/test/java/org/soa/GreetingResourceIT.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.soa; - -import io.quarkus.test.junit.QuarkusIntegrationTest; - -@QuarkusIntegrationTest -class GreetingResourceIT extends GreetingResourceTest { - // Execute the same tests but in packaged mode. -} diff --git a/cart/src/test/java/org/soa/GreetingResourceTest.java b/cart/src/test/java/org/soa/GreetingResourceTest.java deleted file mode 100644 index 02c9f0ef..00000000 --- a/cart/src/test/java/org/soa/GreetingResourceTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.soa; - -import io.quarkus.test.junit.QuarkusTest; -import org.junit.jupiter.api.Test; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.CoreMatchers.is; - -@QuarkusTest -class GreetingResourceTest { - @Test - void testHelloEndpoint() { - given() - .when().get("/hello") - .then() - .statusCode(200) - .body(is("Hello from Quarkus REST")); - } - -} \ No newline at end of file