-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Implement REST API endpoints for Library Management System #738
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
thusser252
wants to merge
2
commits into
code-differently:main
from
thusser252:th_lesson23_single_file
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||
|
|
@@ -24,11 +30,67 @@ public MediaItemsController(Library library) throws IOException { | |
| this.librarian = library.getLibrarians().stream().findFirst().orElseThrow(); | ||
| } | ||
|
|
||
| @GetMapping("/") | ||
| public ResponseEntity<Map<String, Object>> welcome() { | ||
| Map<String, Object> response = | ||
| Map.of( | ||
| "message", "Welcome to the Library Management API!", | ||
| "version", "1.0.0", | ||
| "endpoints", | ||
| Map.of( | ||
| "DELETE /items/{id}", "Delete a media item", | ||
| "POST /items", "Create a new media item", | ||
| "GET /items", "Get all media items", | ||
| "GET /items/{id}", "Get a specific media item"), | ||
| "documentation", | ||
| Map.of( | ||
| "note", "Built-in Swagger/OpenAPI documentation available if configured"), | ||
| "statistics", | ||
| Map.of( | ||
| "totalItems", library.search(SearchCriteria.builder().build()).size(), | ||
| "totalLibrarians", library.getLibrarians().size())); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/items") | ||
| public ResponseEntity<GetMediaItemsResponse> getItems() { | ||
| Set<MediaItem> items = library.search(SearchCriteria.builder().build()); | ||
| List<MediaItemResponse> responseItems = items.stream().map(MediaItemResponse::from).toList(); | ||
| var response = GetMediaItemsResponse.builder().items(responseItems).build(); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/items/{id}") | ||
| public ResponseEntity<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(); | ||
| MediaItemResponse response = MediaItemResponse.from(item); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/items") | ||
| public ResponseEntity<CreateMediaItemResponse> createItem( | ||
| @Valid @RequestBody CreateMediaItemRequest request) { | ||
| MediaItem mediaItem = MediaItemRequest.asMediaItem(request.getItem()); | ||
| library.addMediaItem(mediaItem, librarian); | ||
| MediaItemResponse responseItem = MediaItemResponse.from(mediaItem); | ||
| var response = CreateMediaItemResponse.builder().item(responseItem).build(); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
|
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. Extra line breaks |
||
|
|
||
|
|
||
| @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(); | ||
| } | ||
| MediaItem item = items.iterator().next(); | ||
| library.removeMediaItem(item, librarian); | ||
| 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.
Not in the requirements, please remove.