7
7
import java .io .IOException ;
8
8
import java .util .List ;
9
9
import java .util .Set ;
10
+ import java .util .UUID ;
11
+ import org .springframework .http .ResponseEntity ;
10
12
import org .springframework .web .bind .annotation .CrossOrigin ;
13
+ import org .springframework .web .bind .annotation .DeleteMapping ;
11
14
import org .springframework .web .bind .annotation .GetMapping ;
15
+ import org .springframework .web .bind .annotation .PathVariable ;
16
+ import org .springframework .web .bind .annotation .PostMapping ;
17
+ import org .springframework .web .bind .annotation .RequestBody ;
12
18
import org .springframework .web .bind .annotation .RestController ;
13
19
14
20
@ RestController
@@ -29,4 +35,33 @@ public GetMediaItemsResponse getItems() {
29
35
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
30
36
return response ;
31
37
}
38
+
39
+ @ GetMapping ("/items/{id}" )
40
+ public MediaItemResponse getItemById (@ PathVariable ("id" ) String id ) {
41
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
42
+ MediaItemResponse response =
43
+ items .stream ().map (MediaItemResponse ::from ).findFirst ().orElseThrow ();
44
+ return response ;
45
+ }
46
+
47
+ @ PostMapping ("/items" )
48
+ public CreateMediaItemResponse postItems (@ RequestBody CreateMediaItemRequest requestItem ) {
49
+ MediaItemRequest itemMediaItemRequest = requestItem .getItem ();
50
+ MediaItem item = MediaItemRequest .asMediaItem (itemMediaItemRequest );
51
+ library .addMediaItem (item , librarian );
52
+ MediaItemResponse itemResponse = MediaItemResponse .from (item );
53
+ return CreateMediaItemResponse .builder ().item (itemResponse ).build ();
54
+ }
55
+
56
+ @ DeleteMapping ("/items/{id}" )
57
+ public ResponseEntity <Void > deleteItem (@ PathVariable ("id" ) UUID id ) {
58
+ SearchCriteria searchCriteria = SearchCriteria .builder ().id (id .toString ()).build ();
59
+ Set <MediaItem > foundItem = library .search (searchCriteria );
60
+ if (foundItem .isEmpty ()) {
61
+ return ResponseEntity .notFound ().build ();
62
+ }
63
+ MediaItem item = foundItem .iterator ().next ();
64
+ library .removeMediaItem (item , librarian );
65
+ return ResponseEntity .noContent ().build ();
66
+ }
32
67
}
0 commit comments