diff --git a/common/src/main/java/com/nashtech/common/command/ApproveOrderCommand.java b/common/src/main/java/com/nashtech/common/command/ApproveOrderCommand.java
index 02a1de45..5b9d4188 100644
--- a/common/src/main/java/com/nashtech/common/command/ApproveOrderCommand.java
+++ b/common/src/main/java/com/nashtech/common/command/ApproveOrderCommand.java
@@ -10,6 +10,8 @@
public class ApproveOrderCommand {
@TargetAggregateIdentifier
String orderId;
+ String paymentId;
+ String shipmentId;
OrderStatus orderStatus;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/command/CancelShipmentCommand.java b/common/src/main/java/com/nashtech/common/command/CancelShipmentCommand.java
index 0f6b95ba..a8420aae 100644
--- a/common/src/main/java/com/nashtech/common/command/CancelShipmentCommand.java
+++ b/common/src/main/java/com/nashtech/common/command/CancelShipmentCommand.java
@@ -14,7 +14,13 @@ public class CancelShipmentCommand {
String productId;
Integer quantity;
Double price;
+ Double subTotal;
+ Double grandTotal;
+ Float tax;
String userId;
+ String firstName;
+ String lastName;
+ String address;
String reasonToFailed;
String paymentId;
ShipmentStatus shipmentStatus = ShipmentStatus.SHIPMENT_CANCELLED;
diff --git a/common/src/main/java/com/nashtech/common/command/CreateShipmentCommand.java b/common/src/main/java/com/nashtech/common/command/CreateShipmentCommand.java
new file mode 100644
index 00000000..fa0f8d69
--- /dev/null
+++ b/common/src/main/java/com/nashtech/common/command/CreateShipmentCommand.java
@@ -0,0 +1,23 @@
+package com.nashtech.common.command;
+
+import com.nashtech.common.model.User;
+import lombok.Builder;
+import lombok.Value;
+import org.axonframework.modelling.command.TargetAggregateIdentifier;
+
+@Value
+@Builder
+public class CreateShipmentCommand {
+ @TargetAggregateIdentifier
+ String shipmentId;
+ String paymentId;
+ String orderId;
+ User user;
+ String productId;
+ Double subTotal;
+ Double total;
+ Float tax;
+ Double basePrice;
+ Integer quantity;
+
+}
diff --git a/common/src/main/java/com/nashtech/common/command/CreatedShipmentCommand.java b/common/src/main/java/com/nashtech/common/command/CreatedShipmentCommand.java
index ab293e58..8a35e4c3 100644
--- a/common/src/main/java/com/nashtech/common/command/CreatedShipmentCommand.java
+++ b/common/src/main/java/com/nashtech/common/command/CreatedShipmentCommand.java
@@ -14,7 +14,13 @@ public class CreatedShipmentCommand {
String productId;
Integer quantity;
Double price;
+ Double subTotal;
+ Double grandTotal;
+ Float tax;
String userId;
+ String firstName;
+ String lastName;
+ String address;
String paymentId;
ShipmentStatus shipmentStatus = ShipmentStatus.SHIPMENT_CREATED;
}
diff --git a/common/src/main/java/com/nashtech/common/command/ProcessPaymentCommand.java b/common/src/main/java/com/nashtech/common/command/ProcessPaymentCommand.java
index 32e08c7e..faceaec2 100644
--- a/common/src/main/java/com/nashtech/common/command/ProcessPaymentCommand.java
+++ b/common/src/main/java/com/nashtech/common/command/ProcessPaymentCommand.java
@@ -1,8 +1,5 @@
package com.nashtech.common.command;
-import com.nashtech.common.model.PaymentDetails;
-import com.nashtech.common.model.PaymentStatus;
-import com.nashtech.common.model.User;
import lombok.Builder;
import lombok.Value;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
@@ -13,12 +10,10 @@ public class ProcessPaymentCommand {
@TargetAggregateIdentifier
String paymentId;
String orderId;
- Double price;
Integer quantity;
+ Float tax;
+ Double baseAmount;
String productId;
- User userDetails;
- PaymentDetails paymentDetails;
- PaymentStatus paymentStatus = PaymentStatus.PAYMENT_APPROVED;
-
+ String userId;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/command/ReserveProductCommand.java b/common/src/main/java/com/nashtech/common/command/ReserveProductCommand.java
index a79e0365..4beb4985 100644
--- a/common/src/main/java/com/nashtech/common/command/ReserveProductCommand.java
+++ b/common/src/main/java/com/nashtech/common/command/ReserveProductCommand.java
@@ -9,10 +9,10 @@
public class ReserveProductCommand {
@TargetAggregateIdentifier
String productId;
+ String orderId;
+ String userId;
String title;
Double basePrice;
Integer quantity;
Float tax;
- String orderId;
- String userId;
}
diff --git a/common/src/main/java/com/nashtech/common/event/OrderShippedEvent.java b/common/src/main/java/com/nashtech/common/event/OrderShippedEvent.java
index 8fcad472..c0cbacee 100644
--- a/common/src/main/java/com/nashtech/common/event/OrderShippedEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/OrderShippedEvent.java
@@ -7,5 +7,6 @@
@Builder
public class OrderShippedEvent {
String orderId;
-
+ String paymentId;
+ String shipmentId;
}
diff --git a/common/src/main/java/com/nashtech/common/event/PaymentApprovedEvent.java b/common/src/main/java/com/nashtech/common/event/PaymentApprovedEvent.java
index 5122d3f7..8ae8e722 100644
--- a/common/src/main/java/com/nashtech/common/event/PaymentApprovedEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/PaymentApprovedEvent.java
@@ -1,6 +1,6 @@
package com.nashtech.common.event;
-import com.nashtech.common.model.PaymentStatus;
+import com.nashtech.common.model.User;
import lombok.Builder;
import lombok.Value;
@@ -9,11 +9,12 @@
public class PaymentApprovedEvent {
String paymentId;
String orderId;
- Double price;
- String userId;
- Integer quantity;
String productId;
- PaymentStatus paymentStatus;
-
+ User user;
+ Integer quantity;
+ Double subTotal;
+ Double total;
+ Float tax;
+ Double basePrice;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/event/PaymentCancelledEvent.java b/common/src/main/java/com/nashtech/common/event/PaymentCancelledEvent.java
index fef33e6e..690f97a6 100644
--- a/common/src/main/java/com/nashtech/common/event/PaymentCancelledEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/PaymentCancelledEvent.java
@@ -11,7 +11,7 @@ public class PaymentCancelledEvent {
String orderId;
Integer quantity;
String userId;
- String reason;
+ String reasonToFailed;
String productId;
PaymentStatus paymentStatus;
diff --git a/common/src/main/java/com/nashtech/common/event/ProductFailedEvent.java b/common/src/main/java/com/nashtech/common/event/ProductFailedEvent.java
new file mode 100644
index 00000000..044da46f
--- /dev/null
+++ b/common/src/main/java/com/nashtech/common/event/ProductFailedEvent.java
@@ -0,0 +1,14 @@
+package com.nashtech.common.event;
+
+import lombok.Builder;
+import lombok.Value;
+
+@Value
+@Builder
+public class ProductFailedEvent {
+ String orderId;
+ String productId;
+ Integer quantity;
+ String reasonToFailed;
+
+}
diff --git a/common/src/main/java/com/nashtech/common/event/ProductReserveCancelledEvent.java b/common/src/main/java/com/nashtech/common/event/ProductReserveCancelledEvent.java
index 1fa2d887..c4191384 100644
--- a/common/src/main/java/com/nashtech/common/event/ProductReserveCancelledEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/ProductReserveCancelledEvent.java
@@ -6,7 +6,10 @@
@Value
@Builder
public class ProductReserveCancelledEvent {
+ String productId;
String orderId;
- String reasonToFailed;
+ String userId;
Integer quantity;
+ String reasonToFailed;
+
}
diff --git a/common/src/main/java/com/nashtech/common/event/ProductReserveFailedEvent.java b/common/src/main/java/com/nashtech/common/event/ProductReserveFailedEvent.java
new file mode 100644
index 00000000..a91a3ec8
--- /dev/null
+++ b/common/src/main/java/com/nashtech/common/event/ProductReserveFailedEvent.java
@@ -0,0 +1,13 @@
+package com.nashtech.common.event;
+
+import lombok.Builder;
+import lombok.Value;
+
+@Value
+@Builder
+public class ProductReserveFailedEvent {
+ String orderId;
+ String userId;
+ String productId;
+ String reasonToFailed;
+}
diff --git a/common/src/main/java/com/nashtech/common/event/ProductReservedEvent.java b/common/src/main/java/com/nashtech/common/event/ProductReservedEvent.java
index cc81a54e..0039dcc8 100644
--- a/common/src/main/java/com/nashtech/common/event/ProductReservedEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/ProductReservedEvent.java
@@ -6,10 +6,12 @@
@Value
@Builder
public class ProductReservedEvent {
- String productId;
- Double price;
- Integer quantity;
String orderId;
String userId;
+ String productId;
+ String title;
+ Double baseAmount;
+ Float tax;
+ Integer quantity;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/event/ShipmentCancelledEvent.java b/common/src/main/java/com/nashtech/common/event/ShipmentCancelledEvent.java
index e53b155a..b4ec278b 100644
--- a/common/src/main/java/com/nashtech/common/event/ShipmentCancelledEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/ShipmentCancelledEvent.java
@@ -12,7 +12,13 @@ public class ShipmentCancelledEvent {
String productId;
Integer quantity;
Double price;
+ Double subTotal;
+ Double grandTotal;
+ Float tax;
String userId;
+ String firstName;
+ String lastName;
+ String address;
String reasonToFailed;
String paymentId;
ShipmentStatus shipmentStatus;
diff --git a/common/src/main/java/com/nashtech/common/event/ShipmentCreatedEvent.java b/common/src/main/java/com/nashtech/common/event/ShipmentCreatedEvent.java
index e8365a6f..83b646bd 100644
--- a/common/src/main/java/com/nashtech/common/event/ShipmentCreatedEvent.java
+++ b/common/src/main/java/com/nashtech/common/event/ShipmentCreatedEvent.java
@@ -1,6 +1,6 @@
package com.nashtech.common.event;
-import com.nashtech.common.model.ShipmentStatus;
+import com.nashtech.common.model.User;
import lombok.Builder;
import lombok.Value;
@@ -8,11 +8,14 @@
@Builder
public class ShipmentCreatedEvent {
String shipmentId;
+ String paymentId;
String orderId;
+ User user;
String productId;
+ Double subTotal;
+ Double total;
+ Float tax;
+ Double basePrice;
Integer quantity;
- Double price;
- String userId;
- String paymentId;
- ShipmentStatus shipmentStatus;
+
}
diff --git a/common/src/main/java/com/nashtech/common/model/PaymentDetails.java b/common/src/main/java/com/nashtech/common/model/PaymentDetails.java
index 915387e2..c7e7a266 100644
--- a/common/src/main/java/com/nashtech/common/model/PaymentDetails.java
+++ b/common/src/main/java/com/nashtech/common/model/PaymentDetails.java
@@ -6,10 +6,12 @@
@Value
@Builder
public class PaymentDetails {
+ String userId;
String bank;
String cardNumber;
Integer validUntilMonth;
Integer validUntilYear;
Integer cvv;
+ Double balanceAmount;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/model/User.java b/common/src/main/java/com/nashtech/common/model/User.java
index 4c365d73..776622fd 100644
--- a/common/src/main/java/com/nashtech/common/model/User.java
+++ b/common/src/main/java/com/nashtech/common/model/User.java
@@ -1,14 +1,16 @@
package com.nashtech.common.model;
import lombok.Builder;
-import lombok.Value;
+import lombok.Data;
-@Value
+@Data
@Builder
public class User {
- String firstName;
- String lastName;
- String userId;
- String address;
+ private String userId;
+ private String firstName;
+ private String lastName;
+ private String address;
+ private String emailId;
+ private String mobileNumber;
}
\ No newline at end of file
diff --git a/common/src/main/java/com/nashtech/common/utils/OrderStatus.java b/common/src/main/java/com/nashtech/common/utils/OrderStatus.java
index 35eeb584..5dd01e64 100644
--- a/common/src/main/java/com/nashtech/common/utils/OrderStatus.java
+++ b/common/src/main/java/com/nashtech/common/utils/OrderStatus.java
@@ -1,6 +1,6 @@
package com.nashtech.common.utils;
public enum OrderStatus {
- ORDER_CREATED, ORDER_APPROVED, ORDER_REJECTED
+ ORDER_NOT_APPROVED,ORDER_PARTIALLY_APPROVED, ORDER_APPROVED, ORDER_PLACED
}
\ No newline at end of file
diff --git a/inventory-service/app-config.yaml b/inventory-service/app-config.yaml
new file mode 100644
index 00000000..63d30df1
--- /dev/null
+++ b/inventory-service/app-config.yaml
@@ -0,0 +1,8 @@
+kind: ConfigMap
+apiVersion: v1
+metadata:
+ name: inventory-configmap
+data:
+ # Configuration values can be set as key-value properties
+ MYSQL_HOST: 35.231.178.113
+ AXON_HOST: 34.23.126.183:8124
diff --git a/inventory-service/deployment.yaml b/inventory-service/deployment.yaml
index 4a139921..1961f955 100644
--- a/inventory-service/deployment.yaml
+++ b/inventory-service/deployment.yaml
@@ -22,3 +22,19 @@ spec:
- name: http
containerPort: 9091
protocol: TCP
+ env:
+ - name: MYSQL_DB_USERNAME
+ valueFrom:
+ secretKeyRef:
+ name: secret-to-be-created
+ key: mysql-db-username
+ optional: false
+ - name: MYSQL_DB_PASSWORD
+ valueFrom:
+ secretKeyRef:
+ name: secret-to-be-created
+ key: mysql-db-userpassword
+ optional: false
+ envFrom:
+ - configMapRef:
+ name: inventory-configmap
\ No newline at end of file
diff --git a/inventory-service/kustomization.yaml b/inventory-service/kustomization.yaml
index 42a42ff1..4809c39a 100644
--- a/inventory-service/kustomization.yaml
+++ b/inventory-service/kustomization.yaml
@@ -2,6 +2,8 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
+- secretstore.yaml
+- app-config.yaml
- service.yaml
- deployment.yaml
diff --git a/inventory-service/pom.xml b/inventory-service/pom.xml
index 1668344a..8a34fb0c 100644
--- a/inventory-service/pom.xml
+++ b/inventory-service/pom.xml
@@ -11,8 +11,7 @@
com.nashtech.inventory
inventory-service
1.0.0
- Inventory-Mircoservice
- Demo project for Spring Boot
+ inventory-service
@@ -87,11 +86,6 @@
org.springframework
spring-web
-
-
-
-
-
javax.validation
diff --git a/inventory-service/secretstore.yaml b/inventory-service/secretstore.yaml
new file mode 100644
index 00000000..2d9f8e99
--- /dev/null
+++ b/inventory-service/secretstore.yaml
@@ -0,0 +1,42 @@
+apiVersion: external-secrets.io/v1beta1
+kind: SecretStore
+metadata:
+ name: car-demo-inventory-secretstore
+ namespace: default
+spec:
+ provider:
+ gcpsm:
+ auth:
+ secretRef:
+ secretAccessKeySecretRef:
+ name: gcpsm-secret
+ key: secret-access-credentials
+ projectID: boreal-gravity-396810
+---
+apiVersion: external-secrets.io/v1beta1
+kind: ExternalSecret
+metadata:
+ name: car-demo-externalsecret
+ namespace: default
+spec:
+ secretStoreRef:
+ name: car-demo-inventory-secretstore
+ kind: SecretStore
+ target:
+ name: secret-to-be-created
+ data:
+ - secretKey: "mysql-db-username"
+ remoteRef:
+ key: car-demo-secret
+ version: latest
+ property: mysql-db-username
+ - secretKey: "mysql-db-userpassword"
+ remoteRef:
+ key: car-demo-secret
+ version: latest
+ property: mysql-db-userpassword
+
+ - secretKey: "MY_SQL_SECRETS"
+ remoteRef:
+ key: car-demo-secret
+ version: latest
\ No newline at end of file
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/InventoryServiceApplication.java b/inventory-service/src/main/java/com/nashtech/inventory/InventoryServiceApplication.java
index 66556f78..01418e57 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/InventoryServiceApplication.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/InventoryServiceApplication.java
@@ -1,7 +1,7 @@
package com.nashtech.inventory;
import com.nashtech.inventory.command.interceptors.CreateProductCommandInterceptor;
-import com.nashtech.inventory.core.errorhandling.ProductsServiceEventsErrorHandler;
+import com.nashtech.inventory.exception.ProductsServiceEventsErrorHandler;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.config.EventProcessingConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/InventoryAggregate.java b/inventory-service/src/main/java/com/nashtech/inventory/aggregate/InventoryAggregate.java
similarity index 62%
rename from inventory-service/src/main/java/com/nashtech/inventory/command/InventoryAggregate.java
rename to inventory-service/src/main/java/com/nashtech/inventory/aggregate/InventoryAggregate.java
index da9d48bf..86667245 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/InventoryAggregate.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/aggregate/InventoryAggregate.java
@@ -1,10 +1,14 @@
-package com.nashtech.inventory.command;
+package com.nashtech.inventory.aggregate;
+import com.nashtech.common.command.CancelProductReserveCommand;
import com.nashtech.common.command.ReserveProductCommand;
import com.nashtech.common.event.ProductReserveCancelledEvent;
+import com.nashtech.common.event.ProductReserveFailedEvent;
import com.nashtech.common.event.ProductReservedEvent;
-import com.nashtech.inventory.core.events.ProductCreatedEvent;
+import com.nashtech.inventory.command.CreateProductCommand;
+import com.nashtech.inventory.events.ProductCreatedEvent;
+import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier;
@@ -15,28 +19,32 @@
@Aggregate
+@Slf4j
public class InventoryAggregate{
@AggregateIdentifier
private String productId;
private String title;
- private Double price;
+ private Double basePrice;
+ private Float tax;
private Integer quantity;
+ private String userId;
+ private String orderId;
+
public InventoryAggregate() {
}
@CommandHandler
public InventoryAggregate(CreateProductCommand createProductCommand) {
-
+ log.info("CreateProductCommand started with productId {}",createProductCommand.getProductId());
if(createProductCommand.getTitle() == null
|| createProductCommand.getTitle().isBlank()) {
throw new IllegalArgumentException("Title cannot be empty");
}
ProductCreatedEvent productCreatedEvent = new ProductCreatedEvent();
-
BeanUtils.copyProperties(createProductCommand, productCreatedEvent);
AggregateLifecycle.apply(productCreatedEvent);
@@ -44,18 +52,25 @@ public InventoryAggregate(CreateProductCommand createProductCommand) {
@CommandHandler
public void handle(ReserveProductCommand reserveProductCommand) {
-
+ log.info("ReserveProductCommand started with productId {}",reserveProductCommand.getProductId());
if(quantity < reserveProductCommand.getQuantity()) {
- ProductReserveCancelledEvent productReserveCancelledEvent = ProductReserveCancelledEvent.builder()
+ ProductReserveFailedEvent productFailedEvent = ProductReserveFailedEvent.builder()
+ .productId(reserveProductCommand.getProductId())
+ .userId(reserveProductCommand.getUserId())
.orderId(reserveProductCommand.getOrderId())
.reasonToFailed("Insufficient number of items in stock").build();
+ AggregateLifecycle.apply(productFailedEvent);
+ return;
}
ProductReservedEvent productReservedEvent = ProductReservedEvent.builder()
.orderId(reserveProductCommand.getOrderId())
.productId(reserveProductCommand.getProductId())
- .quantity(reserveProductCommand.getQuantity())
.userId(reserveProductCommand.getUserId())
+ .quantity(reserveProductCommand.getQuantity())
+ .title(title)
+ .baseAmount(basePrice)
+ .tax(tax)
.build();
AggregateLifecycle.apply(productReservedEvent);
@@ -63,11 +78,13 @@ public void handle(ReserveProductCommand reserveProductCommand) {
}
@CommandHandler
- public void handle(ProductReserveCancelledEvent cancelProductReservationCommand) {
-
- ProductReserveCancelledEvent productReservationCancelledEvent =
- ProductReserveCancelledEvent.builder()
+ public void handle(CancelProductReserveCommand cancelProductReservationCommand) {
+ log.info("ProductReserveCancelledEvent started with productId {}",cancelProductReservationCommand.getProductId());
+ ProductReserveCancelledEvent productReservationCancelledEvent = ProductReserveCancelledEvent.builder()
+ .productId(cancelProductReservationCommand.getProductId())
+ .userId(cancelProductReservationCommand.getUserId())
.orderId(cancelProductReservationCommand.getOrderId())
+ .quantity(cancelProductReservationCommand.getQuantity())
.build();
AggregateLifecycle.apply(productReservationCancelledEvent);
@@ -84,7 +101,8 @@ public void on(ProductReserveCancelledEvent productReservationCancelledEvent) {
@EventSourcingHandler
public void on(ProductCreatedEvent productCreatedEvent) {
this.productId = productCreatedEvent.getProductId();
- this.price = productCreatedEvent.getPrice();
+ this.basePrice = productCreatedEvent.getBasePrice();
+ this.tax = productCreatedEvent.getTax();
this.title = productCreatedEvent.getTitle();
this.quantity = productCreatedEvent.getQuantity();
}
@@ -92,9 +110,9 @@ public void on(ProductCreatedEvent productCreatedEvent) {
@EventSourcingHandler
public void on(ProductReservedEvent productReservedEvent) {
+ this.userId = productReservedEvent.getUserId();
+ this.orderId = productReservedEvent.getOrderId();
this.quantity -= productReservedEvent.getQuantity();
}
-
-
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/CreateProductCommand.java b/inventory-service/src/main/java/com/nashtech/inventory/command/CreateProductCommand.java
index da3f8fa2..e3e3f77f 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/CreateProductCommand.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/command/CreateProductCommand.java
@@ -1,18 +1,17 @@
package com.nashtech.inventory.command;
+import lombok.Builder;
import lombok.Value;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
-import lombok.Builder;
-
@Builder
@Value
public class CreateProductCommand {
-
@TargetAggregateIdentifier
String productId;
String title;
- Double price;
+ Double basePrice;
+ Float tax;
Integer quantity;
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/interceptors/CreateProductCommandInterceptor.java b/inventory-service/src/main/java/com/nashtech/inventory/command/interceptors/CreateProductCommandInterceptor.java
index 81d96f14..1d557d3e 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/interceptors/CreateProductCommandInterceptor.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/command/interceptors/CreateProductCommandInterceptor.java
@@ -1,24 +1,20 @@
package com.nashtech.inventory.command.interceptors;
-import java.math.BigDecimal;
-import java.util.List;
-import java.util.function.BiFunction;
-
-
import com.nashtech.inventory.command.CreateProductCommand;
-import com.nashtech.inventory.core.data.ProductLookupEntity;
-import com.nashtech.inventory.core.data.ProductLookupRepository;
+import com.nashtech.inventory.repository.ProductLookupEntity;
+import com.nashtech.inventory.repository.ProductLookupRepository;
+import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.messaging.MessageDispatchInterceptor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
+import java.util.List;
+import java.util.function.BiFunction;
+
@Component
+@Slf4j
public class CreateProductCommandInterceptor implements MessageDispatchInterceptor> {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(CreateProductCommandInterceptor.class);
private final ProductLookupRepository productLookupRepository;
public CreateProductCommandInterceptor(ProductLookupRepository productLookupRepository) {
@@ -31,14 +27,13 @@ public BiFunction, CommandMessage>> handle(
return (index, command) -> {
- LOGGER.info("Intercepted command: " + command.getPayloadType());
+ log.info("Intercepted command: " + command.getPayloadType());
if(CreateProductCommand.class.equals(command.getPayloadType())) {
CreateProductCommand createProductCommand = (CreateProductCommand)command.getPayload();
- ProductLookupEntity productLookupEntity = productLookupRepository.findByProductIdOrTitle(createProductCommand.getProductId(),
- createProductCommand.getTitle());
+ ProductLookupEntity productLookupEntity = productLookupRepository.findByProductId(createProductCommand.getProductId());
if(productLookupEntity != null) {
throw new IllegalStateException(
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/rest/ProductsController.java b/inventory-service/src/main/java/com/nashtech/inventory/command/rest/ProductsController.java
deleted file mode 100644
index 596960a5..00000000
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/rest/ProductsController.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package com.nashtech.inventory.command.rest;
-
-import com.nashtech.inventory.command.CreateProductCommand;
-import com.nashtech.inventory.query.FindProductsQuery;
-import com.nashtech.inventory.query.rest.ProductRestModel;
-import org.axonframework.commandhandling.gateway.CommandGateway;
-import org.axonframework.messaging.responsetypes.ResponseTypes;
-import org.axonframework.queryhandling.QueryGateway;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import javax.validation.Valid;
-import java.util.List;
-import java.util.UUID;
-
-@RestController
-@RequestMapping("/products")
-public class ProductsController {
- @Autowired
- private QueryGateway queryGateway;
- private final CommandGateway commandGateway;
-
- public ProductsController(CommandGateway commandGateway) {
- this.commandGateway = commandGateway;
- }
- @GetMapping("get")
- public List getProducts() {
- FindProductsQuery findProductsQuery = new FindProductsQuery();
- return queryGateway.query(findProductsQuery,
- ResponseTypes.multipleInstancesOf(ProductRestModel.class)).join();
- }
-
-
- @PostMapping
- public String createProduct(@Valid @RequestBody CreateProductRestModel createProductRestModel) {
-
- CreateProductCommand createProductCommand = CreateProductCommand.builder()
- .price(createProductRestModel.getPrice())
- .quantity(createProductRestModel.getQuantity())
- .title(createProductRestModel.getTitle())
- .productId(UUID.randomUUID().toString()).build();
-
- String returnValue;
-
- returnValue = commandGateway.sendAndWait(createProductCommand);
-
-
- return returnValue;
- }
-}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/config/AxonXStreamConfig.java b/inventory-service/src/main/java/com/nashtech/inventory/config/AxonXStreamConfig.java
new file mode 100644
index 00000000..a94d2923
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/config/AxonXStreamConfig.java
@@ -0,0 +1,19 @@
+package com.nashtech.inventory.config;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.security.AnyTypePermission;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class AxonXStreamConfig {
+
+ @Bean
+ public XStream xStream() {
+ XStream xStream = new XStream();
+ xStream.addPermission(AnyTypePermission.ANY);
+
+ return xStream;
+ }
+
+}
\ No newline at end of file
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupEntity.java b/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupEntity.java
deleted file mode 100644
index 461ab22f..00000000
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupEntity.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package com.nashtech.inventory.core.data;
-
-import java.io.Serializable;
-
-import jakarta.persistence.Column;
-import jakarta.persistence.Entity;
-import jakarta.persistence.Id;
-import jakarta.persistence.Table;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-
-
-@Data
-@AllArgsConstructor
-@NoArgsConstructor
-@Entity
-@Table(name="productlookup")
-public class ProductLookupEntity implements Serializable {
-
- private static final long serialVersionUID = 2788007460547645663L;
-
- @Id
- private String productId;
-
- @Column(unique=true)
- private String title;
-
-}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/events/ProductCreatedEvent.java b/inventory-service/src/main/java/com/nashtech/inventory/events/ProductCreatedEvent.java
similarity index 59%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/events/ProductCreatedEvent.java
rename to inventory-service/src/main/java/com/nashtech/inventory/events/ProductCreatedEvent.java
index 74456c36..35f6caad 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/events/ProductCreatedEvent.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/events/ProductCreatedEvent.java
@@ -1,6 +1,4 @@
-package com.nashtech.inventory.core.events;
-
-import java.math.BigDecimal;
+package com.nashtech.inventory.events;
import lombok.Data;
@@ -9,7 +7,8 @@ public class ProductCreatedEvent {
private String productId;
private String title;
- private Double price;
+ private Double basePrice;
+ private Float tax;
private Integer quantity;
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ErrorMessage.java b/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java
similarity index 79%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ErrorMessage.java
rename to inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java
index 8bbf0597..073441af 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ErrorMessage.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java
@@ -1,10 +1,10 @@
-package com.nashtech.inventory.core.errorhandling;
-
-import java.util.Date;
+package com.nashtech.inventory.exception;
import lombok.AllArgsConstructor;
import lombok.Data;
+import java.util.Date;
+
@Data
@AllArgsConstructor
public class ErrorMessage {
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ProductsServiceEventsErrorHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsServiceEventsErrorHandler.java
similarity index 89%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ProductsServiceEventsErrorHandler.java
rename to inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsServiceEventsErrorHandler.java
index 63bbdb68..1cfb371c 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/errorhandling/ProductsServiceEventsErrorHandler.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsServiceEventsErrorHandler.java
@@ -1,4 +1,4 @@
-package com.nashtech.inventory.core.errorhandling;
+package com.nashtech.inventory.exception;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.eventhandling.EventMessageHandler;
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/handler/ProductEventsHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/handler/ProductEventsHandler.java
new file mode 100644
index 00000000..6290a201
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/handler/ProductEventsHandler.java
@@ -0,0 +1,79 @@
+package com.nashtech.inventory.handler;
+
+
+import com.nashtech.common.event.ProductReserveCancelledEvent;
+import com.nashtech.common.event.ProductReservedEvent;
+import com.nashtech.inventory.events.ProductCreatedEvent;
+import com.nashtech.inventory.repository.Product;
+import com.nashtech.inventory.repository.ProductsRepository;
+import com.nashtech.inventory.repository.ProductsSold;
+import com.nashtech.inventory.repository.ProductsSoldRepository;
+import lombok.extern.slf4j.Slf4j;
+import org.axonframework.config.ProcessingGroup;
+import org.axonframework.eventhandling.EventHandler;
+import org.axonframework.messaging.interceptors.ExceptionHandler;
+import org.springframework.beans.BeanUtils;
+import org.springframework.stereotype.Component;
+
+
+@Component
+@ProcessingGroup("product-group")
+@Slf4j
+public class ProductEventsHandler {
+
+ private final ProductsRepository productsRepository;
+ private final ProductsSoldRepository productsSoldRepository;
+
+ public ProductEventsHandler(ProductsRepository productsRepository, ProductsSoldRepository productsSoldRepository) {
+ this.productsRepository = productsRepository;
+ this.productsSoldRepository = productsSoldRepository;
+ }
+
+ @EventHandler
+ public void on(ProductCreatedEvent event) {
+ Product productEntity = new Product();
+ BeanUtils.copyProperties(event, productEntity);
+ productsRepository.save(productEntity);
+ }
+
+ @EventHandler
+ public void on(ProductReservedEvent productReservedEvent) {
+ log.info("ProductReservedEvent: Remaining product stock {}", productReservedEvent.getQuantity());
+ Product product = productsRepository.findByProductId(productReservedEvent.getProductId());
+ product.setQuantity(product.getQuantity() - productReservedEvent.getQuantity());
+ productsRepository.save(product);
+
+ ProductsSold soldProduct = new ProductsSold(product.getProductId(), productReservedEvent.getOrderId(),
+ productReservedEvent.getUserId(), productReservedEvent.getQuantity(),productReservedEvent.getTitle(),
+ productReservedEvent.getBaseAmount(),productReservedEvent.getTax());
+ productsSoldRepository.save(soldProduct);
+ }
+
+ @EventHandler
+ public void on(ProductReserveCancelledEvent productReservationCancelledEvent) {
+ log.info("ProductReservationCancelledEvent: Reversing product {} quantity {}",
+ productReservationCancelledEvent.getQuantity(), productReservationCancelledEvent.getProductId());
+
+ Product currentlyStoredProduct = productsRepository.findByProductId(productReservationCancelledEvent.getOrderId());
+ currentlyStoredProduct.setQuantity(currentlyStoredProduct.getQuantity() +
+ productReservationCancelledEvent.getQuantity());
+
+ productsRepository.save(currentlyStoredProduct);
+
+ log.info("Stable product {} quantity {}", productReservationCancelledEvent.getProductId(),
+ currentlyStoredProduct.getQuantity());
+
+ ProductsSold currentlyStoredSoldProduct =
+ productsSoldRepository.findByProductIdAndOrderId(productReservationCancelledEvent.getProductId(),
+ productReservationCancelledEvent.getOrderId());
+ if (currentlyStoredSoldProduct != null) {
+ productsSoldRepository.delete(currentlyStoredSoldProduct);
+ }
+ }
+
+ @ExceptionHandler(resultType = Exception.class)
+ public void handle(Exception exception) throws Exception {
+ throw exception;
+ }
+
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/ProductLookupEventsHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/handler/ProductLookupEventsHandler.java
similarity index 53%
rename from inventory-service/src/main/java/com/nashtech/inventory/command/ProductLookupEventsHandler.java
rename to inventory-service/src/main/java/com/nashtech/inventory/handler/ProductLookupEventsHandler.java
index c36a7eb7..1a4c31d9 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/ProductLookupEventsHandler.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/handler/ProductLookupEventsHandler.java
@@ -1,11 +1,10 @@
-package com.nashtech.inventory.command;
+package com.nashtech.inventory.handler;
-import com.nashtech.inventory.core.data.ProductLookupEntity;
-import com.nashtech.inventory.core.data.ProductLookupRepository;
-import com.nashtech.inventory.core.events.ProductCreatedEvent;
+import com.nashtech.inventory.events.ProductCreatedEvent;
+import com.nashtech.inventory.repository.ProductLookupEntity;
+import com.nashtech.inventory.repository.ProductLookupRepository;
import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.EventHandler;
-import org.axonframework.eventhandling.ResetHandler;
import org.springframework.stereotype.Component;
@@ -21,12 +20,7 @@ public ProductLookupEventsHandler(ProductLookupRepository productLookupRepositor
@EventHandler
public void on(ProductCreatedEvent event) {
-
- ProductLookupEntity productLookupEntity = new ProductLookupEntity(event.getProductId(),
- event.getTitle());
-
- productLookupRepository.save(productLookupEntity);
-
+ productLookupRepository.save(new ProductLookupEntity(event.getProductId()));
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/ProductEventsHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductEventsHandler.java
deleted file mode 100644
index 02e22dc5..00000000
--- a/inventory-service/src/main/java/com/nashtech/inventory/query/ProductEventsHandler.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.nashtech.inventory.query;
-
-
-import com.nashtech.common.event.ProductReserveCancelledEvent;
-import com.nashtech.common.event.ProductReservedEvent;
-import com.nashtech.inventory.core.data.Product;
-import com.nashtech.inventory.core.data.ProductsRepository;
-import com.nashtech.inventory.core.events.ProductCreatedEvent;
-import org.axonframework.config.ProcessingGroup;
-import org.axonframework.eventhandling.EventHandler;
-import org.axonframework.eventhandling.ResetHandler;
-import org.axonframework.messaging.interceptors.ExceptionHandler;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeanUtils;
-import org.springframework.stereotype.Component;
-
-
-@Component
-@ProcessingGroup("product-group")
-public class ProductEventsHandler {
-
- private final ProductsRepository productsRepository;
- private static final Logger LOGGER = LoggerFactory.getLogger(ProductEventsHandler.class);
-
- public ProductEventsHandler(ProductsRepository productsRepository) {
- this.productsRepository = productsRepository;
- }
-
- @ExceptionHandler(resultType=Exception.class)
- public void handle(Exception exception) throws Exception {
- throw exception;
- }
-
- @ExceptionHandler(resultType=IllegalArgumentException.class)
- public void handle(IllegalArgumentException exception) {
-
- }
-
-
- @EventHandler
- public void on(ProductCreatedEvent event) {
-
- Product productEntity = new Product();
- BeanUtils.copyProperties(event, productEntity);
-
- try {
- productsRepository.save(productEntity);
- } catch (IllegalArgumentException ex) {
- ex.printStackTrace();
- }
-
- }
-
- @EventHandler
- public void on(ProductReservedEvent productReservedEvent) {
- Product productEntity = productsRepository.findByProductId(productReservedEvent.getProductId());
-
- LOGGER.debug("ProductReservedEvent: Current product quantity " + productEntity.getQuantity());
-
- productEntity.setQuantity(productEntity.getQuantity() - productReservedEvent.getQuantity());
-
-
- productsRepository.save(productEntity);
-
- LOGGER.debug("ProductReservedEvent: New product quantity " + productEntity.getQuantity());
-
- LOGGER.info("ProductReservedEvent is called for productId:" + productReservedEvent.getProductId() +
- " and orderId: " + productReservedEvent.getOrderId());
- }
-
- @EventHandler
- public void on(ProductReserveCancelledEvent productReservationCancelledEvent) {
- Product currentlyStoredProduct = productsRepository.findByProductId(productReservationCancelledEvent.getOrderId());
-
- LOGGER.debug("ProductReservationCancelledEvent: Current product quantity "
- + currentlyStoredProduct.getQuantity() );
-
- int newQuantity = currentlyStoredProduct.getQuantity() + productReservationCancelledEvent.getQuantity();
- currentlyStoredProduct.setQuantity(newQuantity);
-
- productsRepository.save(currentlyStoredProduct);
-
- LOGGER.debug("ProductReservationCancelledEvent: New product quantity "
- + currentlyStoredProduct.getQuantity() );
-
- }
-
- @ResetHandler
- public void reset() {
- productsRepository.deleteAll();
- }
-
-}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java
index f86de9fd..ab30b1d8 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java
@@ -1,15 +1,14 @@
package com.nashtech.inventory.query;
-import java.util.ArrayList;
-import java.util.List;
-
-import com.nashtech.inventory.core.data.Product;
-import com.nashtech.inventory.core.data.ProductsRepository;
-import com.nashtech.inventory.query.rest.ProductRestModel;
+import com.nashtech.inventory.repository.Product;
+import com.nashtech.inventory.repository.ProductsRepository;
import org.axonframework.queryhandling.QueryHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
+import java.util.ArrayList;
+import java.util.List;
+
@Component
public class ProductsQueryHandler {
@@ -21,18 +20,13 @@ public ProductsQueryHandler(ProductsRepository productsRepository) {
}
@QueryHandler
- public List findProducts(FindProductsQuery query) {
-
- List productsRest = new ArrayList<>();
-
- List storedProducts = productsRepository.findAll();
-
- for(Product productEntity: storedProducts) {
- ProductRestModel productRestModel = new ProductRestModel();
- BeanUtils.copyProperties(productEntity, productRestModel);
- productsRest.add(productRestModel);
+ public List findProducts(FindProductsQuery query) {
+ List productsRest = new ArrayList<>();
+ for(Product productEntity: productsRepository.findAll()) {
+ ProductsSummary productRequest = new ProductsSummary();
+ BeanUtils.copyProperties(productEntity, productRequest);
+ productsRest.add(productRequest);
}
-
return productsRest;
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java
new file mode 100644
index 00000000..7f4b4e07
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java
@@ -0,0 +1,13 @@
+package com.nashtech.inventory.query;
+
+import lombok.Data;
+
+@Data
+public class ProductsSummary {
+ private String productId;
+ private String title;
+ private Double basePrice;
+ private Float tax;
+ private Integer quantity;
+
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductRestModel.java b/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductRestModel.java
deleted file mode 100644
index 55e81799..00000000
--- a/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductRestModel.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.nashtech.inventory.query.rest;
-
-import java.math.BigDecimal;
-
-import lombok.Data;
-
-@Data
-public class ProductRestModel {
- private String productId;
- private String title;
- private Double price;
- private Integer quantity;
-}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductsQueryController.java b/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductsQueryController.java
deleted file mode 100644
index c2f0e504..00000000
--- a/inventory-service/src/main/java/com/nashtech/inventory/query/rest/ProductsQueryController.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.nashtech.inventory.query.rest;
-
-import java.util.List;
-
-import com.nashtech.inventory.query.FindProductsQuery;
-import org.axonframework.messaging.responsetypes.ResponseTypes;
-import org.axonframework.queryhandling.QueryGateway;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-
-
-@RestController
-@RequestMapping("/products")
-public class ProductsQueryController {
-
- @Autowired
- QueryGateway queryGateway;
-
- @GetMapping
- public List getProducts() {
-
- FindProductsQuery findProductsQuery = new FindProductsQuery();
- List products = queryGateway.query(findProductsQuery,
- ResponseTypes.multipleInstancesOf(ProductRestModel.class)).join();
-
- return products;
-
-
- }
-
-}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/data/Product.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/Product.java
similarity index 88%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/data/Product.java
rename to inventory-service/src/main/java/com/nashtech/inventory/repository/Product.java
index ab348fb8..29e28dce 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/data/Product.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/Product.java
@@ -1,4 +1,4 @@
-package com.nashtech.inventory.core.data;
+package com.nashtech.inventory.repository;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@@ -11,7 +11,6 @@
@Table(name="products")
@Data
public class Product {
-
@Id
private String productId;
private String title;
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupEntity.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupEntity.java
new file mode 100644
index 00000000..0c50495b
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupEntity.java
@@ -0,0 +1,18 @@
+package com.nashtech.inventory.repository;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.Id;
+import jakarta.persistence.Table;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+
+
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+@Table(name="product_lookup")
+public class ProductLookupEntity{
+ @Id
+ private String productId;
+
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupRepository.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupRepository.java
similarity index 57%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupRepository.java
rename to inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupRepository.java
index d52f5972..e8ae377f 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductLookupRepository.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductLookupRepository.java
@@ -1,7 +1,8 @@
-package com.nashtech.inventory.core.data;
+package com.nashtech.inventory.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductLookupRepository extends JpaRepository {
- ProductLookupEntity findByProductIdOrTitle(String productId, String title);
+ ProductLookupEntity findByProductId(String productId);
+
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductsRepository.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsRepository.java
similarity index 63%
rename from inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductsRepository.java
rename to inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsRepository.java
index 7caf51ed..c23460cd 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/core/data/ProductsRepository.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsRepository.java
@@ -1,10 +1,8 @@
-package com.nashtech.inventory.core.data;
+package com.nashtech.inventory.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductsRepository extends JpaRepository {
-
Product findByProductId(String productId);
- Product findByProductIdOrTitle(String productId, String title);
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSold.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSold.java
new file mode 100644
index 00000000..e16288be
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSold.java
@@ -0,0 +1,24 @@
+package com.nashtech.inventory.repository;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.Id;
+import jakarta.persistence.Table;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+
+
+@Entity
+@Table(name="products_sold")
+@AllArgsConstructor
+@NoArgsConstructor
+public class ProductsSold {
+ @Id
+ private String productId;
+ private String orderId;
+ private String userId;
+ private Integer quantity;
+ private String title;
+ private Double baseAmount;
+ private Float tax;
+
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSoldRepository.java b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSoldRepository.java
new file mode 100644
index 00000000..f28e4f03
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/repository/ProductsSoldRepository.java
@@ -0,0 +1,8 @@
+package com.nashtech.inventory.repository;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface ProductsSoldRepository extends JpaRepository {
+ ProductsSold findByProductIdAndOrderId(String productId,String orderId);
+
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/command/rest/CreateProductRestModel.java b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java
similarity index 75%
rename from inventory-service/src/main/java/com/nashtech/inventory/command/rest/CreateProductRestModel.java
rename to inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java
index 88bb40d8..6964cd96 100644
--- a/inventory-service/src/main/java/com/nashtech/inventory/command/rest/CreateProductRestModel.java
+++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java
@@ -1,24 +1,17 @@
-package com.nashtech.inventory.command.rest;
-
-import java.math.BigDecimal;
-
+package com.nashtech.inventory.restapi;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
-
@Data
-public class CreateProductRestModel {
-
+public class ProductRequest {
@NotBlank(message="Product title is a required field")
private String title;
-
- @Min(value=1, message="Price cannot be lower than 1")
- private Double price;
-
@Min(value=1, message="Quantity cannot be lower than 1")
+ private Double price;
+ @Min(value=1, message="Price cannot be lower than 1")
private Integer quantity;
-
+ private Float tax;
}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java
new file mode 100644
index 00000000..6a92925b
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java
@@ -0,0 +1,46 @@
+package com.nashtech.inventory.restapi;
+
+import com.nashtech.inventory.command.CreateProductCommand;
+import com.nashtech.inventory.query.FindProductsQuery;
+import com.nashtech.inventory.query.ProductsSummary;
+import org.axonframework.commandhandling.gateway.CommandGateway;
+import org.axonframework.messaging.responsetypes.ResponseTypes;
+import org.axonframework.queryhandling.QueryGateway;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.List;
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/products")
+public class ProductsController {
+ @Autowired
+ private QueryGateway queryGateway;
+ private final CommandGateway commandGateway;
+
+ public ProductsController(CommandGateway commandGateway) {
+ this.commandGateway = commandGateway;
+ }
+ @GetMapping("get")
+ public List getProducts() {
+ return queryGateway.query(new FindProductsQuery(), ResponseTypes.multipleInstancesOf(ProductsSummary.class)).join();
+ }
+
+
+ @PostMapping
+ public String createProduct(@Valid @RequestBody List createProductRequest) {
+ createProductRequest.forEach(product->{
+ CreateProductCommand createProductCommand = CreateProductCommand.builder()
+ .basePrice(product.getPrice())
+ .quantity(product.getQuantity())
+ .title(product.getTitle())
+ .tax(product.getTax())
+ .productId(UUID.randomUUID().toString()).build();
+ commandGateway.send(createProductCommand);
+ });
+
+ return "Products added";
+ }
+}
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/ErrorMessage.java b/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/ErrorMessage.java
new file mode 100644
index 00000000..913b30f0
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/ErrorMessage.java
@@ -0,0 +1,14 @@
+package com.nashtech.inventory.restapi.exception;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+@AllArgsConstructor
+public class ErrorMessage {
+ private final Date timestamp;
+ private final String message;
+
+}
\ No newline at end of file
diff --git a/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/OrderServiceErrorHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/OrderServiceErrorHandler.java
new file mode 100644
index 00000000..c4775d7c
--- /dev/null
+++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/exception/OrderServiceErrorHandler.java
@@ -0,0 +1,21 @@
+package com.nashtech.inventory.restapi.exception;
+
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.context.request.WebRequest;
+
+import java.util.Date;
+
+@ControllerAdvice
+public class OrderServiceErrorHandler {
+
+ @ExceptionHandler(value = {Exception.class})
+ public ResponseEntity