-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartServiceImpl.java
More file actions
70 lines (62 loc) · 2.54 KB
/
CartServiceImpl.java
File metadata and controls
70 lines (62 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.ecommerece.userinterface.external.services.impl;
import com.ecommerece.userinterface.external.dtos.GetProductDto;
import com.ecommerece.userinterface.external.models.Cart;
import com.ecommerece.userinterface.external.models.CartItem;
import com.ecommerece.userinterface.external.services.ApiResponse;
import com.ecommerece.userinterface.external.services.CartService;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
@Service
public class CartServiceImpl implements CartService {
private final ApiResponse apiResponse;
public CartServiceImpl(ApiResponse apiResponse) {
this.apiResponse = apiResponse;
}
@Override
public void addToCart(CartItem cartItem, HttpServletRequest request) {
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("CART_SESSION");
if (cart == null) {
cart = new Cart(session.getId());
}
List<CartItem> cartItems = cart.getCartItems();
cartItems.removeIf(item -> Objects.equals(item.getProductId(), cartItem.getProductId()));
cartItems.add(new CartItem(cartItem.getProductId(), cartItem.getQuantity()));
cart.setCartItems(cartItems);
session.setAttribute("CART_SESSION", cart);
}
@Override
public void removeFromCart(String productId, HttpSession session) {
Cart cart = (Cart) session.getAttribute("CART_SESSION");
if (cart == null) {
cart = new Cart(session.getId());
}
List<CartItem> cartItems = cart.getCartItems();
cartItems.removeIf(item -> Objects.equals(item.getProductId(), productId));
}
@Override
public HashMap<GetProductDto, Integer> getCartItems(HttpSession session) {
Cart cart = (Cart) session.getAttribute("CART_SESSION");
if (cart == null) {
cart = new Cart(session.getId());
}
HashMap<GetProductDto, Integer> products = new HashMap<>();
List<CartItem> cartItems= cart.getCartItems();
for (CartItem cartItem: cartItems) {
products.put(apiResponse.getProduct(cartItem.getProductId()), cartItem.getQuantity());
}
return products;
}
@Override
public Cart getCart(HttpSession session) {
Cart cart = (Cart) session.getAttribute("CART_SESSION");
if (cart == null) {
cart = new Cart(session.getId());
}
return cart;
}
}