7
7
import java .io .IOException ;
8
8
import java .util .List ;
9
9
import java .util .Set ;
10
+ import java .util .Optional ;
10
11
import org .springframework .http .ResponseEntity ;
11
12
import org .springframework .web .bind .annotation .CrossOrigin ;
12
13
import org .springframework .web .bind .annotation .GetMapping ;
13
14
import org .springframework .web .bind .annotation .RestController ;
14
15
15
16
@ RestController
17
+ @ RequestMapping ("/items" )
16
18
@ CrossOrigin
17
19
public class MediaItemsController {
18
20
@@ -24,11 +26,32 @@ public MediaItemsController(Library library) throws IOException {
24
26
this .librarian = library .getLibrarians ().stream ().findFirst ().orElseThrow ();
25
27
}
26
28
27
- @ GetMapping ("/items" )
28
- public ResponseEntity <GetMediaItemsResponse > getItems () {
29
+ @ GetMapping ()
30
+ public ResponseEntity <GetMediaItemsResponse > getItemById () {
29
31
Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
30
- List <MediaItemResponse > responseItems = items .stream ().map (MediaItemResponse ::from ).toList ();
31
- var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
32
+ if (items .isEmpty ()) {
33
+ ResponseEntity .noContent ();
34
+ }
35
+ }
32
36
return ResponseEntity .ok (response );
33
37
}
34
- }
38
+ @ GetMapping (value = "/{id}" )
39
+ public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable ("id" ) UUID id ) {
40
+
41
+ System .out .println (id .toString ());
42
+
43
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id .toString ()).build ());
44
+ Optional <MediaItem > matchedItem =
45
+ items .stream ().filter (item -> item .getId ().equals (id )).findFirst ();
46
+ System .out .println ("items" );
47
+ System .out .println (items );
48
+ System .out .println (matchedItem );
49
+ System .out .println ("mathcedItems" );
50
+ return matchedItem
51
+ .map (
52
+ item -> {
53
+ MediaItemResponse responseItem = MediaItemResponse .from (item );
54
+ return ResponseEntity .ok (responseItem );
55
+ })
56
+ .orElse (ResponseEntity .notFound ().build ());
57
+ }
0 commit comments