Skip to content

Commit f6ca317

Browse files
author
JEstebanC
committed
modification in the order now all the search will be for bill and fix security problems with the order
1 parent 11c79ea commit f6ca317

File tree

9 files changed

+111
-196
lines changed

9 files changed

+111
-196
lines changed
Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package JEstebanC.FastFoodApp.controller;
22

33
import java.time.Instant;
4-
import java.util.Collection;
54
import java.util.Map;
65

76
import javax.validation.Valid;
@@ -11,17 +10,18 @@
1110
import org.springframework.http.ResponseEntity;
1211
import org.springframework.security.access.prepost.PreAuthorize;
1312
import org.springframework.web.bind.annotation.DeleteMapping;
14-
import org.springframework.web.bind.annotation.GetMapping;
1513
import org.springframework.web.bind.annotation.PathVariable;
1614
import org.springframework.web.bind.annotation.PostMapping;
1715
import org.springframework.web.bind.annotation.PutMapping;
1816
import org.springframework.web.bind.annotation.RequestBody;
1917
import org.springframework.web.bind.annotation.RequestMapping;
2018
import org.springframework.web.bind.annotation.RestController;
2119

22-
import JEstebanC.FastFoodApp.model.Additional;
20+
import JEstebanC.FastFoodApp.dto.UserBillOrdersDTO;
21+
import JEstebanC.FastFoodApp.enumeration.StatusBill;
2322
import JEstebanC.FastFoodApp.model.Orders;
2423
import JEstebanC.FastFoodApp.model.Response;
24+
import JEstebanC.FastFoodApp.service.BillServiceImp;
2525
import JEstebanC.FastFoodApp.service.OrdersServiceImp;
2626
import lombok.RequiredArgsConstructor;
2727

@@ -37,58 +37,62 @@ public class OrdersController {
3737

3838
@Autowired
3939
private final OrdersServiceImp serviceImp;
40+
@Autowired
41+
private final BillServiceImp serviceBillImp;
4042

4143
// CREATE
4244
@PostMapping()
4345
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());
5052
}
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());
5157
}
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());
6361
}
6462

6563
// UPDATE
6664
@PreAuthorize("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')")
6765
@PutMapping(value = "/{id}")
6866
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")
7578
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
7679
}
7780
}
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());
8185
}
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());
8689
}
8790

8891
@PreAuthorize("hasRole('ROLE_ADMIN') OR hasRole('ROLE_EMPLOYEE')")
8992
// DELETE
9093
@DeleteMapping(value = "/{id}")
9194
public ResponseEntity<Response> deleteOrder(@PathVariable("id") Long id) {
95+
9296
if (serviceImp.exist(id)) {
9397
return ResponseEntity.ok(Response.builder().timeStamp(Instant.now())
9498
.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) {
99103
.status(HttpStatus.BAD_REQUEST).statusCode(HttpStatus.BAD_REQUEST.value()).build());
100104
}
101105
}
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-
}
111106
}

src/main/java/JEstebanC/FastFoodApp/dto/BillOrdersDTO.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/JEstebanC/FastFoodApp/dto/BillUserDTO.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
@Data
1919
public class BillUserDTO {
2020
private Long idBill;
21-
private UserForBillDTO userForBill;
22-
private PayMode payMode;
23-
2421
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")
2522
private Date date;
23+
private int noTable;
24+
private int totalPrice;
2625
private StatusBill statusBill;
26+
private PayMode payMode;
27+
private UserForBillDTO userForBill;
28+
2729
}

