|
| 1 | +package com.bytebite.server; |
| 2 | + |
| 3 | +import com.bytebite.server.dto.GroceryListCreateRequest; |
| 4 | +import com.bytebite.server.dto.GroceryListDetailDTO; |
| 5 | +import com.bytebite.server.dto.GroceryListSummaryDTO; |
| 6 | +import com.bytebite.server.service.GroceryListService; |
| 7 | +import io.swagger.v3.oas.annotations.Operation; |
| 8 | +import io.swagger.v3.oas.annotations.Parameter; |
| 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.security.SecurityRequirement; |
| 13 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 17 | +import org.springframework.web.bind.annotation.GetMapping; |
| 18 | +import org.springframework.web.bind.annotation.PathVariable; |
| 19 | +import org.springframework.web.bind.annotation.PostMapping; |
| 20 | +import org.springframework.web.bind.annotation.RequestBody; |
| 21 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 22 | +import org.springframework.web.bind.annotation.RestController; |
| 23 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 24 | + |
| 25 | +import java.net.URI; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +@RestController |
| 31 | +@RequestMapping("/api/grocery-list") |
| 32 | +@Tag(name = "Grocery Lists", description = "Stored grocery-list history and details") |
| 33 | +public class GroceryListController { |
| 34 | + |
| 35 | + private final GroceryListService service; |
| 36 | + |
| 37 | + public GroceryListController(GroceryListService service) { |
| 38 | + this.service = service; |
| 39 | + } |
| 40 | + |
| 41 | + @GetMapping(value = "/history", produces = MediaType.APPLICATION_JSON_VALUE) |
| 42 | + @Operation( |
| 43 | + summary = "List recent grocery lists", |
| 44 | + description = "Returns a summary of the 20 most recently created grocery lists, newest first.", |
| 45 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 46 | + responses = { |
| 47 | + @ApiResponse(responseCode = "200", description = "Grocery-list summaries"), |
| 48 | + @ApiResponse(responseCode = "401", description = "Missing, expired, or invalid JWT", content = @Content) |
| 49 | + } |
| 50 | + ) |
| 51 | + public List<GroceryListSummaryDTO> getHistory() { |
| 52 | + return service.getHistory(); |
| 53 | + } |
| 54 | + |
| 55 | + @GetMapping(value = "/history/{id}", produces = MediaType.APPLICATION_JSON_VALUE) |
| 56 | + @Operation( |
| 57 | + summary = "Get a grocery list by id", |
| 58 | + description = "Returns a single grocery list including its items.", |
| 59 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 60 | + responses = { |
| 61 | + @ApiResponse(responseCode = "200", description = "Grocery list with items"), |
| 62 | + @ApiResponse(responseCode = "401", description = "Missing, expired, or invalid JWT", content = @Content), |
| 63 | + @ApiResponse(responseCode = "404", description = "Grocery list not found", content = @Content(schema = @Schema(implementation = Map.class))) |
| 64 | + } |
| 65 | + ) |
| 66 | + public GroceryListDetailDTO getById( |
| 67 | + @Parameter(description = "Identifier of the grocery list", required = true) |
| 68 | + @PathVariable UUID id) { |
| 69 | + return service.getById(id); |
| 70 | + } |
| 71 | + |
| 72 | + @PostMapping(value = "/history", |
| 73 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 74 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 75 | + @Operation( |
| 76 | + summary = "Create a grocery list", |
| 77 | + description = "Persists a new grocery list with its items and returns the created resource.", |
| 78 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 79 | + responses = { |
| 80 | + @ApiResponse(responseCode = "201", description = "Grocery list created"), |
| 81 | + @ApiResponse(responseCode = "400", description = "Invalid request body", content = @Content(schema = @Schema(implementation = Map.class))), |
| 82 | + @ApiResponse(responseCode = "401", description = "Missing, expired, or invalid JWT", content = @Content) |
| 83 | + } |
| 84 | + ) |
| 85 | + public ResponseEntity<GroceryListDetailDTO> create(@RequestBody GroceryListCreateRequest request) { |
| 86 | + GroceryListDetailDTO created = service.create(request); |
| 87 | + URI location = ServletUriComponentsBuilder.fromCurrentRequest() |
| 88 | + .path("/{id}") |
| 89 | + .buildAndExpand(created.id()) |
| 90 | + .toUri(); |
| 91 | + return ResponseEntity.created(location).body(created); |
| 92 | + } |
| 93 | + |
| 94 | + @DeleteMapping(value = "/history/{id}") |
| 95 | + @Operation( |
| 96 | + summary = "Delete a grocery list", |
| 97 | + description = "Deletes the grocery list identified by the given id along with its items.", |
| 98 | + security = @SecurityRequirement(name = "bearerAuth"), |
| 99 | + responses = { |
| 100 | + @ApiResponse(responseCode = "204", description = "Grocery list deleted"), |
| 101 | + @ApiResponse(responseCode = "401", description = "Missing, expired, or invalid JWT", content = @Content), |
| 102 | + @ApiResponse(responseCode = "404", description = "Grocery list not found", content = @Content(schema = @Schema(implementation = Map.class))) |
| 103 | + } |
| 104 | + ) |
| 105 | + public ResponseEntity<Void> delete( |
| 106 | + @Parameter(description = "Identifier of the grocery list", required = true) |
| 107 | + @PathVariable UUID id) { |
| 108 | + service.delete(id); |
| 109 | + return ResponseEntity.noContent().build(); |
| 110 | + } |
| 111 | +} |
0 commit comments