|
1 | 1 | package com.answerdigital.answerking.controller; |
2 | 2 |
|
3 | | -import com.answerdigital.answerking.model.Order; |
| 3 | +import com.answerdigital.answerking.exception.util.ErrorResponse; |
4 | 4 | import com.answerdigital.answerking.request.OrderRequest; |
| 5 | +import com.answerdigital.answerking.response.OrderResponse; |
5 | 6 | import com.answerdigital.answerking.service.OrderService; |
| 7 | +import io.swagger.v3.oas.annotations.Operation; |
| 8 | +import io.swagger.v3.oas.annotations.media.ArraySchema; |
| 9 | +import io.swagger.v3.oas.annotations.media.Content; |
| 10 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
6 | 13 | import io.swagger.v3.oas.annotations.tags.Tag; |
7 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
8 | 15 | import org.springframework.http.HttpStatus; |
9 | 16 | import org.springframework.http.ResponseEntity; |
10 | | -import org.springframework.validation.annotation.Validated; |
| 17 | +import org.springframework.web.bind.annotation.DeleteMapping; |
11 | 18 | import org.springframework.web.bind.annotation.GetMapping; |
| 19 | +import org.springframework.web.bind.annotation.PathVariable; |
12 | 20 | import org.springframework.web.bind.annotation.PostMapping; |
13 | 21 | import org.springframework.web.bind.annotation.PutMapping; |
14 | | -import org.springframework.web.bind.annotation.DeleteMapping; |
15 | 22 | import org.springframework.web.bind.annotation.RequestBody; |
16 | 23 | import org.springframework.web.bind.annotation.RequestMapping; |
17 | 24 | import org.springframework.web.bind.annotation.RestController; |
18 | | -import org.springframework.web.bind.annotation.PathVariable; |
19 | 25 |
|
20 | 26 | import javax.validation.Valid; |
21 | 27 | import javax.validation.constraints.NotNull; |
22 | 28 | import java.util.List; |
23 | 29 |
|
24 | | -@Validated |
25 | | -@Tag(name = "Orders", description = "Create and manage customer orders.") |
26 | 30 | @RestController |
27 | 31 | @RequestMapping(path = "/orders") |
| 32 | +@Tag(name = "Orders", description = "Create and manage customer orders.") |
28 | 33 | public class OrderController { |
29 | | - |
30 | 34 | private final OrderService orderService; |
31 | 35 |
|
32 | 36 | @Autowired |
33 | 37 | public OrderController(final OrderService orderService) { |
34 | 38 | this.orderService = orderService; |
35 | 39 | } |
36 | 40 |
|
| 41 | + @GetMapping |
| 42 | + @Operation(summary = "Get all orders.") |
| 43 | + @ApiResponses(value = { |
| 44 | + @ApiResponse(responseCode = "200", description = "When all the orders have been returned.", |
| 45 | + content = { |
| 46 | + @Content(mediaType = "application/json", |
| 47 | + array = @ArraySchema(schema = @Schema(implementation = OrderResponse.class)))} |
| 48 | + ) |
| 49 | + }) |
| 50 | + public ResponseEntity<List<OrderResponse>> getAllOrders() { |
| 51 | + return new ResponseEntity<>(orderService.findAll(), HttpStatus.OK); |
| 52 | + } |
| 53 | + |
37 | 54 | @PostMapping |
38 | | - public ResponseEntity<Order> addOrder(@Valid @RequestBody final OrderRequest orderRequest) { |
| 55 | + @Operation(summary = "Create a new order.") |
| 56 | + @ApiResponses(value = { |
| 57 | + @ApiResponse(responseCode = "201", description = "When the order has been created.", |
| 58 | + content = {@Content(mediaType = "application/json", schema = @Schema(implementation = OrderResponse.class))}), |
| 59 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 60 | + content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}) |
| 61 | + }) |
| 62 | + public ResponseEntity<OrderResponse> createOrder(@Valid @RequestBody final OrderRequest orderRequest) { |
39 | 63 | return new ResponseEntity<>(orderService.addOrder(orderRequest), HttpStatus.CREATED); |
40 | 64 | } |
41 | 65 |
|
42 | 66 | @GetMapping(path = "/{orderId}") |
43 | | - public ResponseEntity<Order> getOrder(@PathVariable @NotNull final Long orderId) { |
44 | | - return new ResponseEntity<>(orderService.findById(orderId), HttpStatus.CREATED); |
| 67 | + @Operation(summary = "Get a single order.") |
| 68 | + @ApiResponses(value = { |
| 69 | + @ApiResponse(responseCode = "200", description = "When the order with the provided id has been found.", |
| 70 | + content = {@Content(mediaType = "application/json", schema = @Schema(implementation = OrderResponse.class))}), |
| 71 | + @ApiResponse(responseCode = "404", description = "When the order with the given id does not exist.", |
| 72 | + content = {@Content(mediaType = "application/problem+json", schema = @Schema(implementation = ErrorResponse.class))}) |
| 73 | + }) |
| 74 | + public ResponseEntity<OrderResponse> getOrderById(@PathVariable @NotNull final Long orderId) { |
| 75 | + return new ResponseEntity<>(orderService.getOrderResponseById(orderId), HttpStatus.CREATED); |
45 | 76 | } |
46 | 77 |
|
47 | 78 | @PutMapping("/{orderId}") |
48 | | - public ResponseEntity<Order> updateOrder(@PathVariable @NotNull final Long orderId, |
| 79 | + @Operation(summary = "Update an existing order.") |
| 80 | + @ApiResponses(value = { |
| 81 | + @ApiResponse(responseCode = "200", description = "When the order has been updated.", |
| 82 | + content = {@Content(mediaType = "application/json", schema = @Schema(implementation = OrderResponse.class))}), |
| 83 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 84 | + content = {@Content(mediaType = "application/problem+json", schema = @Schema(implementation = ErrorResponse.class))}), |
| 85 | + @ApiResponse(responseCode = "404", description = "When the order with the given id does not exist.", |
| 86 | + content = {@Content(mediaType = "application/problem+json", schema = @Schema(implementation = ErrorResponse.class))}) |
| 87 | + }) |
| 88 | + public ResponseEntity<OrderResponse> updateOrder(@PathVariable @NotNull final Long orderId, |
49 | 89 | @Valid @RequestBody final OrderRequest orderRequest) { |
50 | 90 | return new ResponseEntity<>(orderService.updateOrder(orderId, orderRequest), HttpStatus.OK); |
51 | 91 | } |
52 | 92 |
|
53 | | - @GetMapping |
54 | | - public ResponseEntity<List<Order>> getAllOrders() { |
55 | | - final List<Order> foundOrders = orderService.findAll(); |
56 | | - return new ResponseEntity<>(foundOrders, foundOrders.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK); |
57 | | - } |
58 | | - |
59 | | - @PostMapping(path = "/{orderId}/product/{productId}/quantity/{quantity}") |
60 | | - public ResponseEntity<Order> addProductToBasket(@PathVariable @NotNull final Long orderId, |
61 | | - @PathVariable @NotNull final Long productId, |
62 | | - @PathVariable @NotNull final Integer quantity) { |
63 | | - return new ResponseEntity<>(orderService.addProductToBasket(orderId, productId, quantity), HttpStatus.OK); |
64 | | - } |
65 | | - |
66 | | - @DeleteMapping(path = "/{orderId}/product/{productId}") |
67 | | - public ResponseEntity<Void> deleteProductInBasket(@PathVariable @NotNull final Long orderId, |
68 | | - @PathVariable @NotNull final Long productId) { |
69 | | - orderService.deleteProductInBasket(orderId, productId); |
70 | | - return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
71 | | - } |
72 | | - |
73 | | - @PutMapping(path = "/{orderId}/product/{productId}/quantity/{quantity}") |
74 | | - public ResponseEntity<Order> updateProductQuantity(@PathVariable @NotNull final Long orderId, |
75 | | - @PathVariable @NotNull final Long productId, |
76 | | - @PathVariable @NotNull final Integer quantity) { |
77 | | - return new ResponseEntity<>(orderService.updateProductQuantity(orderId, productId, quantity), HttpStatus.OK); |
| 93 | + @DeleteMapping(path = "/{orderId}") |
| 94 | + @Operation(summary = "Cancel an existing order.") |
| 95 | + @ApiResponses(value = { |
| 96 | + @ApiResponse(responseCode = "200", description = "When the order has been cancelled.", |
| 97 | + content = {@Content(mediaType = "application/json", schema = @Schema(implementation = OrderResponse.class))}), |
| 98 | + @ApiResponse(responseCode = "400", description = "When invalid parameters are provided.", |
| 99 | + content = {@Content(mediaType = "application/problem+json", schema = @Schema(implementation = ErrorResponse.class))}), |
| 100 | + @ApiResponse(responseCode = "404", description = "When the order with the given id does not exist.", |
| 101 | + content = {@Content(mediaType = "application/problem+json", schema = @Schema(implementation = ErrorResponse.class))}) |
| 102 | + }) |
| 103 | + public ResponseEntity<OrderResponse> cancelOrder(@PathVariable @NotNull final Long orderId) { |
| 104 | + return new ResponseEntity<>(orderService.cancelOrder(orderId), HttpStatus.OK); |
78 | 105 | } |
79 | 106 | } |
0 commit comments