src/main/java/JEstebanC/FastFoodApp/dto/OrdersDTO.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class OrdersDTO {
2222
private Long idOrder;
2323
private StatusOrder statusOrder;
2424
private int amount;
25-
private int noTable;
2625
private int total;
2726

2827
// Product

src/main/java/JEstebanC/FastFoodApp/model/Bill.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class Bill {
4141
@NotNull(message = "idPayMode cannot be empty or null")
4242
@JoinColumn(name = "idPayMode")
4343
private PayMode PayMode;
44-
44+
@NotNull(message = "noTable cannot be empty or null")
45+
private int noTable;
46+
@NotNull(message = "total price cannot be empty or null")
47+
private int totalPrice;
4548
private StatusBill statusBill;
4649
}

src/main/java/JEstebanC/FastFoodApp/model/Orders.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class Orders {
4949

5050
@NotNull(message = "amount cannot be empty or null")
5151
private int amount;
52-
private int noTable;
5352
@NotNull(message = "total cannot be empty or null")
5453
private int total;
5554
private StatusOrder statusOrder;

src/main/java/JEstebanC/FastFoodApp/service/BillServiceImp.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ public Collection<UserBillOrdersDTO> findByNewIdUser(Long idUser, StatusBill sta
175175
private BillUserDTO convertirBillToDTO(Bill bill) {
176176
BillUserDTO billUser = new BillUserDTO();
177177
billUser.setIdBill(bill.getIdBill());
178+
billUser.setNoTable(bill.getNoTable());
179+
billUser.setTotalPrice(bill.getTotalPrice());
178180

179181
UserForBillDTO userForBill = new UserForBillDTO();
180182
userForBill.setIdUser(bill.getUser().getIdUser());
@@ -202,6 +204,8 @@ private UserBillOrdersDTO convertirBillOrderToDTO(Bill bill) {
202204

203205
BillUserDTO billUser = new BillUserDTO();
204206
billUser.setIdBill(bill.getIdBill());
207+
billUser.setNoTable(bill.getNoTable());
208+
billUser.setTotalPrice(bill.getTotalPrice());
205209

206210
UserForBillDTO userForBill = new UserForBillDTO();
207211
userForBill.setIdUser(bill.getUser().getIdUser());
@@ -228,31 +232,13 @@ private UserBillOrdersDTO convertirBillOrderToDTO(Bill bill) {
228232
return billOrder;
229233
}
230234

231-
private OrdersDTO convertirOrderToDTO(Orders orders) {
232-
233-
OrdersDTO billOrder = new OrdersDTO();
234-
billOrder.setIdOrder(orders.getIdOrder());
235-
billOrder.setStatusOrder(orders.getStatusOrder());
236-
billOrder.setAmount(orders.getAmount());
237-
billOrder.setNoTable(orders.getNoTable());
238-
billOrder.setTotal(orders.getTotal());
239-
240-
Collection<Product> product = new ArrayList<Product>();
241-
product.add(orders.getProduct());
242-
billOrder.setProduct(product);
243-
244-
Collection<Additional> additional = new ArrayList<Additional>();
245-
additional.addAll(orders.getAdditional());
246-
billOrder.setAdditional(additional);
247-
248-
return billOrder;
249-
}
250-
251235
private UserBillOrdersDTO convertirBillByOrderToDTO(Bill bill, int statusBill) {
252236
UserBillOrdersDTO billOrder = new UserBillOrdersDTO();
253237

254238
BillUserDTO billUser = new BillUserDTO();
255239
billUser.setIdBill(bill.getIdBill());
240+
billUser.setNoTable(bill.getNoTable());
241+
billUser.setTotalPrice(bill.getTotalPrice());
256242

257243
UserForBillDTO userForBill = new UserForBillDTO();
258244
userForBill.setIdUser(bill.getUser().getIdUser());
@@ -278,5 +264,24 @@ private UserBillOrdersDTO convertirBillByOrderToDTO(Bill bill, int statusBill) {
278264
billOrder.setOrdersDTO(orders);
279265
return billOrder;
280266
}
267+
268+
private OrdersDTO convertirOrderToDTO(Orders orders) {
269+
270+
OrdersDTO billOrder = new OrdersDTO();
271+
billOrder.setIdOrder(orders.getIdOrder());
272+
billOrder.setStatusOrder(orders.getStatusOrder());
273+
billOrder.setAmount(orders.getAmount());
274+
billOrder.setTotal(orders.getTotal());
275+
276+
Collection<Product> product = new ArrayList<Product>();
277+
product.add(orders.getProduct());
278+
billOrder.setProduct(product);
279+
280+
Collection<Additional> additional = new ArrayList<Additional>();
281+
additional.addAll(orders.getAdditional());
282+
billOrder.setAdditional(additional);
283+
284+
return billOrder;
285+
}
281286

282287
}

0 commit comments

Comments
 (0)