Skip to content

Commit cf513ef

Browse files
committed
feat: add getItemById and deleteItemById endpoints to MediaItemsController
1 parent ba3fbe6 commit cf513ef

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lesson_26/api/java/api_app/src/main/java/com/codedifferently/lesson26/web/MediaItemsController.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.Set;
1010
import org.springframework.http.ResponseEntity;
1111
import org.springframework.web.bind.annotation.CrossOrigin;
12+
import org.springframework.web.bind.annotation.DeleteMapping;
1213
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PostMapping;
1315
import org.springframework.web.bind.annotation.RestController;
1416

1517
@RestController
@@ -31,4 +33,36 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
3133
var response = GetMediaItemsResponse.builder().items(responseItems).build();
3234
return ResponseEntity.ok(response);
3335
}
36+
37+
/**
38+
* @param id
39+
* @return
40+
*/
41+
@PostMapping("/items/{id}")
42+
public ResponseEntity<MediaItemResponse> getItemById(String id) {
43+
MediaItem item = librarian.getItemById(id);
44+
MediaItemResponse response = MediaItemResponse.from(item);
45+
return ResponseEntity.ok(response);
46+
}
47+
48+
/**
49+
* @param id
50+
* @return
51+
*/
52+
@DeleteMapping("/items/{id}")
53+
public ResponseEntity<Void> deleteItemById(String id) {
54+
SearchCriteria criteria = SearchCriteria.builder().id(id).build();
55+
Set<MediaItem> items = library.search(criteria);
56+
if (items.isEmpty()) {
57+
return ResponseEntity.notFound().build();
58+
}
59+
MediaItem item = items.iterator().next();
60+
61+
librarian.deleteItemById(id);
62+
return ResponseEntity.noContent().build();
63+
64+
65+
}
66+
3467
}
68+

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)