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
+
7
8
import java .io .IOException ;
8
9
import java .util .List ;
9
10
import java .util .Optional ;
10
11
import java .util .Set ;
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 ;
15
+ import org .springframework .web .server .ResponseStatusException ;
14
16
import org .springframework .web .bind .annotation .PostMapping ;
15
17
import org .springframework .web .bind .annotation .DeleteMapping ;
16
18
import org .springframework .web .bind .annotation .PathVariable ;
17
19
import org .springframework .web .bind .annotation .RequestBody ;
18
20
import org .springframework .web .bind .annotation .ResponseStatus ;
19
21
import org .springframework .http .HttpStatus ;
22
+ import org .springframework .validation .annotation .Validated ;
20
23
21
24
@ RestController
22
25
@ CrossOrigin
@@ -44,16 +47,17 @@ public MediaItemResponse getItem(@PathVariable String id) {
44
47
if (item .isPresent ()) {
45
48
return MediaItemResponse .from (item .get ());
46
49
} else {
47
- throw new IllegalArgumentException ( "Unknown media item ID: " + id );
50
+ throw new ResponseStatusException ( HttpStatus . NOT_FOUND , "Unknown media item ID: " + id );
48
51
}
49
52
}
50
53
51
54
// POST a new item to /items
52
55
@ PostMapping ("/items" )
53
- public MediaItemResponse addItem (@ RequestBody CreateMediaItemRequest request ) {
56
+ public MediaItemResponse addItem (@ RequestBody @ Validated CreateMediaItemRequest request ) {
57
+ // Validate the request body and ensure fields are not empty
54
58
MediaItemRequest mediaItemRequest = request .getItem ();
55
59
MediaItem newItem = MediaItemRequest .asMediaItem (mediaItemRequest );
56
- library .addMediaItem (newItem , librarian ); // Assuming the library has an addItem method
60
+ library .addMediaItem (newItem , librarian );
57
61
return MediaItemResponse .from (newItem );
58
62
}
59
63
@@ -63,9 +67,10 @@ public MediaItemResponse addItem(@RequestBody CreateMediaItemRequest request) {
63
67
public void deleteItem (@ PathVariable String id ) {
64
68
Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
65
69
if (items .isEmpty ()) {
66
- throw new IllegalArgumentException ("This ID does not exsist to delete! ID: " + id );
70
+ // Return 404 Not Found if item doesn't exist
71
+ throw new ResponseStatusException (HttpStatus .NOT_FOUND , "Item not found for deletion: " + id );
67
72
}
68
73
MediaItem itemToDelete = items .iterator ().next ();
69
- library .removeMediaItem (itemToDelete , librarian ); // Assuming the library has a removeItem method
74
+ library .removeMediaItem (itemToDelete , librarian );
70
75
}
71
76
}
0 commit comments