Skip to content

chore: (WIP) add JBey @DeleteMapping request for lesson_26 #604

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

Closed
Closed
Show file tree
Hide file tree
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
@@ -1,6 +1,7 @@
package com.codedifferently.lesson26.web;

import java.util.List;

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove empty line, you should only be modifying the MediaItemsController

import lombok.Builder;
import lombok.Data;
import lombok.Singular;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.codedifferently.lesson26.web;

import com.codedifferently.lesson26.library.Librarian;
import com.codedifferently.lesson26.library.Library;
import com.codedifferently.lesson26.library.MediaItem;
import com.codedifferently.lesson26.library.search.SearchCriteria;
import java.io.IOException;
import java.util.List;
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.RestController;

import com.codedifferently.lesson26.library.Librarian;
import com.codedifferently.lesson26.library.Library;
import com.codedifferently.lesson26.library.MediaItem;
import com.codedifferently.lesson26.library.search.SearchCriteria;

@RestController
@CrossOrigin
public class MediaItemsController {
Expand All @@ -31,4 +35,18 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@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.

This method works well. I recommend changing the @PathVariable type from String to UUID to leverage Spring’s built-in validation and ensure invalid UUIDs are rejected automatically with a 400 Bad Request

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

if (items.isEmpty()) {
return ResponseEntity.notFound().build(); // Return 404 if item is not found
}

MediaItem itemToDelete = items.iterator().next();
library.removeMediaItem(itemToDelete, librarian); // Assuming there's a method to remove items in the library

return ResponseEntity.noContent().build(); // Return 204 No Content if deletion is successful
}
}
Loading