Skip to content
Closed
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 @@ -4,12 +4,18 @@
import com.codedifferently.lesson23.library.Library;
import com.codedifferently.lesson23.library.MediaItem;
import com.codedifferently.lesson23.library.search.SearchCriteria;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 @@ -31,4 +37,47 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@GetMapping("/items/{id}")
public ResponseEntity<GetMediaItemsResponse> getItemsById(@PathVariable String id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be getItemById and should just return a MediaItemResponse.

SearchCriteria criteria = SearchCriteria.builder().id(id).build();
Set<MediaItem> items = library.search(criteria);

if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}

MediaItemResponse responseItem = MediaItemResponse.from(items.iterator().next());
var response = GetMediaItemsResponse.builder().item(responseItem).build();
return ResponseEntity.ok(response);
}

@PostMapping("/items")
public ResponseEntity<Map<String, MediaItemResponse>> addItem(
@Valid @RequestBody CreateMediaItemRequest request) {
try {
MediaItem item = MediaItemRequest.asMediaItem(request.getItem());
library.addMediaItem(item, librarian);
MediaItemResponse responseItem = MediaItemResponse.from(item);
Map<String, MediaItemResponse> response = Map.of("item", responseItem);

return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this

return ResponseEntity.badRequest().build();
}
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable String id) {
SearchCriteria criteria = SearchCriteria.builder().id(id).build();
Set<MediaItem> items = library.search(criteria);

if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}

MediaItem item = items.iterator().next();
library.removeMediaItem(item, librarian);
return ResponseEntity.noContent().build();
}
}