9
9
import org .springframework .web .bind .annotation .DeleteMapping ;
10
10
import org .springframework .web .bind .annotation .GetMapping ;
11
11
import org .springframework .web .bind .annotation .PathVariable ;
12
+ import org .springframework .web .bind .annotation .PostMapping ;
13
+ import org .springframework .web .bind .annotation .RequestBody ;
12
14
import org .springframework .web .bind .annotation .RestController ;
13
15
14
16
import com .codedifferently .lesson26 .library .Librarian ;
15
17
import com .codedifferently .lesson26 .library .Library ;
16
18
import com .codedifferently .lesson26 .library .MediaItem ;
17
19
import com .codedifferently .lesson26 .library .search .SearchCriteria ;
18
20
21
+ import jakarta .validation .Valid ;
22
+
19
23
@ RestController
20
24
@ CrossOrigin
21
25
public class MediaItemsController {
@@ -28,6 +32,8 @@ public MediaItemsController(Library library) throws IOException {
28
32
this .librarian = library .getLibrarians ().stream ().findFirst ().orElseThrow ();
29
33
}
30
34
35
+ // Gets all media items.
36
+
31
37
@ GetMapping ("/items" )
32
38
public ResponseEntity <GetMediaItemsResponse > getItems () {
33
39
Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
@@ -36,17 +42,42 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
36
42
return ResponseEntity .ok (response );
37
43
}
38
44
45
+ //
46
+
39
47
@ DeleteMapping ("/items/{id}" )
40
48
public ResponseEntity <Void > deleteItem (@ PathVariable String id ) {
41
49
Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
42
-
43
50
if (items .isEmpty ()) {
44
51
return ResponseEntity .notFound ().build (); // Return 404 if item is not found
45
52
}
46
-
47
53
MediaItem itemToDelete = items .iterator ().next ();
48
54
library .removeMediaItem (itemToDelete , librarian ); // Assuming there's a method to remove items in the library
49
-
50
55
return ResponseEntity .noContent ().build (); // Return 204 No Content if deletion is successful
51
56
}
57
+
58
+ //
59
+
60
+ @ GetMapping ("/items/{id}" )
61
+ public ResponseEntity <MediaItemResponse > getItem (@ PathVariable String id ) {
62
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
63
+ if (items .isEmpty ()) {
64
+ return ResponseEntity .notFound ().build (); // Return 404 if item is not found
65
+ }
66
+ MediaItem item = items .iterator ().next ();
67
+ MediaItemResponse response = MediaItemResponse .from (item );
68
+ return ResponseEntity .ok (response );
69
+ }
70
+
71
+ //
72
+
73
+ @ PostMapping ("/items" )
74
+ public ResponseEntity <CreateMediaItemResponse > createItem (
75
+ @ Valid @ RequestBody CreateMediaItemRequest createRequest ) {
76
+ MediaItemRequest itemRequest = createRequest .getItem ();
77
+ MediaItem newItem = MediaItemRequest .asMediaItem (itemRequest );
78
+ library .addMediaItem (newItem , this .librarian );
79
+ MediaItemResponse itemResponse = MediaItemResponse .from (newItem );
80
+ CreateMediaItemResponse response = CreateMediaItemResponse .builder ().item (itemResponse ).build ();
81
+ return ResponseEntity .ok (response );
82
+ }
52
83
}
0 commit comments