-
Notifications
You must be signed in to change notification settings - Fork 29
feat: adds Trinitie's lesson_23 hw about Web APIs using REST #735
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bede4f6
feat: adds get items by id method
txjackson252 19908ea
feat: adds post mapping method
txjackson252 4558d69
feat: adds delete mapping method
txjackson252 050cac4
refactor: changes a bit of the delete method
txjackson252 2478379
refactor: makes code simpler and doing spotlessApply
txjackson252 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,18 @@ | |
| 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.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 | ||
|
|
@@ -31,4 +37,40 @@ public ResponseEntity<GetMediaItemsResponse> getItems() { | |
| var response = GetMediaItemsResponse.builder().items(responseItems).build(); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/items/{id}") | ||
| public ResponseEntity<MediaItemResponse> 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(); | ||
| return ResponseEntity.ok(MediaItemResponse.from(item)); | ||
| } | ||
|
|
||
| @PostMapping("/items") | ||
| public ResponseEntity<?> addNewItem( | ||
| @Valid @RequestBody(required = false) CreateMediaItemRequest request) { | ||
| if (request == null || request.getItem() == null) { | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
| try { | ||
| MediaItem item = MediaItemRequest.asMediaItem(request.getItem()); | ||
| library.addMediaItem(item, librarian); | ||
| return ResponseEntity.ok(Map.of("item", MediaItemResponse.from(item))); | ||
| } catch (IllegalArgumentException e) { | ||
|
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 need this either if you just use the |
||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
| } | ||
|
|
||
| @DeleteMapping("/items/{id}") | ||
| public ResponseEntity<Void> deleteItem(@PathVariable String id) { | ||
|
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. nit: Better to take a |
||
| Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build()); | ||
| if (items.isEmpty()) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| MediaItem item = items.iterator().next(); | ||
| library.removeMediaItem(item, librarian); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't need this.