Skip to content
Open
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.lesson23.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.

Extra line break

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

import com.codedifferently.lesson23.library.Librarian;
import com.codedifferently.lesson23.library.Library;
import com.codedifferently.lesson23.library.MediaItem;
import com.codedifferently.lesson23.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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

import jakarta.validation.Valid;

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

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

@PostMapping("/items")
public ResponseEntity<CreateMediaItemResponse> addItem(@Valid @RequestBody CreateMediaItemRequest request) {
try {
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 actually need try/catch here. The controller will just do the right thing and return an appropriate error response.

MediaItem mediaItem = MediaItemRequest.asMediaItem(request.getItem());
library.addMediaItem(mediaItem, librarian);
CreateMediaItemResponse response = CreateMediaItemResponse.builder()
.item(MediaItemResponse.from(mediaItem))
.build();
return ResponseEntity.ok(response);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not always true, so this is not the right condition under which to return a bad request response.

}
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable String id) {
try {
java.util.UUID uuid = java.util.UUID.fromString(id);
library.removeMediaItem(uuid, librarian);
return ResponseEntity.noContent().build();
} catch (IllegalArgumentException e) {
return ResponseEntity.notFound().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessarily true that an exception was caught here because the item was not found. Use a more specific exception or change the error you return here to 500.

}
}
}
Loading