[e9b70f6] | 1 | package com.example.salonbella.service;
|
---|
| 2 |
|
---|
| 3 | import com.example.salonbella.controller.cart.ShoppingCart;
|
---|
| 4 | import com.example.salonbella.entity.CartDetailEntity;
|
---|
| 5 | import com.example.salonbella.entity.ProductEntity;
|
---|
| 6 | import com.example.salonbella.repository.ProductRepository;
|
---|
| 7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 8 | import org.springframework.stereotype.Service;
|
---|
| 9 |
|
---|
| 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.Base64;
|
---|
| 12 | import java.util.List;
|
---|
| 13 |
|
---|
| 14 | @Service
|
---|
| 15 | public class ShoppingCartService {
|
---|
| 16 |
|
---|
| 17 | private ShoppingCart shoppingCart;
|
---|
| 18 | private ProductRepository productRepository;
|
---|
| 19 |
|
---|
| 20 | @Autowired
|
---|
| 21 | public ShoppingCartService(ShoppingCart shoppingCart, ProductRepository productRepository) {
|
---|
| 22 | this.shoppingCart = shoppingCart;
|
---|
| 23 | this.productRepository = productRepository;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public void addProduct(Long id) {
|
---|
| 27 | shoppingCart.getCartDetails().add(new CartDetailEntity(id, 1));
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public ShoppingCart getShoppingCart() {
|
---|
| 31 | return shoppingCart;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public List<ProductEntity> getShoppingCartDetails() {
|
---|
| 35 | List<ProductEntity> productEntities = new ArrayList<>();
|
---|
| 36 | List<CartDetailEntity> cartDetailEntities = shoppingCart.getCartDetails();
|
---|
| 37 | for (CartDetailEntity c : cartDetailEntities) {
|
---|
| 38 | ProductEntity p = productRepository.findById(c.getId()).get();
|
---|
| 39 | p.setBase64(Base64.getEncoder().encodeToString(p.getContent()));
|
---|
| 40 | productEntities.add(p);
|
---|
| 41 | }
|
---|
| 42 | return productEntities;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public void deleteProductFromCart(Long id) {
|
---|
| 46 | for (CartDetailEntity c : shoppingCart.getCartDetails()) {
|
---|
| 47 | if (c.getId() == id) {
|
---|
| 48 | shoppingCart.getCartDetails().remove(c);
|
---|
| 49 | break;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public boolean existInCart(Long id) {
|
---|
| 55 | for (CartDetailEntity c : shoppingCart.getCartDetails()) {
|
---|
| 56 | if (c.getId() == id) {
|
---|
| 57 | return true;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | return false;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|