7
7
import jakarta .validation .Valid ;
8
8
import java .io .IOException ;
9
9
import java .util .List ;
10
+ import java .util .Map ;
10
11
import java .util .Optional ;
11
12
import java .util .Set ;
12
13
import java .util .UUID ;
13
- import org .springframework .http .HttpStatus ;
14
14
import org .springframework .http .ResponseEntity ;
15
15
import org .springframework .web .bind .annotation .CrossOrigin ;
16
16
import org .springframework .web .bind .annotation .DeleteMapping ;
@@ -41,16 +41,22 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
41
41
}
42
42
43
43
@ PostMapping ("/items" )
44
- public ResponseEntity <MediaItemResponse > createItem (
45
- @ Valid @ RequestBody MediaItemRequest request ) {
44
+ public ResponseEntity <Map <String , MediaItemResponse >> createItem (
45
+ @ Valid @ RequestBody MediaItemWrapper wrapper ) {
46
+
47
+ MediaItemRequest request = wrapper .getItem ();
48
+ if (request == null ) {
49
+ throw new IllegalArgumentException ("Item must be provided in request body" );
50
+ }
51
+
46
52
MediaItem newItem = MediaItemRequest .asMediaItem (request );
47
- Librarian librarian =
new Librarian (
"system" ,
"[email protected] " );
48
53
library .addMediaItem (newItem , librarian );
49
- return new ResponseEntity <>( MediaItemResponse .from (newItem ), HttpStatus . CREATED );
54
+ return ResponseEntity . ok ( Map . of ( "item" , MediaItemResponse .from (newItem )) );
50
55
}
51
56
52
57
@ GetMapping ("/items/{id}" )
53
- public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable UUID id ) {
58
+ public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable ("id" ) UUID id ) {
59
+
54
60
Set <MediaItem > allItems = library .search (SearchCriteria .builder ().build ());
55
61
56
62
Optional <MediaItem > itemOpt =
@@ -65,7 +71,7 @@ public ResponseEntity<MediaItemResponse> getItemById(@PathVariable UUID id) {
65
71
}
66
72
67
73
@ DeleteMapping ("/items/{id}" )
68
- public ResponseEntity <Void > deleteItemById (@ PathVariable UUID id ) {
74
+ public ResponseEntity <Void > deleteItemById (@ PathVariable ( "id" ) UUID id ) {
69
75
Optional <MediaItem > itemOpt =
70
76
library .search (SearchCriteria .builder ().build ()).stream ()
71
77
.filter (item -> item .getId ().equals (id ))
0 commit comments