-
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 #36 from NashTech-Labs/feature/added_cart_products…
…_visibility added cart product visibility
- Loading branch information
Showing
11 changed files
with
158 additions
and
80 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
14 changes: 14 additions & 0 deletions
14
cart-service/src/main/java/com/nashtech/car/cart/config/Configurations.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,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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
cart-service/src/main/java/com/nashtech/car/cart/config/ProductConfigs.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,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; | ||
} |
8 changes: 6 additions & 2 deletions
8
cart-service/src/main/java/com/nashtech/car/cart/controller/CartController.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
17 changes: 17 additions & 0 deletions
17
cart-service/src/main/java/com/nashtech/car/cart/data/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.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; | ||
|
||
} |
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
87 changes: 87 additions & 0 deletions
87
cart-service/src/main/java/com/nashtech/car/cart/service/CartService.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,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; | ||
} | ||
|
||
} |
57 changes: 0 additions & 57 deletions
57
cart-service/src/main/java/com/nashtech/car/cart/services/CartService.java
This file was deleted.
Oops, something went wrong.
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
17 changes: 0 additions & 17 deletions
17
inventory-service/src/main/java/com/nashtech/inventory/restapi/ProductRequest.java
This file was deleted.
Oops, something went wrong.