Skip to content

feat: adds POST, DELETE, and GET/{id} endpoints to MediaItemsController.java file #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
import java.io.IOException;
import java.util.List;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -27,8 +32,43 @@ public MediaItemsController(Library library) throws IOException {
@GetMapping("/items")
public ResponseEntity<GetMediaItemsResponse> getItems() {
Set<MediaItem> items = library.search(SearchCriteria.builder().build());
if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}
List<MediaItemResponse> responseItems = items.stream().map(MediaItemResponse::from).toList();
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@GetMapping("/items/{id}")
public ResponseEntity<GetMediaItemsResponse> getItem(@PathVariable("id") String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}
MediaItemResponse responseItem = MediaItemResponse.from(items.iterator().next());
GetMediaItemsResponse response = GetMediaItemsResponse.builder().item(responseItem).build();
return ResponseEntity.ok(response);
}

@PostMapping("/items")
public ResponseEntity<MediaItemResponse> postItem(@RequestBody CreateMediaItemRequest request) {
MediaItemRequest item = request.getItem();
MediaItem response = MediaItemRequest.asMediaItem(item);
MediaItemResponse itemResponse = MediaItemResponse.from(response);
CreateMediaItemResponse createItemResponse =
CreateMediaItemResponse.builder().item(itemResponse).build();
MediaItemResponse createdItem = createItemResponse.getItem();
return ResponseEntity.status(HttpStatus.CREATED).body(createdItem);
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable("id") String id) {
Set<MediaItem> item = library.search(SearchCriteria.builder().id(id).build());
if (item.isEmpty()) {
return ResponseEntity.notFound().build();
}
library.removeMediaItem(item.iterator().next(), librarian);
return ResponseEntity.noContent().build();
}
}
Loading