Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeineb2001 committed Dec 25, 2024
2 parents a4ca1d2 + d786224 commit 65fb686
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 62 deletions.
50 changes: 25 additions & 25 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,31 @@ services:
# MONGO_INITDB_ROOT_PASSWORD: azerty
# ports:
# - 27017:27017
# zookeeper:
# 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: 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
zookeeper:
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: 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
# iam:
# image: quay.io/keycloak/keycloak:20.0.2
# environment:
Expand Down
13 changes: 5 additions & 8 deletions shipping/src/main/java/org/shipping/dto/OrderPaidEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

public class OrderPaidEvent {
private UUID orderId;
private UUID cartId;
private UUID addressId;

// Constructeurs, getters et setters
public OrderPaidEvent() {
}

public UUID getCartId() {
return cartId;
}

public OrderPaidEvent(UUID orderId, UUID addressId) {
this.orderId = orderId;
this.addressId = addressId;
Expand All @@ -19,15 +24,7 @@ public UUID getOrderId() {
return orderId;
}

public void setOrderId(UUID orderId) {
this.orderId = orderId;
}

public UUID getAddressId() {
return addressId;
}

public void setAddressId(UUID addressId) {
this.addressId = addressId;
}
}
91 changes: 63 additions & 28 deletions shipping/src/main/java/org/shipping/service/AddressService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public class AddressService {
@Inject
AddressRepository addressRepository;

// @Inject
// SecurityService securityService;

@Inject
SecurityService securityService;
AddressEventPublisher eventPublisher;

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

Expand Down Expand Up @@ -52,7 +55,47 @@ public Address getAddressById(UUID addressId) {
}
}


// Ajouter une adresse pour un utilisateur
@Transactional
public Address addAddress(String street, String postalCode, String city, String country) {
try {
// Validation des champs
if (street == null || postalCode == null || city == null || country == null) {
throw new IllegalArgumentException(
"All address fields (street, postal code, city, country) must be provided.");
}

// UUID utilisateur simulé pour le moment
UUID userId = UUID.fromString("faa1b47d-27e3-4106-b42b-2d1e7d1f6e93");

// Vérifier si l'adresse existe déjà pour l'utilisateur
boolean addressExists = addressRepository
.find("userId = ?1 AND street = ?2 AND postalCode = ?3 AND city = ?4 AND country = ?5",
userId, street, postalCode, city, country)
.firstResult() != null;

if (addressExists) {
throw new IllegalStateException("The address already exists for the user.");
}

// Créer et persister l'adresse
Address address = AddressFactory.createAddress(userId, street, postalCode, city, country);
addressRepository.persist(address);
eventPublisher.publishAddressAddedEvent(address); // Publier l'événement
logger.info("Address added successfully for userId: " + userId);
return address;

} catch (IllegalArgumentException e) {
logger.error("Invalid input: " + e.getMessage(), e);
throw new IllegalArgumentException("Invalid input: " + e.getMessage());
} catch (IllegalStateException e) {
logger.warn("Address already exists: " + e.getMessage());
throw new IllegalStateException(e.getMessage());
} catch (Exception e) {
logger.error("Unexpected error occurred while adding the address.", e);
throw new RuntimeException("An unexpected error occurred while adding the address.");
}
}

// Trouver les adresses d'un utilisateur
public List<Address> getAddressesByUserId() {
Expand Down Expand Up @@ -81,8 +124,6 @@ public List<Address> getAddressesByUserId() {
throw new RuntimeException("An unexpected error occurred while fetching user addresses.");
}
}



// Mettre à jour une adresse
@Transactional
Expand Down Expand Up @@ -123,29 +164,23 @@ public Address updateAddress(UUID addressId, String street, String postalCode, S
}
}



@Inject
AddressEventPublisher eventPublisher;

@Transactional
public Address addAddress(String street, String postalCode, String city, String country) {
UUID userId = UUID.fromString("faa1b47d-27e3-4106-b42b-2d1e7d1f6e93");
Address address = AddressFactory.createAddress(userId, street, postalCode, city, country);
addressRepository.persist(address);
eventPublisher.publishAddressAddedEvent(address); // Publier l'événement
return address;
}

@Transactional
public boolean deleteAddress(UUID addressId) {
Address address = getAddressById(addressId);
if (addressRepository.isPersistent(address)) {
addressRepository.delete(address);
eventPublisher.publishAddressDeletedEvent(addressId); // Publier l'événement
return true;
// Supprimer une adresse
@Transactional
public void deleteAddress(UUID addressId) {
try {
Address address = getAddressById(addressId);
if (addressRepository.isPersistent(address)) {
addressRepository.delete(address);
eventPublisher.publishAddressDeletedEvent(addressId); // Publier l'événement
logger.info("Address with ID " + addressId + " deleted successfully.");
return;
}
} catch (NoResultException e) {
logger.error("Address with ID " + addressId + " is not persistent, cannot delete.");
throw new NoResultException("Address not found during deleting: " + e.getMessage());
} catch (Exception e) {
logger.error("Unexpected error occurred while deleting the address.", e);
throw new RuntimeException("An unexpected error occurred while deleting the address.");
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Shipment createShipment(UUID orderId, UUID addressId) {
// Créer et persister la livraison
Shipment newShipment = new Shipment(orderId, address);
shipmentRepository.persist(newShipment);
statusPublisher.publishStatus(orderId, DeliveryStatus.PENDING); // Publier le statut de la livraison
logger.info("Shipment created successfully: " + newShipment.getShipmentId());
return newShipment;

Expand Down
5 changes: 4 additions & 1 deletion shipping/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
quarkus.http.host=0.0.0.0
quarkus.http.port=8090
quarkus.http.cors=true
quarkus.http.cors.origins=*

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
Expand All @@ -19,8 +22,8 @@ quarkus.http.limits.max-form-attribute-size=4M
# mp.jwt.verify.issuer=https://example.com/iam
# mp.jwt.token.header=Authorization

# S'abonner à un Broker Kafka pour l'event "order-paid"
quarkus.kafka.devservices.enabled=false
# S'abonner à un Broker Kafka pour l'event "order-paid"
mp.messaging.incoming.order-paid.connector=smallrye-kafka
mp.messaging.incoming.order-paid.topic=order-paid
mp.messaging.incoming.order-paid.group.id=shipping-group
Expand Down

0 comments on commit 65fb686

Please sign in to comment.