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 .http .ResponseEntity ;
11
8
import org .springframework .web .bind .annotation .CrossOrigin ;
9
+ import org .springframework .web .bind .annotation .DeleteMapping ;
12
10
import org .springframework .web .bind .annotation .GetMapping ;
11
+ import org .springframework .web .bind .annotation .PathVariable ;
13
12
import org .springframework .web .bind .annotation .RestController ;
14
13
14
+ import com .codedifferently .lesson26 .library .Librarian ;
15
+ import com .codedifferently .lesson26 .library .Library ;
16
+ import com .codedifferently .lesson26 .library .MediaItem ;
17
+ import com .codedifferently .lesson26 .library .search .SearchCriteria ;
18
+
15
19
@ RestController
16
20
@ CrossOrigin
17
21
public class MediaItemsController {
@@ -31,4 +35,18 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
31
35
var response = GetMediaItemsResponse .builder ().items (responseItems ).build ();
32
36
return ResponseEntity .ok (response );
33
37
}
38
+
39
+ @ DeleteMapping ("/items/{id}" )
40
+ public ResponseEntity <Void > deleteItem (@ PathVariable String id ) {
41
+ Set <MediaItem > items = library .search (SearchCriteria .builder ().id (id ).build ());
42
+
43
+ if (items .isEmpty ()) {
44
+ return ResponseEntity .notFound ().build (); // Return 404 if item is not found
45
+ }
46
+
47
+ MediaItem itemToDelete = items .iterator ().next ();
48
+ library .removeMediaItem (itemToDelete , librarian ); // Assuming there's a method to remove items in the library
49
+
50
+ return ResponseEntity .noContent ().build (); // Return 204 No Content if deletion is successful
51
+ }
34
52
}
0 commit comments