|
1 | 1 | package com.codedifferently.lesson26.web;
|
2 | 2 |
|
3 |
| -import com.codedifferently.lesson26.library.Librarian; |
| 3 | +import com.codedifferently.lesson26.library.Librarian; // Ensure this import is present |
4 | 4 | import com.codedifferently.lesson26.library.Library;
|
5 | 5 | import com.codedifferently.lesson26.library.MediaItem;
|
6 | 6 | import com.codedifferently.lesson26.library.search.SearchCriteria;
|
7 |
| -import java.io.IOException; |
| 7 | +import jakarta.validation.Valid; |
8 | 8 | import java.util.List;
|
| 9 | +import java.util.Map; |
9 | 10 | import java.util.Set;
|
| 11 | +import java.util.UUID; |
10 | 12 | import org.springframework.http.ResponseEntity;
|
| 13 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
11 | 14 | import org.springframework.web.bind.annotation.CrossOrigin;
|
| 15 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 16 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
12 | 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; |
13 | 22 | import org.springframework.web.bind.annotation.RestController;
|
14 | 23 |
|
| 24 | +// DTO imports |
| 25 | + |
15 | 26 | @RestController
|
16 | 27 | @CrossOrigin
|
| 28 | +@RequestMapping("/items") |
17 | 29 | public class MediaItemsController {
|
18 | 30 |
|
19 | 31 | private final Library library;
|
20 |
| - private final Librarian librarian; |
| 32 | + private final Librarian librarian = new Librarian("default-librarian", "default-role"); |
21 | 33 |
|
22 |
| - public MediaItemsController(Library library) throws IOException { |
| 34 | + public MediaItemsController(Library library) { |
23 | 35 | this.library = library;
|
24 |
| - this.librarian = library.getLibrarians().stream().findFirst().orElseThrow(); |
25 | 36 | }
|
26 | 37 |
|
27 |
| - @GetMapping("/items") |
| 38 | + @GetMapping |
28 | 39 | public ResponseEntity<GetMediaItemsResponse> getItems() {
|
29 | 40 | Set<MediaItem> items = library.search(SearchCriteria.builder().build());
|
30 | 41 | List<MediaItemResponse> responseItems = items.stream().map(MediaItemResponse::from).toList();
|
| 42 | + |
31 | 43 | var response = GetMediaItemsResponse.builder().items(responseItems).build();
|
32 | 44 | return ResponseEntity.ok(response);
|
33 | 45 | }
|
| 46 | + |
| 47 | + @GetMapping("/{id}") |
| 48 | + public ResponseEntity<MediaItemResponse> getItemById(@PathVariable String id) { |
| 49 | + try { |
| 50 | + Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build()); |
| 51 | + if (items.isEmpty()) { |
| 52 | + return ResponseEntity.notFound().build(); |
| 53 | + } |
| 54 | + MediaItemResponse response = MediaItemResponse.from(items.iterator().next()); |
| 55 | + return ResponseEntity.ok(response); |
| 56 | + } catch (IllegalArgumentException e) { |
| 57 | + return ResponseEntity.notFound().build(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @PostMapping |
| 62 | + public ResponseEntity<CreateMediaItemResponse> createItem( |
| 63 | + @Valid @RequestBody CreateMediaItemRequest request) { |
| 64 | + try { |
| 65 | + MediaItem item = MediaItemRequest.asMediaItem(request.getItem()); |
| 66 | + library.addMediaItem(item, librarian); // Ensure you're passing a Librarian object |
| 67 | + var response = CreateMediaItemResponse.builder().item(MediaItemResponse.from(item)).build(); |
| 68 | + return ResponseEntity.ok(response); |
| 69 | + } catch (IllegalArgumentException e) { |
| 70 | + return ResponseEntity.badRequest().build(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @DeleteMapping("/{id}") |
| 75 | + public ResponseEntity<Void> deleteItemById(@PathVariable String id) { |
| 76 | + try { |
| 77 | + UUID uuid = UUID.fromString(id); |
| 78 | + Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build()); |
| 79 | + if (items.isEmpty()) { |
| 80 | + return ResponseEntity.notFound().build(); |
| 81 | + } |
| 82 | + library.removeMediaItem(uuid, librarian); // Ensure you're passing a Librarian object |
| 83 | + return ResponseEntity.noContent().build(); |
| 84 | + } catch (IllegalArgumentException e) { |
| 85 | + return ResponseEntity.notFound().build(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 90 | + public ResponseEntity<Map<String, List<Map<String, String>>>> handleValidationErrors( |
| 91 | + MethodArgumentNotValidException ex) { |
| 92 | + List<Map<String, String>> errors = |
| 93 | + ex.getBindingResult().getFieldErrors().stream() |
| 94 | + .map( |
| 95 | + fieldError -> |
| 96 | + Map.of( |
| 97 | + "field", fieldError.getField(), |
| 98 | + "message", fieldError.getDefaultMessage())) |
| 99 | + .toList(); |
| 100 | + |
| 101 | + // Optional: Log the validation errors |
| 102 | + System.out.println("Validation errors: " + errors); |
| 103 | + |
| 104 | + return ResponseEntity.badRequest().body(Map.of("errors", errors)); |
| 105 | + } |
34 | 106 | }
|
0 commit comments