From a73c51ee50f25d939f73ac0d812a25d7352f7fe4 Mon Sep 17 00:00:00 2001 From: abidknashtech Date: Thu, 5 Oct 2023 14:27:31 +0530 Subject: [PATCH] added get product api --- inventory-service/pom.xml | 2 +- .../inventory/exception/ErrorMessage.java | 7 ++-- .../inventory/exception/ProductNotFound.java | 11 +++++++ .../exception/ProductsErrorHandler.java | 19 +++++++++++ .../inventory/query/FindProductsQuery.java | 12 +++++++ .../inventory/query/ProductsQueryHandler.java | 32 +++++++++++++++++++ .../inventory/query/ProductsSummary.java | 17 ++++++++++ .../inventory/restapi/ProductRequest.java | 17 ++++++++++ .../inventory/restapi/ProductsController.java | 22 +++++++++++++ local-dev/docker-compose.yml | 2 ++ order-service/pom.xml | 2 +- .../restapi/OrdersCommandController.java | 13 ++++++-- payment-service/pom.xml | 2 +- shipment-service/pom.xml | 2 +- 14 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/exception/ProductNotFound.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsErrorHandler.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/query/FindProductsQuery.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java create mode 100644 inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java diff --git a/inventory-service/pom.xml b/inventory-service/pom.xml index e059dca9..c82d1b0f 100644 --- a/inventory-service/pom.xml +++ b/inventory-service/pom.xml @@ -60,7 +60,7 @@ 19 - 1.1.0 + 2.0 2.0.1.Final 4.1.1 2022.0.1 diff --git a/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java b/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java index 073441af..8f9e843a 100644 --- a/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java +++ b/inventory-service/src/main/java/com/nashtech/inventory/exception/ErrorMessage.java @@ -2,14 +2,15 @@ import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.Date; @Data @AllArgsConstructor +@NoArgsConstructor public class ErrorMessage { - - private final Date timestamp; - private final String message; + private Date timestamp; + private String message; } diff --git a/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductNotFound.java b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductNotFound.java new file mode 100644 index 00000000..37f8ec32 --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductNotFound.java @@ -0,0 +1,11 @@ +package com.nashtech.inventory.exception; + +public class ProductNotFound extends RuntimeException { + private String message; + + public ProductNotFound(String message) { + super(message); + this.message = message; + } + +} diff --git a/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsErrorHandler.java b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsErrorHandler.java new file mode 100644 index 00000000..ee0bfaac --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsErrorHandler.java @@ -0,0 +1,19 @@ +package com.nashtech.inventory.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 java.util.Date; + +@ControllerAdvice +public class ProductsErrorHandler { + @ExceptionHandler(value = {Exception.class}) + public ResponseEntity handleOtherExceptions(Exception ex) { + ErrorMessage errorMessage = new ErrorMessage(new Date(), ex.getMessage()); + return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); + } + +} \ No newline at end of file diff --git a/inventory-service/src/main/java/com/nashtech/inventory/query/FindProductsQuery.java b/inventory-service/src/main/java/com/nashtech/inventory/query/FindProductsQuery.java new file mode 100644 index 00000000..d34adda5 --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/query/FindProductsQuery.java @@ -0,0 +1,12 @@ +package com.nashtech.inventory.query; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class FindProductsQuery { + private String productId; +} 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 new file mode 100644 index 00000000..8b42d7f8 --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java @@ -0,0 +1,32 @@ +package com.nashtech.inventory.query; + +import com.nashtech.inventory.exception.ProductNotFound; +import com.nashtech.inventory.repository.ProductEntity; +import com.nashtech.inventory.repository.ProductsRepository; +import org.axonframework.queryhandling.QueryHandler; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +@Component +public class ProductsQueryHandler { + + private final ProductsRepository productsRepository; + + public ProductsQueryHandler(ProductsRepository productsRepository) { + this.productsRepository = productsRepository; + } + + @QueryHandler + public ProductsSummary findProducts(FindProductsQuery query) { + ProductEntity product = productsRepository.findByProductId(query.getProductId()); + if (Objects.isNull(product)) { + throw new ProductNotFound(String.format("Product [%s] does not exist", query.getProductId())); + } + ProductsSummary productsSummary = new ProductsSummary(); + BeanUtils.copyProperties(product, productsSummary); + return productsSummary; + } + +} 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..ffe8f83f --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java @@ -0,0 +1,17 @@ +package com.nashtech.inventory.query; + +import lombok.Data; + +@Data +public class ProductsSummary { + private String productId; + private String brand; + private String model; + private Integer year; + private String color; + private Double mileage; + private Double basePrice; + private Integer quantity; + private Float tax; + +} diff --git a/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java new file mode 100644 index 00000000..6964cd96 --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java @@ -0,0 +1,17 @@ +package com.nashtech.inventory.restapi; + +import lombok.Data; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; + +@Data +public class ProductRequest { + @NotBlank(message="Product title is a required field") + private String title; + @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..401e46d3 --- /dev/null +++ b/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java @@ -0,0 +1,22 @@ +package com.nashtech.inventory.restapi; + +import com.nashtech.inventory.query.FindProductsQuery; +import com.nashtech.inventory.query.ProductsSummary; +import org.axonframework.queryhandling.QueryGateway; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/products") +public class ProductsController { + private final QueryGateway queryGateway; + + public ProductsController(QueryGateway queryGateway) { + this.queryGateway = queryGateway; + } + + @GetMapping("product/{productId}") + public ProductsSummary getProducts(@PathVariable String productId) { + return queryGateway.query(new FindProductsQuery(productId), ProductsSummary.class).join(); + } + +} diff --git a/local-dev/docker-compose.yml b/local-dev/docker-compose.yml index 3e5ac3af..80a78c2b 100644 --- a/local-dev/docker-compose.yml +++ b/local-dev/docker-compose.yml @@ -32,3 +32,5 @@ volumes: networks: axon-network: + +# insert into products (product_id, base_price, brand, color, mileage, model, quantity,tax, timestamp,year) values('101',1000000,'Honda Car','Red',30,'JMI987',20,1.5,now(),2023); \ No newline at end of file diff --git a/order-service/pom.xml b/order-service/pom.xml index 78b979d0..fd1bbd91 100644 --- a/order-service/pom.xml +++ b/order-service/pom.xml @@ -42,7 +42,7 @@ 19 3.5.9 - 1.1.0 + 2.0 2.0.1.Final diff --git a/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java b/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java index f8e37003..484a5d93 100644 --- a/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java +++ b/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java @@ -1,6 +1,7 @@ package com.nashtech.order.restapi; +import com.nashtech.common.utils.OrderStatus; import com.nashtech.order.commands.CreateOrderCommand; import com.nashtech.order.query.FindOrderQuery; import com.nashtech.order.restapi.request.OrderCreateRequest; @@ -40,12 +41,20 @@ public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequ .quantity(orderRequest.getQuantity()).orderId(orderId) .build(); - try (SubscriptionQueryResult queryResult = queryGateway.subscriptionQuery( +/* try (SubscriptionQueryResult queryResult = queryGateway.subscriptionQuery( new FindOrderQuery(orderId), ResponseTypes.instanceOf(OrderSummary.class), ResponseTypes.instanceOf(OrderSummary.class))) { commandGateway.send(createOrderCommand); return queryResult.updates().blockFirst(); - } + }*/ + + commandGateway.send(createOrderCommand); + return OrderSummary.builder() + .orderId(orderId) + .message("Thank you for your order! We’ll let you know as soon as it ships. " + + "You can track your order here,review us here, or shop again here.") + .orderStatus(OrderStatus.ORDER_PLACED.toString()) + .build(); } } \ No newline at end of file diff --git a/payment-service/pom.xml b/payment-service/pom.xml index 3e44b95a..c9816b7b 100644 --- a/payment-service/pom.xml +++ b/payment-service/pom.xml @@ -41,7 +41,7 @@ 19 - 1.1.0 + 2.0 diff --git a/shipment-service/pom.xml b/shipment-service/pom.xml index 25783a9e..71c290be 100644 --- a/shipment-service/pom.xml +++ b/shipment-service/pom.xml @@ -41,7 +41,7 @@ 19 - 1.1.0 + 2.0 4.7.2 2022.0.4