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 .HttpStatus ;
12
+ import org .springframework .http .ResponseEntity ;
10
13
import org .springframework .web .bind .annotation .CrossOrigin ;
14
+ import org .springframework .web .bind .annotation .DeleteMapping ;
11
15
import org .springframework .web .bind .annotation .GetMapping ;
16
+ import org .springframework .web .bind .annotation .PathVariable ;
17
+ import org .springframework .web .bind .annotation .PostMapping ;
18
+ import org .springframework .web .bind .annotation .RequestBody ;
12
19
import org .springframework .web .bind .annotation .RestController ;
13
20
14
21
@ RestController
@@ -29,4 +36,46 @@ public GetMediaItemsResponse getItems() {
29
36
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
30
37
return response ;
31
38
}
39
+
40
+ @ GetMapping ("/items/{id}" )
41
+ public GetMediaItemsResponse getItem (@ PathVariable UUID id ) {
42
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
43
+ List <MediaItemResponse > responseItem =
44
+ items .stream ()
45
+ .map (MediaItemResponse ::from ) // Transform the original objects
46
+ .filter (item -> item .getId ().equals (id ))
47
+ .toList ();
48
+ var response = GetMediaItemsResponse .builder ().items (responseItem ).build ();
49
+
50
+ return response ;
51
+ }
52
+
53
+ @ DeleteMapping ("/items/{id}" )
54
+ public ResponseEntity <Object > deleteItem (@ PathVariable UUID id ) {
55
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
56
+ List <MediaItemResponse > itemToDelete =
57
+ items .stream ()
58
+ .map (MediaItemResponse ::from ) // Transform the original objects
59
+ .filter (item -> item .getId ().equals (id ))
60
+ .toList ();
61
+
62
+ if (!itemToDelete .isEmpty ()) {
63
+ library .removeMediaItem (id , librarian );
64
+ return ResponseEntity .status (HttpStatus .OK ).body ("media item deleted successfully" );
65
+ }
66
+ return ResponseEntity .status (HttpStatus .BAD_REQUEST ).body ("Media deletion not successful" );
67
+ }
68
+
69
+ @ PostMapping ("/items" )
70
+ public ResponseEntity <Object > addItem (@ RequestBody MediaItemRequest mediaItemRequest ) {
71
+
72
+ MediaItem mediaItem = MediaItemRequest .asMediaItem (mediaItemRequest );
73
+ library .addMediaItem (mediaItem , librarian );
74
+
75
+ if (null == mediaItem ) {
76
+ return ResponseEntity .status (HttpStatus .BAD_REQUEST )
77
+ .body ("media item creation not successful" );
78
+ }
79
+ return ResponseEntity .status (HttpStatus .CREATED ).body ("new media item created successfully" );
80
+ }
32
81
}
0 commit comments