44import com .codedifferently .lesson26 .library .Library ;
55import com .codedifferently .lesson26 .library .MediaItem ;
66import com .codedifferently .lesson26 .library .search .SearchCriteria ;
7-
87import java .io .IOException ;
98import java .util .List ;
109import java .util .Optional ;
1110import java .util .Set ;
11+ import org .springframework .http .HttpStatus ;
12+ import org .springframework .validation .annotation .Validated ;
1213import org .springframework .web .bind .annotation .CrossOrigin ;
13- import org .springframework .web .bind .annotation .GetMapping ;
14- import org .springframework .web .bind .annotation .RestController ;
15- import org .springframework .web .server .ResponseStatusException ;
16- import org .springframework .web .bind .annotation .PostMapping ;
1714import org .springframework .web .bind .annotation .DeleteMapping ;
15+ import org .springframework .web .bind .annotation .GetMapping ;
1816import org .springframework .web .bind .annotation .PathVariable ;
17+ import org .springframework .web .bind .annotation .PostMapping ;
1918import org .springframework .web .bind .annotation .RequestBody ;
2019import org .springframework .web .bind .annotation .ResponseStatus ;
21- import org .springframework .http . HttpStatus ;
22- import org .springframework .validation . annotation . Validated ;
20+ import org .springframework .web . bind . annotation . RestController ;
21+ import org .springframework .web . server . ResponseStatusException ;
2322
2423@ RestController
2524@ CrossOrigin
@@ -40,37 +39,38 @@ public GetMediaItemsResponse getItems() {
4039 return response ;
4140 }
4241
43- // GET a single item by its ID
44- @ GetMapping ("/items/{id}" )
45- public MediaItemResponse getItem (@ PathVariable String id ) {
46- Optional <MediaItem > item = library . search ( SearchCriteria . builder (). id ( id ). build ()). stream (). findFirst ();
47- if ( item . isPresent ()) {
48- return MediaItemResponse . from (item .get ());
49- } else {
50- throw new ResponseStatusException ( HttpStatus . NOT_FOUND , "Unknown media item ID: " + id );
51- }
42+ // GET a single item by its ID
43+ @ GetMapping ("/items/{id}" )
44+ public MediaItemResponse getItem (@ PathVariable String id ) {
45+ Optional <MediaItem > item =
46+ library . search ( SearchCriteria . builder (). id ( id ). build ()). stream (). findFirst ();
47+ if (item .isPresent ()) {
48+ return MediaItemResponse . from ( item . get ());
49+ } else {
50+ throw new ResponseStatusException ( HttpStatus . NOT_FOUND , "Unknown media item ID: " + id );
5251 }
52+ }
5353
54- // POST a new item to /items
55- @ PostMapping ("/items" )
56- public MediaItemResponse addItem (@ RequestBody @ Validated CreateMediaItemRequest request ) {
57- // Validate the request body and ensure fields are not empty
58- MediaItemRequest mediaItemRequest = request .getItem ();
59- MediaItem newItem = MediaItemRequest .asMediaItem (mediaItemRequest );
60- library .addMediaItem (newItem , librarian );
61- return MediaItemResponse .from (newItem );
62- }
54+ // POST a new item to /items
55+ @ PostMapping ("/items" )
56+ public MediaItemResponse addItem (@ RequestBody @ Validated CreateMediaItemRequest request ) {
57+ // Validate the request body and ensure fields are not empty
58+ MediaItemRequest mediaItemRequest = request .getItem ();
59+ MediaItem newItem = MediaItemRequest .asMediaItem (mediaItemRequest );
60+ library .addMediaItem (newItem , librarian );
61+ return MediaItemResponse .from (newItem );
62+ }
6363
64- // DELETE an item by its ID
65- @ DeleteMapping ("/items/{id}" )
66- @ ResponseStatus (HttpStatus .NO_CONTENT ) // No content when successfully deleted
67- public void deleteItem (@ PathVariable String id ) {
68- Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
69- if (items .isEmpty ()) {
70- // Return 404 Not Found if item doesn't exist
71- throw new ResponseStatusException (HttpStatus .NOT_FOUND , "Item not found for deletion: " + id );
72- }
73- MediaItem itemToDelete = items .iterator ().next ();
74- library .removeMediaItem (itemToDelete , librarian );
64+ // DELETE an item by its ID
65+ @ DeleteMapping ("/items/{id}" )
66+ @ ResponseStatus (HttpStatus .NO_CONTENT ) // No content when successfully deleted
67+ public void deleteItem (@ PathVariable String id ) {
68+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
69+ if (items .isEmpty ()) {
70+ // Return 404 Not Found if item doesn't exist
71+ throw new ResponseStatusException (HttpStatus .NOT_FOUND , "Item not found for deletion: " + id );
7572 }
73+ MediaItem itemToDelete = items .iterator ().next ();
74+ library .removeMediaItem (itemToDelete , librarian );
75+ }
7676}
0 commit comments