11
11
import org .springframework .web .bind .annotation .CrossOrigin ;
12
12
import org .springframework .web .bind .annotation .GetMapping ;
13
13
import org .springframework .web .bind .annotation .RestController ;
14
+ import org .springframework .web .bind .annotation .PostMapping ;
15
+ import org .springframework .web .bind .annotation .DeleteMapping ;
16
+ import org .springframework .web .bind .annotation .PathVariable ;
17
+ import org .springframework .web .bind .annotation .RequestBody ;
18
+ import org .springframework .web .bind .annotation .ResponseStatus ;
19
+ import org .springframework .http .HttpStatus ;
14
20
15
21
@ RestController
16
22
@ CrossOrigin
@@ -38,15 +44,16 @@ public MediaItemResponse getItem(@PathVariable String id) {
38
44
if (item .isPresent ()) {
39
45
return MediaItemResponse .from (item .get ());
40
46
} else {
41
- throw new ItemNotFoundException ( id );
47
+ throw new IllegalArgumentException ( "Unknown media item ID: " + id );
42
48
}
43
49
}
44
50
45
51
// POST a new item to /items
46
52
@ PostMapping ("/items" )
47
- public MediaItemResponse addItem (@ RequestBody AddMediaItemRequest request ) {
48
- MediaItem newItem = request .toMediaItem ();
49
- library .addItem (newItem ); // Assuming the library has an addItem method
53
+ public MediaItemResponse addItem (@ RequestBody CreateMediaItemRequest request ) {
54
+ MediaItemRequest mediaItemRequest = request .getItem ();
55
+ MediaItem newItem = MediaItemRequest .asMediaItem (mediaItemRequest );
56
+ library .addMediaItem (newItem , librarian ); // Assuming the library has an addItem method
50
57
return MediaItemResponse .from (newItem );
51
58
}
52
59
@@ -56,9 +63,9 @@ public MediaItemResponse addItem(@RequestBody AddMediaItemRequest request) {
56
63
public void deleteItem (@ PathVariable String id ) {
57
64
Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
58
65
if (items .isEmpty ()) {
59
- throw new ItemNotFoundException ( id );
66
+ throw new IllegalArgumentException ( "This ID does not exsist to delete! ID: " + id );
60
67
}
61
68
MediaItem itemToDelete = items .iterator ().next ();
62
- library .removeItem (itemToDelete ); // Assuming the library has a removeItem method
69
+ library .removeMediaItem (itemToDelete , librarian ); // Assuming the library has a removeItem method
63
70
}
64
71
}
0 commit comments