5
5
import com .codedifferently .lesson26 .library .MediaItem ;
6
6
import com .codedifferently .lesson26 .library .search .SearchCriteria ;
7
7
8
- import io .swagger .v3 .oas .annotations .parameters .RequestBody ;
8
+ import jakarta .validation .Valid ;
9
+
10
+ import org .springframework .web .bind .annotation .RequestBody ;
11
+
9
12
10
13
import java .io .IOException ;
11
14
import java .util .List ;
12
15
import java .util .Set ;
16
+ import java .util .UUID ;
17
+
18
+ import org .springframework .http .ResponseEntity ;
19
+ import org .springframework .web .bind .MethodArgumentNotValidException ;
13
20
import org .springframework .web .bind .annotation .CrossOrigin ;
21
+ import org .springframework .web .bind .annotation .DeleteMapping ;
14
22
import org .springframework .web .bind .annotation .GetMapping ;
23
+ import org .springframework .web .bind .annotation .PathVariable ;
15
24
import org .springframework .web .bind .annotation .PostMapping ;
16
25
import org .springframework .web .bind .annotation .RestController ;
17
26
@@ -34,21 +43,53 @@ public GetMediaItemsResponse getItems() {
34
43
return response ;
35
44
}
36
45
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 );
40
61
}
41
62
42
63
@ 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 ();
44
67
45
68
46
- var request = CreateMediaItemRequest . builder (). item ( itemRequest ). build ( );
69
+ System . out . println ( request );
47
70
48
- var item = MediaItemRequest .asMediaItem (request .getItem ());
71
+
72
+ var item = MediaItemRequest .asMediaItem (itemRequest );
49
73
50
74
library .addMediaItem (item , librarian );
51
75
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
+
53
94
}
54
95
}
0 commit comments