1
1
package com .codedifferently .lesson26 .web ;
2
2
3
- import com .codedifferently .lesson26 .library .Librarian ;
4
- import com .codedifferently .lesson26 .library .Library ;
5
- import com .codedifferently .lesson26 .library .MediaItem ;
6
- import com .codedifferently .lesson26 .library .search .SearchCriteria ;
7
3
import java .io .IOException ;
8
4
import java .util .List ;
5
+ import java .util .Map ;
9
6
import java .util .Set ;
7
+
8
+ import org .springframework .http .HttpStatus ;
9
+ import org .springframework .http .ResponseEntity ;
10
10
import org .springframework .web .bind .annotation .CrossOrigin ;
11
+ import org .springframework .web .bind .annotation .DeleteMapping ;
11
12
import org .springframework .web .bind .annotation .GetMapping ;
13
+ import org .springframework .web .bind .annotation .PathVariable ;
14
+ import org .springframework .web .bind .annotation .PostMapping ;
12
15
import org .springframework .web .bind .annotation .RestController ;
13
16
17
+ import com .codedifferently .lesson26 .library .Librarian ;
18
+ import com .codedifferently .lesson26 .library .Library ;
19
+ import com .codedifferently .lesson26 .library .MediaItem ;
20
+ import com .codedifferently .lesson26 .library .exceptions .MediaItemCheckedOutException ;
21
+ import com .codedifferently .lesson26 .library .search .SearchCriteria ;
22
+
23
+ import io .swagger .v3 .oas .annotations .parameters .RequestBody ;
24
+ import jakarta .validation .Valid ;
25
+
14
26
@ RestController
15
27
@ CrossOrigin
16
28
public class MediaItemsController {
@@ -29,4 +41,52 @@ public GetMediaItemsResponse getItems() {
29
41
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
30
42
return response ;
31
43
}
44
+
45
+ @ GetMapping ("/items/{id}" )
46
+ public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable ("id" ) String id ) {
47
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
48
+ if (items .isEmpty ()) {
49
+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
50
+ }
51
+ MediaItem item = items .iterator ().next ();
52
+ MediaItemResponse response = MediaItemResponse .from (item );
53
+ return ResponseEntity .ok (response );
54
+ }
55
+
56
+ @ PostMapping ("/items" )
57
+ public ResponseEntity <?> addItem (@ Valid @ RequestBody MediaItemRequest request ) {
58
+ if (request == null ) {
59
+ return ResponseEntity .badRequest ().body (Map .of ("errors" , List .of ("Missing required fields" )));
60
+ }
61
+
62
+ MediaItem newItem ;
63
+ try {
64
+ newItem = MediaItemRequest .asMediaItem (request );
65
+ } catch (IllegalArgumentException e ) {
66
+ return ResponseEntity .badRequest ().body (Map .of ("errors" , List .of ("Unknown media item type: " + request .getType ())));
67
+ }
68
+
69
+ library .
addMediaItem (
newItem ,
new Librarian (
"Jane Doe" ,
"[email protected] " ));
70
+
71
+ return ResponseEntity .ok (Map .of ("item" , Map .of ("id" , newItem .getId ().toString ())));
72
+ }
73
+
74
+ @ DeleteMapping ("/items/{id}" )
75
+ public ResponseEntity <Map <String , List <String >>> deleteItem (@ PathVariable ("id" ) String id ) {
76
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
77
+ if (items .isEmpty ()) {
78
+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
79
+ }
80
+
81
+ MediaItem item = items .iterator ().next ();
82
+
83
+ try {
84
+ library .removeMediaItem (item , librarian );
85
+ } catch (MediaItemCheckedOutException e ) {
86
+ return ResponseEntity .status (HttpStatus .BAD_REQUEST )
87
+ .body (Map .of ("errors" , List .of (e .getMessage ())));
88
+ }
89
+
90
+ return ResponseEntity .noContent ().build ();
91
+ }
32
92
}
0 commit comments