|
7 | 7 |
|
8 | 8 | import org.springframework.http.ResponseEntity;
|
9 | 9 | import org.springframework.web.bind.annotation.CrossOrigin;
|
| 10 | +import org.springframework.web.bind.annotation.DeleteMapping; |
10 | 11 | import org.springframework.web.bind.annotation.GetMapping;
|
11 | 12 | import org.springframework.web.bind.annotation.PathVariable;
|
12 | 13 | import org.springframework.web.bind.annotation.PostMapping;
|
|
15 | 16 | import com.codedifferently.lesson26.library.Librarian;
|
16 | 17 | import com.codedifferently.lesson26.library.Library;
|
17 | 18 | import com.codedifferently.lesson26.library.MediaItem;
|
| 19 | +import com.codedifferently.lesson26.library.exceptions.MediaItemCheckedOutException; |
18 | 20 | import com.codedifferently.lesson26.library.search.SearchCriteria;
|
19 | 21 |
|
20 | 22 | import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
@@ -62,4 +64,22 @@ public ResponseEntity<CreateMediaItemResponse> postItem(@RequestBody CreateMedia
|
62 | 64 |
|
63 | 65 | return ResponseEntity.ok(response);
|
64 | 66 | }
|
| 67 | + |
| 68 | + @DeleteMapping("/items/{id}") |
| 69 | + public ResponseEntity<Void> deleteItem(@PathVariable("id") UUID id) { |
| 70 | + Set<MediaItem> items = library.search(SearchCriteria.builder().id(id.toString()).build()); |
| 71 | + |
| 72 | + if (items.isEmpty()) { |
| 73 | + return ResponseEntity.notFound().build(); |
| 74 | + } |
| 75 | + |
| 76 | + MediaItem itemToDelete = items.iterator().next(); |
| 77 | + |
| 78 | + try { |
| 79 | + library.removeMediaItem(itemToDelete, librarian); |
| 80 | + return ResponseEntity.noContent().build(); |
| 81 | + } catch (MediaItemCheckedOutException e) { |
| 82 | + return ResponseEntity.badRequest().build(); |
| 83 | + } |
| 84 | + } |
65 | 85 | }
|
0 commit comments