1
1
package JEstebanC .FastFoodApp .controller ;
2
2
3
3
import java .time .Instant ;
4
- import java .util .Collection ;
5
4
import java .util .Map ;
6
5
7
6
import javax .validation .Valid ;
11
10
import org .springframework .http .ResponseEntity ;
12
11
import org .springframework .security .access .prepost .PreAuthorize ;
13
12
import org .springframework .web .bind .annotation .DeleteMapping ;
14
- import org .springframework .web .bind .annotation .GetMapping ;
15
13
import org .springframework .web .bind .annotation .PathVariable ;
16
14
import org .springframework .web .bind .annotation .PostMapping ;
17
15
import org .springframework .web .bind .annotation .PutMapping ;
18
16
import org .springframework .web .bind .annotation .RequestBody ;
19
17
import org .springframework .web .bind .annotation .RequestMapping ;
20
18
import org .springframework .web .bind .annotation .RestController ;
21
19
22
- import JEstebanC .FastFoodApp .model .Additional ;
20
+ import JEstebanC .FastFoodApp .dto .UserBillOrdersDTO ;
21
+ import JEstebanC .FastFoodApp .enumeration .StatusBill ;
23
22
import JEstebanC .FastFoodApp .model .Orders ;
24
23
import JEstebanC .FastFoodApp .model .Response ;
24
+ import JEstebanC .FastFoodApp .service .BillServiceImp ;
25
25
import JEstebanC .FastFoodApp .service .OrdersServiceImp ;
26
26
import lombok .RequiredArgsConstructor ;
27
27
@@ -37,58 +37,62 @@ public class OrdersController {
37
37
38
38
@ Autowired
39
39
private final OrdersServiceImp serviceImp ;
40
+ @ Autowired
41
+ private final BillServiceImp serviceBillImp ;
40
42
41
43
// CREATE
42
44
@ PostMapping ()
43
45
public ResponseEntity <Response > saveOrder (@ RequestBody @ Valid Orders order ) {
44
- Collection < Additional > additionals = order .getAdditional ( );
45
- for ( Additional additional : additionals ) {
46
- if (additional . getIdAdditional ()!= null && additional . getPrice ()<= 0 ) {
47
- return ResponseEntity
48
- .ok ( Response . builder (). timeStamp ( Instant . now ()) .message ("The order with id:" + order . getIdOrder () + " does not have the additional prices" )
49
- . status ( HttpStatus . BAD_REQUEST ). statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
46
+ UserBillOrdersDTO userBillOrdersDTO = serviceBillImp . findByIdBill ( order .getBill (). getIdBill () );
47
+ if ( userBillOrdersDTO != null ) {
48
+ if (userBillOrdersDTO . getBillUserDTO (). getStatusBill () != StatusBill . PAID ) {
49
+ return ResponseEntity . ok ( Response . builder (). timeStamp ( Instant . now ())
50
+ .data ( Map . of ( "order" , serviceImp . create ( order ))) .message ("Create order" ). status ( HttpStatus . OK )
51
+ . statusCode (HttpStatus .OK .value ()).build ());
50
52
}
53
+ } else {
54
+ return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
55
+ .message ("The bill " + order .getIdOrder () + " does not exist" ).status (HttpStatus .BAD_REQUEST )
56
+ .statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
51
57
}
52
- return ResponseEntity
53
- .ok (Response .builder ().timeStamp (Instant .now ()).data (Map .of ("order" , serviceImp .create (order )))
54
- .message ("Create order" ).status (HttpStatus .OK ).statusCode (HttpStatus .OK .value ()).build ());
55
- }
56
-
57
- // READ
58
- @ PreAuthorize ("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')" )
59
- @ GetMapping (value = "/list" )
60
- public ResponseEntity <Response > getOrder () {
61
- return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ()).data (Map .of ("order" , serviceImp .list ()))
62
- .message ("List orders" ).status (HttpStatus .OK ).statusCode (HttpStatus .OK .value ()).build ());
58
+ return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
59
+ .message ("The order with id:" + order .getIdOrder () + " does not created because the bill already paid" )
60
+ .status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
63
61
}
64
62
65
63
// UPDATE
66
64
@ PreAuthorize ("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')" )
67
65
@ PutMapping (value = "/{id}" )
68
66
public ResponseEntity <Response > updateOrder (@ PathVariable ("id" ) Long id , @ RequestBody @ Valid Orders order ) {
69
- if (serviceImp .exist (id )) {
70
- Collection <Additional > additionals = order .getAdditional ();
71
- for (Additional additional : additionals ) {
72
- if (additional .getIdAdditional ()!=null && additional .getPrice ()<=0 ) {
73
- return ResponseEntity
74
- .ok (Response .builder ().timeStamp (Instant .now ()).message ("The order with id:" +id + " does not have the additional prices" )
67
+ UserBillOrdersDTO userBillOrdersDTO = serviceBillImp .findByIdBill (order .getBill ().getIdBill ());
68
+ if (userBillOrdersDTO != null ) {
69
+ if (userBillOrdersDTO .getBillUserDTO ().getStatusBill () != StatusBill .PAID ) {
70
+ Orders orderRequest = serviceImp .findByIdOrder (id );
71
+ if (orderRequest != null ) {
72
+ return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
73
+ .data (Map .of ("order" , serviceImp .update (id , order ))).message ("Updating order with id: " +id )
74
+ .status (HttpStatus .OK ).statusCode (HttpStatus .OK .value ()).build ());
75
+ } else {
76
+ return ResponseEntity .ok (
77
+ Response .builder ().timeStamp (Instant .now ()).message ("The order " + id + " does not exist" )
75
78
.status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
76
79
}
77
80
}
78
- return ResponseEntity
79
- .ok (Response .builder ().timeStamp (Instant .now ()).data (Map .of ("order" , serviceImp .update (id ,order )))
80
- .message ("Create order" ).status (HttpStatus .OK ).statusCode (HttpStatus .OK .value ()).build ());
81
+ } else {
82
+ return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
83
+ .message ("The bill " + order .getBill ().getIdBill () + " does not exist" )
84
+ .status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
81
85
}
82
- return ResponseEntity
83
- .ok (Response .builder ().timeStamp (Instant .now ()).message ("The order with id:" + id + " does not exist" )
84
- .status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
85
-
86
+ return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
87
+ .message ("The order with id:" + order .getIdOrder () + " does not created because the bill already paid" )
88
+ .status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
86
89
}
87
90
88
91
@ PreAuthorize ("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')" )
89
92
// DELETE
90
93
@ DeleteMapping (value = "/{id}" )
91
94
public ResponseEntity <Response > deleteOrder (@ PathVariable ("id" ) Long id ) {
95
+
92
96
if (serviceImp .exist (id )) {
93
97
return ResponseEntity .ok (Response .builder ().timeStamp (Instant .now ())
94
98
.data (Map .of ("order" , serviceImp .delete (id ))).message ("order bill with id: " + id )
@@ -99,13 +103,4 @@ public ResponseEntity<Response> deleteOrder(@PathVariable("id") Long id) {
99
103
.status (HttpStatus .BAD_REQUEST ).statusCode (HttpStatus .BAD_REQUEST .value ()).build ());
100
104
}
101
105
}
102
-
103
- @ PreAuthorize ("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')" )
104
- // SEARCH ORDER BY ID CLIENT
105
- @ GetMapping (value = "/bill/{idBill}" )
106
- public ResponseEntity <Response > getOrderByIdClient (@ PathVariable ("idBill" ) Long idBill ) {
107
- return ResponseEntity
108
- .ok (Response .builder ().timeStamp (Instant .now ()).data (Map .of ("order" , serviceImp .findByIdBill (idBill )))
109
- .message ("List orders" ).status (HttpStatus .OK ).statusCode (HttpStatus .OK .value ()).build ());
110
- }
111
106
}
0 commit comments