Skip to content

Commit 33119f1

Browse files
author
jjcapparell
committed
feat: added GET /items/:id, POST /items, and DELETE/items/:id methods to MediaItemsController
1 parent 3a82490 commit 33119f1

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import org.springframework.web.bind.annotation.CrossOrigin;
1212
import org.springframework.web.bind.annotation.GetMapping;
1313
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.DeleteMapping;
16+
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.RequestBody;
18+
import org.springframework.web.bind.annotation.ResponseStatus;
19+
import org.springframework.http.HttpStatus;
1420

1521
@RestController
1622
@CrossOrigin
@@ -38,15 +44,16 @@ public MediaItemResponse getItem(@PathVariable String id) {
3844
if (item.isPresent()) {
3945
return MediaItemResponse.from(item.get());
4046
} else {
41-
throw new ItemNotFoundException(id);
47+
throw new IllegalArgumentException("Unknown media item ID: " + id);
4248
}
4349
}
4450

4551
// POST a new item to /items
4652
@PostMapping("/items")
47-
public MediaItemResponse addItem(@RequestBody AddMediaItemRequest request) {
48-
MediaItem newItem = request.toMediaItem();
49-
library.addItem(newItem); // Assuming the library has an addItem method
53+
public MediaItemResponse addItem(@RequestBody CreateMediaItemRequest request) {
54+
MediaItemRequest mediaItemRequest = request.getItem();
55+
MediaItem newItem = MediaItemRequest.asMediaItem(mediaItemRequest);
56+
library.addMediaItem(newItem, librarian); // Assuming the library has an addItem method
5057
return MediaItemResponse.from(newItem);
5158
}
5259

@@ -56,9 +63,9 @@ public MediaItemResponse addItem(@RequestBody AddMediaItemRequest request) {
5663
public void deleteItem(@PathVariable String id) {
5764
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
5865
if (items.isEmpty()) {
59-
throw new ItemNotFoundException(id);
66+
throw new IllegalArgumentException("This ID does not exsist to delete! ID: " + id);
6067
}
6168
MediaItem itemToDelete = items.iterator().next();
62-
library.removeItem(itemToDelete); // Assuming the library has a removeItem method
69+
library.removeMediaItem(itemToDelete, librarian); // Assuming the library has a removeItem method
6370
}
6471
}

0 commit comments

Comments
 (0)