1
1
package com .manir .springbootecommercerestapi .service .Impl ;
2
2
3
3
import com .manir .springbootecommercerestapi .dto .CartItemDto ;
4
+ import com .manir .springbootecommercerestapi .exception .EcommerceApiException ;
4
5
import com .manir .springbootecommercerestapi .exception .ResourceNotFoundException ;
5
6
import com .manir .springbootecommercerestapi .model .CartItem ;
6
7
import com .manir .springbootecommercerestapi .model .Customer ;
11
12
import com .manir .springbootecommercerestapi .service .ShoppingCartService ;
12
13
import lombok .extern .slf4j .Slf4j ;
13
14
import org .modelmapper .ModelMapper ;
15
+ import org .springframework .http .HttpStatus ;
14
16
import org .springframework .stereotype .Service ;
15
17
16
18
import javax .annotation .Resource ;
19
+ import javax .transaction .Transactional ;
17
20
import java .util .List ;
18
21
import java .util .stream .Collectors ;
19
22
20
23
@ Service
21
24
@ Slf4j
25
+
22
26
public class ShoppingCartServiceImpl implements ShoppingCartService {
23
27
24
28
@ Resource
@@ -32,7 +36,13 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
32
36
33
37
@ Override
34
38
public List <CartItemDto > findByCustomerId (Long customerId ) {
39
+
35
40
List <CartItem > cartItems = cartItemRepository .findByCustomerId (customerId );
41
+
42
+ if (cartItems .size () == 0 ){
43
+ throw new EcommerceApiException ("Customer has no product in cart item" , HttpStatus .BAD_REQUEST );
44
+ }
45
+
36
46
List <CartItemDto > cartItemDtoList = cartItems .stream ()
37
47
.map (cartItem -> mapToDto (cartItem ))
38
48
.collect (Collectors .toList ());
@@ -49,7 +59,7 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
49
59
);
50
60
Product product = productRepository .findById (productId )
51
61
.orElseThrow (
52
- () -> new ResourceNotFoundException ("Customer " , customerId )
62
+ () -> new ResourceNotFoundException ("Product " , productId )
53
63
);
54
64
55
65
CartItem cartItem = cartItemRepository .findByCustomerIdAndProductId (customerId , productId );
@@ -68,6 +78,47 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
68
78
return mapToDto (addedCartItem );
69
79
}
70
80
81
+ @ Override
82
+ public CartItemDto updateItemQuantity (Long customerId , Long productId , Integer quantity ) {
83
+
84
+ Customer customer = customerRepository .findById (customerId )
85
+ .orElseThrow (
86
+ () -> new ResourceNotFoundException ("Customer" , customerId )
87
+ );
88
+ Product product = productRepository .findById (productId )
89
+ .orElseThrow (
90
+ () -> new ResourceNotFoundException ("Product" , productId )
91
+ );
92
+
93
+ CartItem cartItem = cartItemRepository .findByCustomerIdAndProductId (customerId , productId );
94
+ if (!cartItem .getCustomer ().getId ().equals (customer .getId ()) || !cartItem .getProduct ().getId ().equals (product .getId ())){
95
+ throw new EcommerceApiException (" this cart item does not belong to Customer or Product " , HttpStatus .BAD_REQUEST );
96
+ }
97
+ cartItem .setQuantity (quantity );
98
+ CartItem updatedItemQuantity = cartItemRepository .save (cartItem );
99
+ return mapToDto (updatedItemQuantity );
100
+ }
101
+
102
+ @ Override
103
+ @ Transactional
104
+ public void deleteItemProduct (Long customerId , Long productId ) {
105
+ Customer customer = customerRepository .findById (customerId )
106
+ .orElseThrow (
107
+ () -> new ResourceNotFoundException ("Customer" , customerId )
108
+ );
109
+ Product product = productRepository .findById (productId )
110
+ .orElseThrow (
111
+ () -> new ResourceNotFoundException ("Product" , productId )
112
+ );
113
+ CartItem cartItem = cartItemRepository .findByCustomerIdAndProductId (customerId , productId );
114
+ if (cartItem != null ){
115
+ cartItemRepository .deleteByCustomerIdAndProductId (customerId , productId );
116
+ }
117
+ if (!cartItem .getCustomer ().getId ().equals (customer .getId ()) || !cartItem .getProduct ().getId ().equals (product .getId ())){
118
+ throw new EcommerceApiException (" this cart item does not belong to Customer or Product " , HttpStatus .BAD_REQUEST );
119
+ }
120
+ }
121
+
71
122
//map to dto
72
123
private CartItemDto mapToDto (CartItem cartItem ){
73
124
CartItemDto cartItemDto = modelMapper .map (cartItem , CartItemDto .class );
0 commit comments