55import com .codedifferently .lesson26 .library .MediaItem ;
66import com .codedifferently .lesson26 .library .search .SearchCriteria ;
77
8- import io .swagger .v3 .oas .annotations .parameters .RequestBody ;
8+ import jakarta .validation .Valid ;
9+
10+ import org .springframework .web .bind .annotation .RequestBody ;
11+
912
1013import java .io .IOException ;
1114import java .util .List ;
1215import java .util .Set ;
16+ import java .util .UUID ;
17+
18+ import org .springframework .http .ResponseEntity ;
19+ import org .springframework .web .bind .MethodArgumentNotValidException ;
1320import org .springframework .web .bind .annotation .CrossOrigin ;
21+ import org .springframework .web .bind .annotation .DeleteMapping ;
1422import org .springframework .web .bind .annotation .GetMapping ;
23+ import org .springframework .web .bind .annotation .PathVariable ;
1524import org .springframework .web .bind .annotation .PostMapping ;
1625import org .springframework .web .bind .annotation .RestController ;
1726
@@ -34,21 +43,53 @@ public GetMediaItemsResponse getItems() {
3443 return response ;
3544 }
3645
37- @ GetMapping ("/items:id" )
38- public MediaItemResponse getItemById (){
39- return null ;
46+ @ GetMapping ("/items/{id}" )
47+ public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable ("id" ) UUID id ) {
48+ String stringId = id .toString ();
49+ SearchCriteria searchCriteria = SearchCriteria .builder ().id (stringId ).build ();
50+
51+ Set <MediaItem > foundItems = library .search (searchCriteria );
52+
53+ if (foundItems .isEmpty ()) {
54+ return ResponseEntity .notFound ().build ();
55+ }
56+
57+ MediaItem item = foundItems .iterator ().next ();
58+ MediaItemResponse response = MediaItemResponse .from (item );
59+
60+ return ResponseEntity .ok (response );
4061 }
4162
4263 @ PostMapping ("/items" )
43- public CreateMediaItemResponse createMediaItem (@ RequestBody MediaItemRequest itemRequest ) {
64+ public CreateMediaItemResponse createMediaItem (@ RequestBody @ Valid CreateMediaItemRequest request ) throws MethodArgumentNotValidException {
65+
66+ MediaItemRequest itemRequest = request .getItem ();
4467
4568
46- var request = CreateMediaItemRequest . builder (). item ( itemRequest ). build ( );
69+ System . out . println ( request );
4770
48- var item = MediaItemRequest .asMediaItem (request .getItem ());
71+
72+ var item = MediaItemRequest .asMediaItem (itemRequest );
4973
5074 library .addMediaItem (item , librarian );
5175
52- return null ;
76+ var response = CreateMediaItemResponse .builder ().item (getItemById (item .getId ()).getBody ()).build ();
77+
78+ return response ;
79+ }
80+
81+ @ DeleteMapping ("/items/{id}" )
82+ public ResponseEntity <Void > deleteItem (@ PathVariable ("id" ) UUID id ){
83+
84+
85+ if (getItemById (id ).getBody () == null ) {
86+ return ResponseEntity .notFound ().build ();
87+ }
88+
89+ library .removeMediaItem (id , librarian );
90+
91+
92+ return ResponseEntity .noContent ().build ();
93+
5394 }
5495}
0 commit comments