3
3
import com .codedifferently .lesson26 .library .Librarian ;
4
4
import com .codedifferently .lesson26 .library .Library ;
5
5
import com .codedifferently .lesson26 .library .MediaItem ;
6
+ import com .codedifferently .lesson26 .library .exceptions .MediaItemCheckedOutException ;
6
7
import com .codedifferently .lesson26 .library .search .SearchCriteria ;
8
+ import jakarta .validation .Valid ;
7
9
import java .io .IOException ;
8
10
import java .util .List ;
9
11
import java .util .Set ;
12
+ import java .util .UUID ;
13
+ import org .springframework .http .HttpStatus ;
10
14
import org .springframework .http .ResponseEntity ;
11
15
import org .springframework .web .bind .annotation .CrossOrigin ;
16
+ import org .springframework .web .bind .annotation .DeleteMapping ;
12
17
import org .springframework .web .bind .annotation .GetMapping ;
18
+ import org .springframework .web .bind .annotation .PathVariable ;
19
+ import org .springframework .web .bind .annotation .PostMapping ;
20
+ import org .springframework .web .bind .annotation .RequestBody ;
13
21
import org .springframework .web .bind .annotation .RestController ;
14
22
15
23
@ RestController
@@ -31,4 +39,56 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
31
39
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
32
40
return ResponseEntity .ok (response );
33
41
}
42
+
43
+ @ PostMapping ("/items" )
44
+ public ResponseEntity <CreateMediaItemResponse > createItem (
45
+ @ Valid @ RequestBody CreateMediaItemRequest createRequest ) {
46
+ MediaItemRequest itemRequest = createRequest .getItem ();
47
+ MediaItem newItem = MediaItemRequest .asMediaItem (itemRequest );
48
+ library .addMediaItem (newItem , this .librarian );
49
+ MediaItemResponse itemResponse = MediaItemResponse .from (newItem );
50
+ CreateMediaItemResponse response = CreateMediaItemResponse .builder ().item (itemResponse ).build ();
51
+ return ResponseEntity .ok (response );
52
+ }
53
+
54
+ @ GetMapping ("/items/{id}" )
55
+ public ResponseEntity <MediaItemResponse > getItemById (@ PathVariable UUID id ) {
56
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
57
+ MediaItem foundItem =
58
+ items .stream ().filter (item -> item .getId ().equals (id )).findFirst ().orElse (null );
59
+ if (foundItem != null ) {
60
+ MediaItemResponse itemResponse = MediaItemResponse .from (foundItem );
61
+ return ResponseEntity .ok (itemResponse );
62
+ } else {
63
+ return ResponseEntity .notFound ().build ();
64
+ }
65
+ }
66
+
67
+ @ DeleteMapping ("/items/{id}" )
68
+ public ResponseEntity <Void > deleteItem (@ PathVariable String id ) { // Takes String id
69
+ UUID itemUuid ;
70
+
71
+ try {
72
+ itemUuid = UUID .fromString (id );
73
+ } catch (IllegalArgumentException e ) {
74
+ return ResponseEntity .badRequest ().build ();
75
+ }
76
+
77
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().build ());
78
+ MediaItem foundItem =
79
+ items .stream ().filter (item -> item .getId ().equals (itemUuid )).findFirst ().orElse (null );
80
+
81
+ if (foundItem == null ) {
82
+ return ResponseEntity .notFound ().build ();
83
+ }
84
+
85
+ try {
86
+ library .removeMediaItem (itemUuid , this .librarian );
87
+ return ResponseEntity .noContent ().build ();
88
+ } catch (MediaItemCheckedOutException ex ) {
89
+ return ResponseEntity .status (HttpStatus .CONFLICT ).build ();
90
+ } catch (Exception e ) {
91
+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ();
92
+ }
93
+ }
34
94
}
0 commit comments