Skip to content
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 @@ -4,12 +4,19 @@
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.Optional;
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 +38,45 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@GetMapping("/items/{id}")
public ResponseEntity<GetMediaItemsResponse> getItemID(@PathVariable String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
// Optional can be an object or null ex. a:string | null;
// Optional here is saying if the first item ID is found grab it but if not keep it empty/null.
Optional<MediaItem> itemsId = items.stream().findFirst();
MediaItemResponse responseItem = null;
if (itemsId.isEmpty()) {
return ResponseEntity.notFound().build();
}
responseItem = MediaItemResponse.from(itemsId.get());
var response = GetMediaItemsResponse.builder().item(responseItem).build();
return ResponseEntity.ok(response);
}

@DeleteMapping("items/{id}")
public ResponseEntity<GetMediaItemsResponse> deleteItemId(@PathVariable String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
Optional<MediaItem> itemsId = items.stream().findFirst();
if (itemsId.isPresent()) {
library.removeMediaItem(itemsId.get(), librarian);
return ResponseEntity.noContent().build();
}
return ResponseEntity.notFound().build();
}

@PostMapping("/items")
public ResponseEntity<Map<String, MediaItemResponse>> addItem(
Copy link
Contributor

Choose a reason for hiding this comment

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

Could just use MediaItemResponse right?

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

return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
}
}
}