-
Notifications
You must be signed in to change notification settings - Fork 29
Feat/lesson 23 hw toby evans #745
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
Open
tobyev
wants to merge
5
commits into
main
Choose a base branch
from
feat/lesson-23-hw-toby-evans
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e4cda4
feat(web): implement GET/POST/DELETE endpoints for MediaItemsControll…
tobyev 2af5e8e
feat(web): implement GET/POST/DELETE endpoints for MediaItemsControll…
tobyev aa76a82
feat(web): add request/response DTOs and wire controller for lesson 23
tobyev eb6c79d
chore(lesson-23): remove unused response DTOs; keep controller + requ…
tobyev 1b7619e
feat(lesson-23): wrap single-item responses as {item: ...}; keep Libr…
tobyev 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
34 changes: 34 additions & 0 deletions
34
.../api/java/api_app/src/main/java/com/codedifferently/lesson23/web/AddMediaItemRequest.java
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.codedifferently.lesson23.web; | ||
| import java.util.UUID; | ||
|
|
||
| import com.codedifferently.lesson23.library.MediaItem; | ||
| import com.codedifferently.lesson23.library.Book; | ||
| import java.util.List; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| @Value | ||
| @Builder | ||
| public class AddMediaItemRequest { | ||
| @NotNull | ||
| Item item; | ||
|
|
||
| @Value | ||
| @Builder | ||
| public static class Item { | ||
| String id; | ||
| String type; | ||
| String title; | ||
| String isbn; | ||
| List<String> authors; | ||
| Integer pages; | ||
|
|
||
| public MediaItem toDomain() { | ||
| int p = pages == null ? 0 : pages; | ||
| return new Book(UUID.fromString(id), title, isbn, authors, p); | ||
| } | ||
| } | ||
|
|
||
| public Item getItem() { return item; } | ||
| } |
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 |
|---|---|---|
| @@ -1,27 +1,33 @@ | ||
| package com.codedifferently.lesson23.web; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import com.codedifferently.lesson23.library.Librarian; | ||
| import java.util.UUID; | ||
|
|
||
| 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.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 | ||
| @CrossOrigin | ||
| public class MediaItemsController { | ||
|
|
||
| private final Library library; | ||
| private final Librarian librarian; | ||
|
|
||
| public MediaItemsController(Library library) throws IOException { | ||
| this.library = library; | ||
| this.librarian = library.getLibrarians().stream().findFirst().orElseThrow(); | ||
| } | ||
|
|
||
| @GetMapping("/items") | ||
|
|
@@ -31,4 +37,33 @@ public ResponseEntity<GetMediaItemsResponse> getItems() { | |
| var response = GetMediaItemsResponse.builder().items(responseItems).build(); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/items/{id}") | ||
| public ResponseEntity<Map<String, MediaItemResponse>> getItem(@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 body = Collections.singletonMap("item", MediaItemResponse.from(item)); | ||
| return ResponseEntity.ok(body); | ||
| } | ||
|
|
||
| @PostMapping("/items") | ||
| public ResponseEntity<Map<String, MediaItemResponse>> addItem(@Valid @RequestBody AddMediaItemRequest request) { | ||
|
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. Use the existing |
||
| MediaItem item = request.getItem().toDomain(); | ||
| library.addMediaItem(item, new Librarian("System", "[email protected]")); | ||
|
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. There's already a |
||
| var body = Collections.singletonMap("item", MediaItemResponse.from(item)); | ||
| return ResponseEntity.ok(body); | ||
| } | ||
|
|
||
| @DeleteMapping("/items/{id}") | ||
| public ResponseEntity<Void> deleteItem(@PathVariable String id) { | ||
| Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build()); | ||
| if (items.isEmpty()) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| library.removeMediaItem(UUID.fromString(id), new Librarian("System", "[email protected]")); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Should just return
MediaItemResponse.