source: src/main/java/com/example/salonbella/service/ShoppingCartService.java@ e9b70f6

Last change on this file since e9b70f6 was e9b70f6, checked in by makyjovanovsky <mjovanovski04@…>, 17 months ago

order

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package com.example.salonbella.service;
2
3import com.example.salonbella.controller.cart.ShoppingCart;
4import com.example.salonbella.entity.CartDetailEntity;
5import com.example.salonbella.entity.ProductEntity;
6import com.example.salonbella.repository.ProductRepository;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.stereotype.Service;
9
10import java.util.ArrayList;
11import java.util.Base64;
12import java.util.List;
13
14@Service
15public 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}
Note: See TracBrowser for help on using the repository browser.