9
9
import com .manir .springbootecommercerestapi .repository .CartItemRepository ;
10
10
import com .manir .springbootecommercerestapi .repository .CustomerRepository ;
11
11
import com .manir .springbootecommercerestapi .repository .ProductRepository ;
12
+ import com .manir .springbootecommercerestapi .response .CartItemResponse ;
13
+ import com .manir .springbootecommercerestapi .service .CommonService ;
12
14
import com .manir .springbootecommercerestapi .service .ShoppingCartService ;
13
15
import lombok .extern .slf4j .Slf4j ;
14
16
import org .modelmapper .ModelMapper ;
17
19
18
20
import javax .annotation .Resource ;
19
21
import javax .transaction .Transactional ;
22
+ import java .util .ArrayList ;
20
23
import java .util .List ;
21
24
import java .util .stream .Collectors ;
25
+ import java .util .stream .DoubleStream ;
22
26
23
27
@ Service
24
28
@ Slf4j
25
-
26
29
public class ShoppingCartServiceImpl implements ShoppingCartService {
27
30
28
31
@ Resource
@@ -33,9 +36,10 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
33
36
private ProductRepository productRepository ;
34
37
@ Resource
35
38
private CustomerRepository customerRepository ;
36
-
39
+ @ Resource
40
+ private CommonService commonService ;
37
41
@ Override
38
- public List < CartItemDto > findByCustomerId (Long customerId ) {
42
+ public CartItemResponse findByCustomerId (Long customerId ) {
39
43
40
44
List <CartItem > cartItems = cartItemRepository .findByCustomerId (customerId );
41
45
@@ -46,21 +50,19 @@ public List<CartItemDto> findByCustomerId(Long customerId) {
46
50
List <CartItemDto > cartItemDtoList = cartItems .stream ()
47
51
.map (cartItem -> mapToDto (cartItem ))
48
52
.collect (Collectors .toList ());
49
-
50
- return cartItemDtoList ;
53
+ DoubleStream totalPrice = cartItemDtoList .stream ()
54
+ .mapToDouble (cartItemDto -> cartItemDto .getProduct ().getPrice () * cartItemDto .getQuantity ());
55
+ CartItemResponse cartItemResponse = new CartItemResponse ();
56
+ cartItemResponse .setContent (cartItemDtoList );
57
+ cartItemResponse .setTotalCost (totalPrice .sum ());
58
+ return cartItemResponse ;
51
59
}
52
60
53
61
@ Override
54
- public CartItemDto addCartItem (Long customerId , Long productId , Integer quantity ) {
62
+ public CartItemResponse addCartItem (Long customerId , Long productId , Integer quantity ) {
55
63
Integer addedQuantity = quantity ;
56
- Customer customer = customerRepository .findById (customerId )
57
- .orElseThrow (
58
- () -> new ResourceNotFoundException ("Customer" , customerId )
59
- );
60
- Product product = productRepository .findById (productId )
61
- .orElseThrow (
62
- () -> new ResourceNotFoundException ("Product" , productId )
63
- );
64
+ Customer customer = findCustomerById (customerId );
65
+ Product product = findProductById (productId );
64
66
65
67
CartItem cartItem = cartItemRepository .findByCustomerIdAndProductId (customerId , productId );
66
68
if (cartItem != null ){
@@ -74,49 +76,36 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
74
76
}
75
77
76
78
CartItem addedCartItem = cartItemRepository .save (cartItem );
79
+ CartItemDto cartItemDto = mapToDto (addedCartItem );
77
80
78
- return mapToDto (addedCartItem );
81
+ CartItemResponse cartItemResponse = commonService .getResponse (cartItemDto );
82
+ return cartItemResponse ;
79
83
}
80
84
81
85
@ 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
- );
86
+ public CartItemResponse updateItemQuantity (Long customerId , Long productId , Integer quantity ) {
92
87
93
88
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 );
89
+ if (cartItem == null ){
90
+ throw new EcommerceApiException ("Product is not in the cart item " , HttpStatus .BAD_REQUEST );
96
91
}
97
92
cartItem .setQuantity (quantity );
98
93
CartItem updatedItemQuantity = cartItemRepository .save (cartItem );
99
- return mapToDto (updatedItemQuantity );
94
+ CartItemDto cartItemDto = mapToDto (updatedItemQuantity );
95
+
96
+ CartItemResponse cartItemResponse = commonService .getResponse (cartItemDto );
97
+ return cartItemResponse ;
100
98
}
101
99
102
100
@ Override
103
101
@ Transactional
104
102
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
- );
103
+
113
104
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 );
105
+ if (cartItem == null ){
106
+ throw new EcommerceApiException ("Product is not in the cart item" , HttpStatus .BAD_REQUEST );
119
107
}
108
+ cartItemRepository .deleteByCustomerIdAndProductId (customerId , productId );
120
109
}
121
110
122
111
//map to dto
@@ -130,4 +119,20 @@ private CartItem mapToEntity(CartItemDto cartItemDto){
130
119
CartItem cartItem = modelMapper .map (cartItemDto , CartItem .class );
131
120
return cartItem ;
132
121
}
122
+
123
+ private Customer findCustomerById (Long customerId ){
124
+ Customer customer = customerRepository .findById (customerId )
125
+ .orElseThrow (
126
+ () -> new ResourceNotFoundException ("Customer" , customerId )
127
+ );
128
+ return customer ;
129
+ }
130
+
131
+ private Product findProductById (Long productId ){
132
+ Product product = productRepository .findById (productId )
133
+ .orElseThrow (
134
+ () -> new ResourceNotFoundException ("Product" , productId )
135
+ );
136
+ return product ;
137
+ }
133
138
}
0 commit comments