Skip to content

Commit

Permalink
Merged changes of last push mahdiJ2001
Browse files Browse the repository at this point in the history
  • Loading branch information
DaoudAA committed Dec 23, 2024
2 parents a9249d9 + 3a1ffbc commit ebf745d
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 53 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
package org.ecommerce.domain.events;


public class InventoryEvent {

private String eventType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class InventoryEvent extends Event {
private String productId;
private String name;
private String description;
private String category;
private boolean disponibility;


public String getEventType() {
return eventType;
}

public String getProductId() {
return productId;
}

public String getDescription() {
return description;
}

public String getCategory() {
return category;
}

public boolean isDisponibility() {
return disponibility;
}

public String getName() {
return name;
public InventoryEvent(String aggregateType, String aggregateId, String productId, String name, String description, String category, boolean disponibility) {
super("InventoryEvent", aggregateType, aggregateId);
this.productId = productId;
this.name = name;
this.description = description;
this.category = category;
this.disponibility = disponibility;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ProductListed extends Event {
public ProductListed(Product product) {
super("ProductListed", "Product", product.getId().toString());
this.productName = product.getProductName();
this.categoryName = product.getCategory().getCategoryName();
this.categoryName = product.getCategory().getCategoryName();
this.description = product.getDescription();
this.price = product.getShownPrice();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,64 @@ public class ProductService {

@Inject
ProductRepository productRepo;

@Inject
ProductCategoryService categoryService;

@RestClient
PricingService pricingService;

@Inject
OutboxService outboxService;

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

@Transactional
public List<Product> findByRange(int page, int maxResults) {
return productRepo.findByRange(page, maxResults);
List<Product> products = productRepo.findByRange(page, maxResults);
products.forEach(product -> {
BigDecimal price = pricingService.getProductPrice(product.getId());
product.setShownPrice(price);
});
return products;
}

@Transactional
public List<Product> findAll() {
return productRepo.findAll();
List<Product> products = productRepo.findAll();
products.forEach(product -> {
BigDecimal price = pricingService.getProductPrice(product.getId());
product.setShownPrice(price);
});
return products;
}

@Transactional
public Product getProductDetails(UUID id) throws EntityNotFoundException {
return productRepo.findById(id);
Product product = productRepo.findById(id);
BigDecimal price = pricingService.getProductPrice(product.getId());
product.setShownPrice(price);
return product;
}
@Transactional
public Product add(Product product, String categoryName) throws EntityAlreadyExistsException, EntityNotFoundException {

product.setId(UUID.randomUUID());
BigDecimal temp = new BigDecimal("10506");
product.setBasePrice(temp);
product.setShownPrice(product.getBasePrice());


BigDecimal price = pricingService.getProductPrice(product.getId());
product.setBasePrice(price);
product.setShownPrice(price);

ProductCategory category = categoryService.getCategoryByName(categoryName);
product.setCategory(category);
ProductListed productListed = new ProductListed(product);

ProductListed productListed = new ProductListed(product);

try {
outboxService.createOutboxMessage(productListed);
}catch (Exception e) {
} catch (Exception e) {
logger.error("Failed to create outbox message for product: " + product.getProductName());
throw new RuntimeException("Failed to create outbox message"); // Handle accordingly
throw new RuntimeException("Failed to create outbox message");
}

return productRepo.insert(product);
Expand All @@ -77,37 +93,35 @@ public Product add(Product product, String categoryName) throws EntityAlreadyExi
@Transactional
public Product updateProduct(Product product) throws EntityNotFoundException {
Product existingProduct = productRepo.findById(product.getId());

if (product.getCategory() != null) {
ProductCategory newCategory = categoryService.getCategoryByName(product.getCategory().getCategoryName());
if (newCategory != null) {
existingProduct.setCategory(newCategory);
} else {
throw new EntityNotFoundException("Category not found: " + product.getCategory().getCategoryName());
}
ProductCategory newCategory = categoryService.getCategoryByName(product.getCategory().getCategoryName());
if (newCategory != null) {
existingProduct.setCategory(newCategory);
} else {
throw new EntityNotFoundException("Category not found: " + product.getCategory().getCategoryName());
}
}
if (product.getProductName() != null) {
existingProduct.setProductName(product.getProductName());
existingProduct.setProductName(product.getProductName());
}
if (product.getDescription() != null) {
existingProduct.setDescription(product.getDescription());
existingProduct.setDescription(product.getDescription());
}
if (product.getShownPrice() != null) {
existingProduct.setShownPrice(product.getShownPrice());
existingProduct.setShownPrice(product.getShownPrice());
}
existingProduct.setDisponibility(product.isDisponibility());

ProductUpdated productUpdated = new ProductUpdated(existingProduct);
try {
outboxService.createOutboxMessage(productUpdated);
} catch (JsonProcessingException e) {
e.printStackTrace();
logger.error("Failed to create outbox message for product: " + product.getProductName());
throw new RuntimeException("Failed to create outbox message"); // Handle accordingly
throw new RuntimeException("Failed to create outbox message");
} catch (EntityAlreadyExistsException e) {
e.printStackTrace();
logger.error("Failed to create outbox message for product: " + product.getProductName());
throw new RuntimeException("Failed to create outbox message"); // Handle accordingly
throw new RuntimeException("Failed to create outbox message");
}

return productRepo.update(existingProduct);
Expand Down

0 comments on commit ebf745d

Please sign in to comment.