1
1
package com .manir .springbootecommercerestapi .controller ;
2
2
3
- import com .manir .springbootecommercerestapi .exception .EcommerceApiException ;
4
3
import com .manir .springbootecommercerestapi .model .User ;
5
- import com .manir .springbootecommercerestapi .repository .UserRepository ;
6
4
import com .manir .springbootecommercerestapi .response .CartItemResponse ;
7
- import com .manir .springbootecommercerestapi .service .OrderService ;
5
+ import com .manir .springbootecommercerestapi .service .CommonService ;
8
6
import com .manir .springbootecommercerestapi .service .ShoppingCartService ;
9
7
import com .manir .springbootecommercerestapi .utils .isAuthenticatedAsAdminOrUser ;
10
8
import org .springframework .beans .factory .annotation .Autowired ;
11
9
import org .springframework .http .HttpStatus ;
12
10
import org .springframework .http .ResponseEntity ;
13
- import org .springframework .security .authentication .AnonymousAuthenticationToken ;
14
11
import org .springframework .security .core .Authentication ;
15
12
import org .springframework .security .core .annotation .AuthenticationPrincipal ;
16
- import org .springframework .security .core .context .SecurityContextHolder ;
17
- import org .springframework .security .core .userdetails .UsernameNotFoundException ;
18
13
import org .springframework .web .bind .annotation .*;
19
14
20
15
import javax .annotation .Resource ;
@@ -26,26 +21,16 @@ public class ShoppingCartController {
26
21
@ Resource
27
22
private ShoppingCartService shoppingCartService ;
28
23
@ Autowired
29
- private UserRepository userRepository ;
30
- @ Autowired
31
- private OrderService orderService ;
24
+ private CommonService commonService ;
32
25
33
26
//find by customer api
34
27
@ isAuthenticatedAsAdminOrUser
35
28
@ GetMapping ("/findByCustomer" )
36
29
public CartItemResponse findByCustomerId (@ AuthenticationPrincipal Authentication authentication ){
37
- authentication = SecurityContextHolder .getContext ().getAuthentication ();
38
- if (!(authentication instanceof AnonymousAuthenticationToken )) {
39
- String currentUserEmail = authentication .getName ();
40
- //System.out.println("Name:" + currentUserEmail);
41
- User customer = userRepository .findByEmail (currentUserEmail ).orElseThrow (()-> new UsernameNotFoundException ("Customer not found" ));
42
- CartItemResponse responseCartItems = shoppingCartService .findByCustomer (customer );
43
- return responseCartItems ;
44
-
45
- }else {
46
- throw new EcommerceApiException ("User not authenticated" , HttpStatus .BAD_REQUEST );
47
- }
48
30
31
+ User customer = commonService .getCurrentAuthenticatedUser (authentication );
32
+ CartItemResponse responseCartItems = shoppingCartService .findByCustomer (customer );
33
+ return responseCartItems ;
49
34
}
50
35
51
36
//add item to the cart api
@@ -54,15 +39,10 @@ public CartItemResponse findByCustomerId(@AuthenticationPrincipal Authentication
54
39
public ResponseEntity <CartItemResponse > addCartItem (@ AuthenticationPrincipal Authentication authentication ,
55
40
@ PathVariable Long productId ,
56
41
@ PathVariable Integer quantity ){
57
- authentication = SecurityContextHolder .getContext ().getAuthentication ();
58
- if (!(authentication instanceof AnonymousAuthenticationToken )){
59
- String currentUserEmail = authentication .getName ();
60
- User customer = userRepository .findByEmail (currentUserEmail ).orElseThrow (() -> new UsernameNotFoundException ("Customer not found" ));
61
- CartItemResponse responseCartItem = shoppingCartService .addCartItem (customer , productId , quantity );
62
- return new ResponseEntity <>(responseCartItem , HttpStatus .CREATED );
63
- }else {
64
- throw new EcommerceApiException ("User not authenticated" , HttpStatus .BAD_REQUEST );
65
- }
42
+
43
+ User customer = commonService .getCurrentAuthenticatedUser (authentication );
44
+ CartItemResponse responseCartItem = shoppingCartService .addCartItem (customer , productId , quantity );
45
+ return new ResponseEntity <>(responseCartItem , HttpStatus .CREATED );
66
46
}
67
47
68
48
//update item quantity api
@@ -71,31 +51,20 @@ public ResponseEntity<CartItemResponse> addCartItem(@AuthenticationPrincipal Aut
71
51
public ResponseEntity <CartItemResponse > updateItemQuantity (@ AuthenticationPrincipal Authentication authentication ,
72
52
@ PathVariable Long productId ,
73
53
@ PathVariable Integer quantity ){
74
- authentication = SecurityContextHolder .getContext ().getAuthentication ();
75
- if (!(authentication instanceof AnonymousAuthenticationToken )){
76
- String currentUserEmail = authentication .getName ();
77
- User customer = userRepository .findByEmail (currentUserEmail ).orElseThrow (() -> new UsernameNotFoundException ("Customer Not found" ));
78
- CartItemResponse responseCartItem = shoppingCartService .updateItemQuantity (customer , productId , quantity );
79
- return new ResponseEntity <>(responseCartItem , HttpStatus .OK );
80
- }else {
81
- throw new EcommerceApiException ("User not authenticated" , HttpStatus .BAD_REQUEST );
82
- }
54
+ User customer = commonService .getCurrentAuthenticatedUser (authentication );
55
+ CartItemResponse responseCartItem = shoppingCartService .updateItemQuantity (customer , productId , quantity );
56
+ return new ResponseEntity <>(responseCartItem , HttpStatus .OK );
83
57
}
84
58
85
59
//delete item product api
86
60
@ isAuthenticatedAsAdminOrUser
87
61
@ DeleteMapping ("/deleteItemProduct/{productId}" )
88
62
public ResponseEntity <String > deleteItemProduct (@ AuthenticationPrincipal Authentication authentication ,
89
63
@ PathVariable Long productId ){
90
- authentication = SecurityContextHolder .getContext ().getAuthentication ();
91
- if (!(authentication instanceof AnonymousAuthenticationToken )){
92
- String currentUserEmail = authentication .getName ();
93
- User customer = userRepository .findByEmail (currentUserEmail ).orElseThrow (() -> new UsernameNotFoundException ("Customer Not found" ));
94
- shoppingCartService .deleteItemProduct (customer , productId );
95
- return ResponseEntity .ok ("Product with id = " + productId +" is deleted successfully from your shopping cart" );
96
- }else {
97
- throw new EcommerceApiException ("User not authenticated" , HttpStatus .BAD_REQUEST );
98
- }
64
+
65
+ User customer = commonService .getCurrentAuthenticatedUser (authentication );
66
+ shoppingCartService .deleteItemProduct (customer , productId );
67
+ return ResponseEntity .ok ("Product with id = " + productId +" is deleted successfully from your shopping cart" );
99
68
}
100
69
101
70
0 commit comments