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
+ import jakarta .validation .Valid ;
7
8
import java .io .IOException ;
8
9
import java .util .List ;
9
10
import java .util .Set ;
11
+ import org .springframework .http .ResponseEntity ;
10
12
import org .springframework .web .bind .annotation .CrossOrigin ;
11
13
import org .springframework .web .bind .annotation .DeleteMapping ;
12
14
import org .springframework .web .bind .annotation .GetMapping ;
13
15
import org .springframework .web .bind .annotation .PathVariable ;
16
+ import org .springframework .web .bind .annotation .PostMapping ;
17
+ import org .springframework .web .bind .annotation .RequestBody ;
14
18
import org .springframework .web .bind .annotation .RestController ;
15
19
16
20
@ RestController
@@ -32,24 +36,33 @@ public GetMediaItemsResponse getItems() {
32
36
return response ;
33
37
}
34
38
35
- @ GetMapping ("/items" )
36
- public GetMediaItemsResponse postItems () {
37
- Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
38
- List <MediaItemResponse > responseItems = items .stream ().map (MediaItemResponse ::from ).toList ();
39
- return postItems ();
39
+ @ PostMapping ("/items" )
40
+ public ResponseEntity <?> postItems (@ Valid @ RequestBody CreateMediaItemRequest request ) {
41
+ MediaItemRequest itemRequest = request .getItem ();
42
+ MediaItem item = MediaItemRequest .asMediaItem (itemRequest );
43
+ library .addMediaItem (item , librarian );
44
+ MediaItemResponse response = MediaItemResponse .from (item );
45
+ return ResponseEntity .ok (CreateMediaItemResponse .builder ().item (response ).build ());
40
46
}
41
47
42
- @ GetMapping ("/items/:id " )
43
- public GetMediaItemsResponse getItemsId (@ PathVariable String id ) {
48
+ @ GetMapping ("/items/{id} " )
49
+ public ResponseEntity < GetMediaItemsResponse > getItemsId (@ PathVariable String id ) {
44
50
Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
51
+ if (items .isEmpty ()) {
52
+ return ResponseEntity .notFound ().build ();
53
+ }
45
54
MediaItemResponse responseItem = items .stream ().map (MediaItemResponse ::from ).iterator ().next ();
46
- return GetMediaItemsResponse .builder ().item (responseItem ).build ();
55
+ return ResponseEntity . ok ( GetMediaItemsResponse .builder ().item (responseItem ).build () );
47
56
}
48
57
49
- @ DeleteMapping (value = " {id}" )
50
- public void deletebById (@ PathVariable String id ) {
58
+ @ DeleteMapping ("/items/ {id}" )
59
+ public ResponseEntity <?> deletebById (@ PathVariable String id ) {
51
60
Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
52
61
MediaItem item = items .iterator ().next ();
62
+ if (items .isEmpty () || item == null ) {
63
+ ResponseEntity .notFound ().build ();
64
+ }
53
65
library .removeMediaItem (item , librarian );
66
+ return ResponseEntity .noContent ().build ();
54
67
}
55
68
}
0 commit comments