-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from NashTech-Labs/feature/added_car_api_for_cart
added get product api
- Loading branch information
Showing
14 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
inventory-service/src/main/java/com/nashtech/inventory/exception/ProductNotFound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
inventory-service/src/main/java/com/nashtech/inventory/exception/ProductsErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
inventory-service/src/main/java/com/nashtech/inventory/query/FindProductsQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
32 changes: 32 additions & 0 deletions
32
inventory-service/src/main/java/com/nashtech/inventory/query/ProductsQueryHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
inventory-service/src/main/java/com/nashtech/inventory/query/ProductsSummary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters