Skip to content

Commit

Permalink
BP-001 Added the product package
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMachadoVasconcelos committed Oct 26, 2024
1 parent a59c0f9 commit 0ab344f
Show file tree
Hide file tree
Showing 41 changed files with 505 additions and 110 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

public class AggregateNotFoundException extends RuntimeException{
public AggregateNotFoundException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;


import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;


public interface CommandDispatcher {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;


@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

public class ConcurrencyException extends RuntimeException {
public ConcurrencyException(String errorMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
Expand All @@ -16,7 +16,6 @@
import java.util.Date;
import lombok.NoArgsConstructor;
import lombok.With;
import org.checkerframework.checker.units.qual.C;

@With
@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

public interface EventProducer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import java.util.List;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import java.util.List;
import org.springframework.data.repository.ListCrudRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.ead.payments.orders;
package com.ead.payments.eventsourcing;

import com.ead.payments.BaseCommand;
import com.ead.payments.CommandDispatcher;
import com.ead.payments.CommandHandlerMethod;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -15,7 +12,7 @@

@Service
@AllArgsConstructor
public class OrderCommandDispatcher implements CommandDispatcher {
public class InMemoryCommandDispatcher implements CommandDispatcher {

private final Map<Class<? extends BaseCommand>, List<CommandHandlerMethod>> routes = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ead.payments;
package com.ead.payments.eventsourcing;

import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ead.payments.inventory;

import com.ead.payments.orders.OrderPlacedEvent;
import com.ead.payments.products.ProductCreatedEvent;
import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand All @@ -15,6 +16,12 @@ class InventoryController {
@ApplicationModuleListener
void on (OrderPlacedEvent event) throws InterruptedException {
Thread.sleep(Duration.ofSeconds(5)); // Simulate inventory update
log.info("Inventory updated: {}", event.toString());
log.info("Reserving the Product. Inventory updated: {}", event.toString());
}

@ApplicationModuleListener
void on (ProductCreatedEvent event) throws InterruptedException {
Thread.sleep(Duration.ofSeconds(2)); // Simulate inventory update
log.info("New Product created. SKU registered: {}", event.toString());
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/ead/payments/orders/OrderAggregate.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.AggregateRoot;
import com.ead.payments.eventsourcing.AggregateRoot;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand All @@ -21,6 +21,13 @@ public class OrderAggregate extends AggregateRoot {
private Long amount;

public OrderAggregate(PlaceOrderCommand command) {
Preconditions.checkNotNull(command.getId(), "The id is required");
Preconditions.checkNotNull(command.getCurrency(), "The currency is required");
Preconditions.checkArgument(!command.getCurrency().isBlank(), "The currency is required");
Preconditions.checkArgument(command.getCurrency().length() == 3, "The currency must be in ISO 4217 format");
Preconditions.checkNotNull(command.getAmount(), "The amount is required");
Preconditions.checkArgument(command.getAmount() > 0, "The amount must be greater than 0");

raiseEvent(new OrderPlacedEvent(
command.getId(),
command.getCurrency(),
Expand All @@ -29,13 +36,6 @@ public OrderAggregate(PlaceOrderCommand command) {
}

public void apply(OrderPlacedEvent event) {
Preconditions.checkNotNull(event.getId(), "The id is required");
Preconditions.checkNotNull(event.getCurrency(), "The currency is required");
Preconditions.checkArgument(!event.getCurrency().isBlank(), "The currency is required");
Preconditions.checkArgument(event.getCurrency().length() == 3, "The currency must be in ISO 4217 format");
Preconditions.checkNotNull(event.getAmount(), "The amount is required");
Preconditions.checkArgument(event.getAmount() > 0, "The amount must be greater than 0");

this.id = event.getId();
this.currency = event.getCurrency();
this.amount = event.getAmount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.ead.payments.orders;

import com.ead.payments.AggregateRoot;
import com.ead.payments.EventSourcingHandler;
import com.ead.payments.eventsourcing.EventSourcingHandler;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class OrderCommandHandler implements CommandHandler {
public class OrderCommandHandler implements CommandHandler {

EventSourcingHandler<AggregateRoot> eventSourcingHandler;
EventSourcingHandler<OrderAggregate> eventSourcingHandler;

@Override
public void handler(PlaceOrderCommand command) {
var aggregate = new OrderAggregate(command);
eventSourcingHandler.save(aggregate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.CommandDispatcher;
import com.ead.payments.eventsourcing.CommandDispatcher;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
Expand All @@ -17,7 +17,7 @@ public class OrderCommandRegister {

@EventListener(ApplicationReadyEvent.class)
public void registerHandlers() {
log.debug("Registering the Account commands handlers to the Commander Dispatcher!");
log.debug("Registering the Order commands handlers to the Commander Dispatcher!");
commandDispatcher.registerHandler(PlaceOrderCommand.class, commandHandler::handler);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.ConcurrencyException;
import com.ead.payments.eventsourcing.ConcurrencyException;
import java.net.URI;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ead/payments/orders/OrderController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.CommandDispatcher;
import com.ead.payments.eventsourcing.CommandDispatcher;
import jakarta.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
Expand All @@ -18,13 +18,13 @@
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(path = "/v1/orders")
@RequestMapping(path = "/orders")
@RolesAllowed({"ROLE_MERCHANT", "ROLE_CUSTOMER"})
public class OrderController {

CommandDispatcher commandDispatcher;

@PostMapping
@PostMapping(headers = "version=1.0.0")
@ResponseStatus(HttpStatus.CREATED)
public PlaceOrderResponse placeOrder(@RequestBody @Valid @NotNull PlaceOrderRequest request) {
var orderId = UUID.randomUUID();
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/ead/payments/orders/OrderEventProducer.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ead.payments.orders;

import com.ead.payments.AggregateRoot;
import com.ead.payments.BaseEvent;
import com.ead.payments.EventSourcingHandler;
import com.ead.payments.EventStore;
import com.ead.payments.eventsourcing.AggregateRoot;
import com.ead.payments.eventsourcing.BaseEvent;
import com.ead.payments.eventsourcing.EventSourcingHandler;
import com.ead.payments.eventsourcing.EventStore;
import java.util.UUID;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -12,12 +12,12 @@

@Service
@AllArgsConstructor
public class OrderEventSourcingHandler implements EventSourcingHandler<AggregateRoot> {
public class OrderEventSourcingHandler implements EventSourcingHandler<OrderAggregate> {

EventStore eventStore;

@Override
public void save(AggregateRoot aggregate) {
public void save(OrderAggregate aggregate) {
eventStore.saveEvents(aggregate.getId(), aggregate.getUncommittedChanges(), aggregate.getVersion());
aggregate.markChangesAsCommitted();
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/ead/payments/orders/OrderEventStore.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.ead.payments.orders;

import com.ead.payments.AggregateNotFoundException;
import com.ead.payments.BaseEvent;
import com.ead.payments.ConcurrencyException;
import com.ead.payments.EventModel;
import com.ead.payments.EventStore;
import com.ead.payments.EventStoreRepository;
import com.ead.payments.eventsourcing.AggregateNotFoundException;
import com.ead.payments.eventsourcing.ConcurrencyException;
import com.ead.payments.eventsourcing.BaseEvent;
import com.ead.payments.eventsourcing.EventModel;
import com.ead.payments.eventsourcing.EventStore;
import com.ead.payments.eventsourcing.EventStoreRepository;
import jakarta.transaction.Transactional;
import java.util.UUID;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
Expand All @@ -25,7 +26,7 @@
public class OrderEventStore implements EventStore {

private EventStoreRepository repository;
private OrderEventProducer producer;
private ApplicationEventPublisher producer;

@Override
public void saveEvents(UUID aggregatedId, Iterable<BaseEvent> events, int expectedVersion) {
Expand Down Expand Up @@ -57,7 +58,7 @@ public void saveEvents(UUID aggregatedId, Iterable<BaseEvent> events, int expect

var persistedEvent = repository.save(eventModel);
if (!persistedEvent.getId().isEmpty()) {
producer.producer(event.getClass().getSimpleName(), event);
producer.publishEvent(event);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.BaseEvent;
import com.ead.payments.eventsourcing.BaseEvent;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ead.payments.orders;

import com.ead.payments.BaseCommand;
import com.ead.payments.eventsourcing.BaseCommand;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ead/payments/products/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ead.payments.products;

public interface CommandHandler {
void handler(CreateProductCommand command);
}
Loading

0 comments on commit 0ab344f

Please sign in to comment.