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 ;
9
5
import java .util .Set ;
6
+
10
7
import org .springframework .web .bind .annotation .CrossOrigin ;
11
8
import org .springframework .web .bind .annotation .GetMapping ;
12
9
import org .springframework .web .bind .annotation .RestController ;
13
10
11
+ import com .codedifferently .lesson26 .library .Librarian ;
12
+ import com .codedifferently .lesson26 .library .Library ;
13
+ import com .codedifferently .lesson26 .library .MediaItem ;
14
+ import com .codedifferently .lesson26 .library .search .SearchCriteria ;
15
+
14
16
@ RestController
15
17
@ CrossOrigin
16
18
public class MediaItemsController {
@@ -29,4 +31,29 @@ public GetMediaItemsResponse getItems() {
29
31
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
30
32
return response ;
31
33
}
34
+ @ PostMapping ("/items" )
35
+ public CreateMediaItemResponse createItem (@ RequestBody CreateMediaItemRequest request ) {
36
+ if (request == null || request .getItem () == null ) {
37
+ throw new IllegalArgumentException ("Invalid request body" );
38
+ }
39
+ MediaItem newItem = request .toMediaItem ();
40
+ library .addItem (newItem );
41
+ return new CreateMediaItemResponse (newItem );
42
+ }
43
+ @ GetMapping ("/items/{id}" )
44
+ public MediaItemResponse getItemById (@ PathVariable String id ) {
45
+ MediaItem item = library .getItemById (id );
46
+ if (item == null ) {
47
+ throw new MediaItemNotFoundException ("Media item not found" );
48
+ }
49
+ return MediaItemResponse .from (item );
50
+ }
51
+ @ DeleteMapping ("/items/{id}" )
52
+ public ResponseEntity <Void > deleteItem (@ PathVariable String id ) {
53
+ boolean deleted = library .deleteItem (id );
54
+ if (!deleted ) {
55
+ throw new MediaItemNotFoundException ("Item not found" );
56
+ }
57
+ return ResponseEntity .noContent ().build ();
58
+ }
32
59
}
0 commit comments