4
4
import com .codedifferently .lesson26 .library .Library ;
5
5
import com .codedifferently .lesson26 .library .MediaItem ;
6
6
import com .codedifferently .lesson26 .library .search .SearchCriteria ;
7
-
8
7
import java .io .IOException ;
9
8
import java .util .List ;
10
9
import java .util .Optional ;
11
10
import java .util .Set ;
11
+ import org .springframework .http .HttpStatus ;
12
+ import org .springframework .validation .annotation .Validated ;
12
13
import 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 ;
17
14
import org .springframework .web .bind .annotation .DeleteMapping ;
15
+ import org .springframework .web .bind .annotation .GetMapping ;
18
16
import org .springframework .web .bind .annotation .PathVariable ;
17
+ import org .springframework .web .bind .annotation .PostMapping ;
19
18
import org .springframework .web .bind .annotation .RequestBody ;
20
19
import 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 ;
23
22
24
23
@ RestController
25
24
@ CrossOrigin
@@ -40,37 +39,38 @@ public GetMediaItemsResponse getItems() {
40
39
return response ;
41
40
}
42
41
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 );
52
51
}
52
+ }
53
53
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
+ }
63
63
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 );
75
72
}
73
+ MediaItem itemToDelete = items .iterator ().next ();
74
+ library .removeMediaItem (itemToDelete , librarian );
75
+ }
76
76
}
0 commit comments