-
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.
- Loading branch information
Showing
3 changed files
with
100 additions
and
15 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
62 changes: 58 additions & 4 deletions
62
shopping_cart/src/main/java/com/thutasann/shopping_cart/service/cart/CartItemService.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 |
---|---|---|
@@ -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")); | ||
} | ||
|
||
} |
48 changes: 38 additions & 10 deletions
48
shopping_cart/src/main/java/com/thutasann/shopping_cart/service/cart/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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |