Skip to content

Commit

Permalink
Merge pull request #36 from NashTech-Labs/feature/added_cart_products…
Browse files Browse the repository at this point in the history
…_visibility

added cart product visibility
  • Loading branch information
abidknashtech authored Oct 6, 2023
2 parents 1e869aa + ada82e0 commit d7433ec
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 80 deletions.
1 change: 0 additions & 1 deletion cart-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<java.version>19</java.version>
<maven.compiler.source>19</maven.compiler.source>
<mysql-connector.version>8.0.33</mysql-connector.version>
<common.version>1.0</common.version>
<dotenv.version>4.0.0</dotenv.version>
</properties>

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

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +21,14 @@ public class CartItem extends Auditable<String> 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;

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

}

This file was deleted.

1 change: 1 addition & 0 deletions cart-service/src/main/resources/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ MYSQL_HOST=localhost
MYSQL_DB_USERNAME=root
MYSQL_DB_PASSWORD=password
SERVICE_NAME=CartService
INVENTORY_HOST=http://localhost:9091
6 changes: 5 additions & 1 deletion cart-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ spring:
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: update
ddl-auto: update

inventory-service:
host: ${INVENTORY_HOST}
product-uri: /products/product/{productId}

This file was deleted.

0 comments on commit d7433ec

Please sign in to comment.