From b1642947f644c3b5a2fae7e54af7950d497108d9 Mon Sep 17 00:00:00 2001 From: abidknashtech Date: Fri, 6 Oct 2023 12:55:21 +0530 Subject: [PATCH 1/2] added cart product visibility --- cart-service/pom.xml | 1 - .../car/cart/config/Configurations.java | 14 +++ .../car/cart/config/ProductConfigs.java | 15 ++++ .../car/cart/controller/CartController.java | 8 +- .../car/cart/data/ProductsSummary.java | 17 ++++ .../com/nashtech/car/cart/model/CartItem.java | 15 +++- .../car/cart/repository/ProductEntity.java | 27 ++++++ .../cart/repository/ProductsRepository.java | 8 ++ .../car/cart/service/CartService.java | 87 +++++++++++++++++++ .../car/cart/services/CartService.java | 57 ------------ cart-service/src/main/resources/.env | 1 + .../src/main/resources/application.yml | 6 +- .../inventory/restapi/ProductRequest.java | 17 ---- 13 files changed, 193 insertions(+), 80 deletions(-) create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/config/Configurations.java create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/config/ProductConfigs.java create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/data/ProductsSummary.java create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/service/CartService.java delete mode 100644 cart-service/src/main/java/com/nashtech/car/cart/services/CartService.java delete mode 100644 inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java diff --git a/cart-service/pom.xml b/cart-service/pom.xml index 9ddca2e2..4e63bd1e 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -43,7 +43,6 @@ 19 19 8.0.33 - 1.0 4.0.0 diff --git a/cart-service/src/main/java/com/nashtech/car/cart/config/Configurations.java b/cart-service/src/main/java/com/nashtech/car/cart/config/Configurations.java new file mode 100644 index 00000000..1dcf0571 --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/config/Configurations.java @@ -0,0 +1,14 @@ +package com.nashtech.car.cart.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class Configurations { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/config/ProductConfigs.java b/cart-service/src/main/java/com/nashtech/car/cart/config/ProductConfigs.java new file mode 100644 index 00000000..6bc3ca3e --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/config/ProductConfigs.java @@ -0,0 +1,15 @@ +package com.nashtech.car.cart.config; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@ConfigurationProperties(prefix ="inventory-service") +@Component +@Getter +@Setter +public class ProductConfigs { + private String host; + private String productUri; +} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/controller/CartController.java b/cart-service/src/main/java/com/nashtech/car/cart/controller/CartController.java index bf29bb45..79b86baa 100644 --- a/cart-service/src/main/java/com/nashtech/car/cart/controller/CartController.java +++ b/cart-service/src/main/java/com/nashtech/car/cart/controller/CartController.java @@ -1,12 +1,16 @@ package com.nashtech.car.cart.controller; import com.nashtech.car.cart.model.CartItem; -import com.nashtech.car.cart.services.CartService; +import com.nashtech.car.cart.service.CartService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/cart") diff --git a/cart-service/src/main/java/com/nashtech/car/cart/data/ProductsSummary.java b/cart-service/src/main/java/com/nashtech/car/cart/data/ProductsSummary.java new file mode 100644 index 00000000..678288c1 --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/data/ProductsSummary.java @@ -0,0 +1,17 @@ +package com.nashtech.car.cart.data; + +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 = 0.0d; + private Double basePrice = 0.0d; + private Integer quantity =0; + private Float tax = 0.0f; + +} \ No newline at end of file diff --git a/cart-service/src/main/java/com/nashtech/car/cart/model/CartItem.java b/cart-service/src/main/java/com/nashtech/car/cart/model/CartItem.java index 28eef784..652694a0 100644 --- a/cart-service/src/main/java/com/nashtech/car/cart/model/CartItem.java +++ b/cart-service/src/main/java/com/nashtech/car/cart/model/CartItem.java @@ -1,7 +1,11 @@ package com.nashtech.car.cart.model; import com.nashtech.car.cart.audit.Auditable; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; import lombok.Getter; import lombok.Setter; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -17,7 +21,14 @@ public class CartItem extends Auditable implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String productId; - private int quantity; + private String brand; + private String model; + private Integer year; + private String color; + private Double mileage; + private Double basePrice; + private Integer quantity; + private Float tax; private String userId; } diff --git a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java new file mode 100644 index 00000000..931a313f --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java @@ -0,0 +1,27 @@ +package com.nashtech.car.cart.repository; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Data; + +import java.util.Date; + + +@Entity +@Table(name="products") +@Data +public class ProductEntity { + @Id + 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; + private Date timestamp = new Date(); + +} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java new file mode 100644 index 00000000..874c773b --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java @@ -0,0 +1,8 @@ +package com.nashtech.car.cart.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProductsRepository extends JpaRepository { + ProductEntity findByProductId(String productId); + +} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/service/CartService.java b/cart-service/src/main/java/com/nashtech/car/cart/service/CartService.java new file mode 100644 index 00000000..69d60e90 --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/service/CartService.java @@ -0,0 +1,87 @@ +package com.nashtech.car.cart.service; + +import com.nashtech.car.cart.config.ProductConfigs; +import com.nashtech.car.cart.data.ProductsSummary; +import com.nashtech.car.cart.model.CartItem; +import com.nashtech.car.cart.repository.CartItemRepository; +import jakarta.transaction.Transactional; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.Objects; + +@Service +@Slf4j +public class CartService { + + private final CartItemRepository cartItemRepository; + + private final RestTemplate apiCall; + + private final ProductConfigs productConfigs; + + public CartService(CartItemRepository cartItemRepository, RestTemplate apiCall, ProductConfigs productConfigs) { + this.cartItemRepository = cartItemRepository; + this.apiCall = apiCall; + this.productConfigs = productConfigs; + } + + // @Transactional + public CartItem addToCart(String productId, int quantity, String userId) { + + CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId, userId); + + if (Objects.isNull(cartItem)) { + ProductsSummary productsSummary; + try { + productsSummary = apiCall.getForEntity(productConfigs.getHost() + productConfigs.getProductUri(), + ProductsSummary.class, productId).getBody(); + } catch (Exception ex) { + log.error("Unable to process add to cart due to {} ", ex.getMessage()); + throw ex; + } + cartItem = new CartItem(); + cartItem.setProductId(productId); + cartItem.setQuantity(quantity); + cartItem.setUserId(userId); + cartItem.setBasePrice(productsSummary.getBasePrice()); + cartItem.setMileage(productsSummary.getMileage()); + cartItem.setModel(productsSummary.getModel()); + cartItem.setColor(productsSummary.getColor()); + cartItem.setBrand(productsSummary.getBrand()); + cartItem.setTax(productsSummary.getTax()); + cartItem.setYear(productsSummary.getYear()); + } else { + cartItem.setQuantity(cartItem.getQuantity() + quantity); + } + + return cartItemRepository.save(cartItem); + } + + public CartItem removeFromCart(String productId, int quantity, String userId) { + CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId, userId); + + if (cartItem != null) { + int updatedQuantity = cartItem.getQuantity() - quantity; + if (updatedQuantity <= 0) { + cartItemRepository.delete(cartItem); + throw new IllegalStateException("Product removed from cart"); + } else { + cartItem.setQuantity(updatedQuantity); + cartItem = cartItemRepository.save(cartItem); + } + } + return cartItem; + } + + @Transactional + public CartItem getFromCart(String productId, String userId) { + CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId, userId); + if (cartItem == null) { + throw new IllegalStateException("Product not found from the cart"); + } + return cartItem; + } + +} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/services/CartService.java b/cart-service/src/main/java/com/nashtech/car/cart/services/CartService.java deleted file mode 100644 index 16521faf..00000000 --- a/cart-service/src/main/java/com/nashtech/car/cart/services/CartService.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.nashtech.car.cart.services; - -import com.nashtech.car.cart.model.CartItem; -import com.nashtech.car.cart.repository.CartItemRepository; -import jakarta.transaction.Transactional; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -@Slf4j -public class CartService { - @Autowired - private CartItemRepository cartItemRepository; - - // @Transactional - public CartItem addToCart(String productId, int quantity , String userId) { - CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId,userId); - - if (cartItem == null) { - cartItem = new CartItem(); - cartItem.setProductId(productId); - cartItem.setQuantity(quantity); - cartItem.setUserId(userId); - } else { - cartItem.setQuantity(cartItem.getQuantity() + quantity); - } - - return cartItemRepository.save(cartItem); - } - - public CartItem removeFromCart(String productId, int quantity,String userId) { - CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId,userId); - - if (cartItem != null) { - int updatedQuantity = cartItem.getQuantity() - quantity; - if (updatedQuantity <= 0) { - cartItemRepository.delete(cartItem); - throw new IllegalStateException("Product removed from cart"); - } else { - cartItem.setQuantity(updatedQuantity); - cartItem= cartItemRepository.save(cartItem); - } - } - return cartItem; - } - - @Transactional - public CartItem getFromCart(String productId, String userId) { - CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId,userId); - if (cartItem == null) { - throw new IllegalStateException("Product not found from the cart"); - } - return cartItem; - } - -} diff --git a/cart-service/src/main/resources/.env b/cart-service/src/main/resources/.env index 3eb9914c..a795318c 100644 --- a/cart-service/src/main/resources/.env +++ b/cart-service/src/main/resources/.env @@ -2,3 +2,4 @@ MYSQL_HOST=localhost MYSQL_DB_USERNAME=root MYSQL_DB_PASSWORD=password SERVICE_NAME=CartService +INVENTORY_HOST=http://localhost:9091 \ No newline at end of file diff --git a/cart-service/src/main/resources/application.yml b/cart-service/src/main/resources/application.yml index dcc784e6..f903837e 100644 --- a/cart-service/src/main/resources/application.yml +++ b/cart-service/src/main/resources/application.yml @@ -11,4 +11,8 @@ spring: jpa: database-platform: org.hibernate.dialect.MySQL8Dialect hibernate: - ddl-auto: update \ No newline at end of file + ddl-auto: update + +inventory-service: + host: ${INVENTORY_HOST} + product-uri: /products/product/{productId} \ No newline at end of file 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 deleted file mode 100644 index 6964cd96..00000000 --- a/inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java +++ /dev/null @@ -1,17 +0,0 @@ -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; -} From ada82e017e57fe35154c52945be531966f0a0141 Mon Sep 17 00:00:00 2001 From: abidknashtech Date: Fri, 6 Oct 2023 12:58:42 +0530 Subject: [PATCH 2/2] removed unused repo --- .../car/cart/repository/ProductEntity.java | 27 ------------------- .../cart/repository/ProductsRepository.java | 8 ------ 2 files changed, 35 deletions(-) delete mode 100644 cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java delete mode 100644 cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java diff --git a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java deleted file mode 100644 index 931a313f..00000000 --- a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductEntity.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.nashtech.car.cart.repository; - -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.Data; - -import java.util.Date; - - -@Entity -@Table(name="products") -@Data -public class ProductEntity { - @Id - 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; - private Date timestamp = new Date(); - -} diff --git a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java b/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java deleted file mode 100644 index 874c773b..00000000 --- a/cart-service/src/main/java/com/nashtech/car/cart/repository/ProductsRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.nashtech.car.cart.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -public interface ProductsRepository extends JpaRepository { - ProductEntity findByProductId(String productId); - -}