Skip to content

Commit

Permalink
added ProductAvialabilityEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
DaoudAA committed Dec 4, 2024
1 parent c57d999 commit 1d00bc1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.ecommerce.domain.events;

import com.fasterxml.jackson.annotation.JsonProperty;

public class ProductAvailabilityEvent extends Event {
@JsonProperty("productId")
private String productId;

@JsonProperty("eventType")
private final String availability;

public ProductAvailabilityEvent(String productId, String availability) {
super("productsAvailability", "Product", productId);
this.productId = productId;
this.availability = availability;
}

@Override
public String toString() {
return "ProductAvailabilityEvent{" +
", eventType=" + getEventType() +
", aggregateType=" + getAggregateType() +
", aggregateId=" + getAggregateId() +
'}';
}

public String getAvailability() {
return availability;
}

public String getProductId() {
return productId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.ecommerce.domain.Product;
import org.ecommerce.domain.ProductCategory;
import org.ecommerce.domain.events.ProductAvailabilityEvent;
import org.ecommerce.dto.InventoryEvent;
import org.ecommerce.exceptions.EntityAlreadyExistsException;
import org.ecommerce.exceptions.EntityNotFoundException;
import org.ecommerce.repository.ProductRepository;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.UUID;

@ApplicationScoped
Expand All @@ -24,6 +28,8 @@ public class KafkaProductConsumer {
@Inject
ProductRepository productRepo;

private final ObjectMapper objectMapper = new ObjectMapper();

@Transactional
@Incoming("products-created")
public void handleInventoryEvent(String payload) {
Expand Down Expand Up @@ -109,4 +115,26 @@ private ProductCategory getOrCreateCategory(String categoryName) {
}
return category;
}

@Transactional
@Incoming("product-availability")
public void updateAvailabilityOfProduct(String payload) {
try {
ProductAvailabilityEvent event = objectMapper.readValue(payload, ProductAvailabilityEvent.class);
UUID productId = UUID.fromString(event.getAggregateId());
Product product = productService.getProductDetails(productId);

if (product != null) {
boolean isAvailable = "IN_STOCK".equals(event.getAvailability());
product.setDisponibility(isAvailable);
productService.updateProduct(product);
System.out.println("Product availability updated: " + product.getId() + " to " + isAvailable);
} else {
System.err.println("Product not found for availability update: " + productId);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to process product availability event: " + payload);
}
}
}

0 comments on commit 1d00bc1

Please sign in to comment.