Skip to content

Commit

Permalink
feat: cart and cart item services
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Dec 25, 2024
1 parent 4ab6a04 commit d239ed8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public class CartItem {
@JoinColumn(name = "cart_id")
private Cart cart;

public void setToPrice() {
/**
* Custom Set Total Price Method
*/
public void setTotalPrice() {
this.totalPrice = this.unitPrice.multiply(new BigDecimal(quantity));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,86 @@
package com.thutasann.shopping_cart.service.cart;

import java.math.BigDecimal;

import org.springframework.stereotype.Service;

import com.thutasann.shopping_cart.exceptions.ResourceNotFoundException;
import com.thutasann.shopping_cart.model.Cart;
import com.thutasann.shopping_cart.model.CartItem;
import com.thutasann.shopping_cart.model.Product;
import com.thutasann.shopping_cart.repository.CartItemRepository;
import com.thutasann.shopping_cart.repository.CartRepository;
import com.thutasann.shopping_cart.service.product.IProductService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CartItemService implements ICartItemService {
private final CartItemRepository cartItemRepository;
private final CartRepository cartRepository;
private final IProductService productService;
private final ICartService cartService;

@Override
public void AddItemToCart(Long cartId, Long productId, int quantity) {
throw new UnsupportedOperationException("Unimplemented method 'AddItemToCart'");
Cart cart = cartService.getCart(cartId);
Product product = productService.getProductById(productId);
CartItem cartItem = cart.getItems()
.stream()
.filter(item -> item.getProduct().getId().equals(productId))
.findFirst().orElse(new CartItem());

if (cartItem.getId() == null) {
cartItem.setCart(cart);
cartItem.setProduct(product);
cartItem.setQuantity(quantity);
cartItem.setUnitPrice(product.getPrice());
} else {
cartItem.setQuantity(cartItem.getQuantity() + quantity);
}

cartItem.setTotalPrice();
cart.addItem(cartItem);
cartItemRepository.save(cartItem);
cartRepository.save(cart);
}

@Override
public void removeItemFromCart(Long cartId, Long productId) {
throw new UnsupportedOperationException("Unimplemented method 'removeItemFromCart'");
Cart cart = cartService.getCart(cartId);
CartItem cartItem = this.getCartItem(cartId, productId);
cart.removeItem(cartItem);
cartRepository.save(cart);
}

@Override
public void updateItemQuantity(Long cartId, Long productId, int quantity) {
throw new UnsupportedOperationException("Unimplemented method 'updateItemQuantity'");
Cart cart = cartService.getCart(cartId);
cart.getItems()
.stream()
.filter(item -> item.getProduct().getId().equals(productId))
.findFirst()
.ifPresent(item -> {
item.setQuantity(quantity);
item.setUnitPrice(item.getProduct().getPrice());
item.setTotalPrice();
});
BigDecimal totalAmount = cart.getItems()
.stream().map(CartItem::getTotalPrice)
.reduce(BigDecimal.ZERO, BigDecimal::add);
cart.setTotalAmount(totalAmount);
cartRepository.save(cart);
}

@Override
public CartItem getCartItem(Long cartId, Long productId) {
throw new UnsupportedOperationException("Unimplemented method 'getCartItem'");
Cart cart = cartService.getCart(cartId);
return cart.getItems()
.stream()
.filter(item -> item.getProduct().getId().equals(productId))
.findFirst()
.orElseThrow(() -> new ResourceNotFoundException("Cart Item not found"));
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,60 @@
package com.thutasann.shopping_cart.service.cart;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Service;

import com.thutasann.shopping_cart.model.CartItem;
import com.thutasann.shopping_cart.exceptions.ResourceNotFoundException;
import com.thutasann.shopping_cart.model.Cart;
import com.thutasann.shopping_cart.repository.CartItemRepository;
import com.thutasann.shopping_cart.repository.CartRepository;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CartService implements ICartItemService {
public class CartService implements ICartService {

private final CartRepository cartRepository;
private final CartItemRepository cartItemRepository;
private final AtomicLong cartIdGenerator = new AtomicLong(0);

@Override
public Cart getCart(Long id) {
Cart cart = cartRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Cart not found"));
BigDecimal totalAmount = cart.getTotalAmount();
cart.setTotalAmount(totalAmount);
return cart;
}

@Transactional
@Override
public void AddItemToCart(Long cartId, Long productId, int quantity) {
throw new UnsupportedOperationException("Unimplemented method 'AddItemToCart'");
public void clearCart(Long id) {
Cart cart = this.getCart(id);
cartItemRepository.deleteAllByCartId(id);
cart.getItems().clear();
cartRepository.deleteById(id);
}

@Override
public void removeItemFromCart(Long cartId, Long productId) {
throw new UnsupportedOperationException("Unimplemented method 'removeItemFromCart'");
public BigDecimal getTotalPrice(Long id) {
Cart cart = this.getCart(id);
return cart.getTotalAmount();
}

@Override
public void updateItemQuantity(Long cartId, Long productId, int quantity) {
throw new UnsupportedOperationException("Unimplemented method 'updateItemQuantity'");
public Long initializeNewCart() {
Cart newCart = new Cart();
Long newCartId = cartIdGenerator.incrementAndGet();
newCart.setId(newCartId);
return cartRepository.save(newCart).getId();
}

@Override
public CartItem getCartItem(Long cartId, Long productId) {
throw new UnsupportedOperationException("Unimplemented method 'getCartItem'");
public Cart getCartByUserId(Long userId) {
return cartRepository.findByUserId(userId);
}

}

0 comments on commit d239ed8

Please sign in to comment.