Skip to content

feat: adds new methods for MediaItemsController.java for lesson 26 #679

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
wants to merge 2 commits into from
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 @@ -5,13 +5,24 @@
import com.codedifferently.lesson26.library.MediaItem;
import com.codedifferently.lesson26.library.search.SearchCriteria;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/items")
@CrossOrigin
public class MediaItemsController {
private final Library library;
Expand All @@ -22,11 +33,80 @@ public MediaItemsController(Library library) throws IOException {
this.librarian = library.getLibrarians().stream().findFirst().orElseThrow();
}

@GetMapping("/items")
public GetMediaItemsResponse getItems() {
@GetMapping()
public ResponseEntity<GetMediaItemsResponse> getItemById() {
Set<MediaItem> items = library.search(SearchCriteria.builder().build());
if (items.isEmpty()) {
ResponseEntity.noContent();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nope, should return GetMediaItmesResponse with an empty list of items. Anything else would be unexpected and unusual.

}
List<MediaItemResponse> responseItems = items.stream().map(MediaItemResponse::from).toList();
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return response;
return ResponseEntity.ok(response);
}

@GetMapping(value = "/{id}")
public ResponseEntity<MediaItemResponse> getItemById(@PathVariable("id") UUID id) {

System.out.println(id.toString());

Set<MediaItem> items = library.search(SearchCriteria.builder().id(id.toString()).build());
Optional<MediaItem> matchedItem =
items.stream().filter(item -> item.getId().equals(id)).findFirst();
System.out.println("items");
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove debugging statements.

System.out.println(items);
System.out.println(matchedItem);
System.out.println("mathcedItems");
return matchedItem
.map(
item -> {
MediaItemResponse responseItem = MediaItemResponse.from(item);
return ResponseEntity.ok(responseItem);
})
.orElse(ResponseEntity.notFound().build());
}

@PostMapping()
public ResponseEntity<?> postItems(@RequestBody CreateMediaItemRequest requestBody) {
List<String> errorsList = new ArrayList<>();
MediaItemRequest request = requestBody.getItem();
if (requestBody.getItem() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't need any of this validation code if you just use the @Valid attribute on the requestBody argument.

errorsList.add("Cannot enter null");
} else {
if ((request.getTitle() == null || request.getTitle().isBlank())
&& (request.getType() == null || request.getType().isBlank())) {
errorsList.add("title and type cannot be null or blank");
} else {
if (request.getTitle() == null || request.getTitle().isBlank()) {
errorsList.add("title cannot be null or blank");
} else {
if (request.getType() == null || request.getType().isBlank()) {
errorsList.add("type cannot be null or blank");
}
}
}
}
if (!errorsList.isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(
new Object() {
public final List<String> errors = errorsList;
});
}
MediaItem newMediaItem = MediaItemRequest.asMediaItem(request);
library.addMediaItem(newMediaItem, librarian);
return ResponseEntity.ok(requestBody);
}

@DeleteMapping(value = "/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable("id") UUID id) {
Set<MediaItem> test1 = library.search(SearchCriteria.builder().build());
Set<MediaItem> item = library.search(SearchCriteria.builder().id(id.toString()).build());
if (item.isEmpty()) {
return ResponseEntity.notFound().build();
} else if (!test1.contains(item.iterator().next()) || item.iterator().next() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you doing this? Doesn't seem necessary at all. My guess is removing this code (and test1) above won't change your results at all.

return ResponseEntity.notFound().build();
}
library.removeMediaItem(id, librarian);
return ResponseEntity.noContent().build();
}
}
Loading