-
Notifications
You must be signed in to change notification settings - Fork 29
Feat: Lesson 23 adds HTTP methods using REST API #744
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra line break