Skip to content

Commit

Permalink
added get product api
Browse files Browse the repository at this point in the history
  • Loading branch information
abidknashtech committed Oct 5, 2023
1 parent 0552567 commit a73c51e
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 9 deletions.
2 changes: 1 addition & 1 deletion inventory-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<properties>
<java.version>19</java.version>
<common.version>1.1.0</common.version>
<common.version>2.0</common.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<spring-cloud-gcp.version>4.1.1</spring-cloud-gcp.version>
<spring-cloud.version>2022.0.1</spring-cloud.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Object> handleOtherExceptions(Exception ex) {
ErrorMessage errorMessage = new ErrorMessage(new Date(), ex.getMessage());
return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
2 changes: 2 additions & 0 deletions local-dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<properties>
<java.version>19</java.version>
<project-reactor.version>3.5.9</project-reactor.version>
<common.version>1.1.0</common.version>
<common.version>2.0</common.version>
<validation-api.version>2.0.1.Final</validation-api.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,12 +41,20 @@ public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequ
.quantity(orderRequest.getQuantity()).orderId(orderId)
.build();

try (SubscriptionQueryResult<OrderSummary, OrderSummary> queryResult = queryGateway.subscriptionQuery(
/* try (SubscriptionQueryResult<OrderSummary, OrderSummary> 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();
}

}
2 changes: 1 addition & 1 deletion payment-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<properties>
<java.version>19</java.version>
<common.version>1.1.0</common.version>
<common.version>2.0</common.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion shipment-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<properties>
<java.version>19</java.version>
<common.version>1.1.0</common.version>
<common.version>2.0</common.version>
<spring-cloud-gcp.version>4.7.2</spring-cloud-gcp.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
</properties>
Expand Down

0 comments on commit a73c51e

Please sign in to comment.