-
Notifications
You must be signed in to change notification settings - Fork 23
Feat lesson 26 - Library Springboot Integration #605
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
Dlafferty251
wants to merge
4
commits into
code-differently:main
from
Dlafferty251:feat-libray-functions
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
918a03a
feat: adds getters for getting a book by id, deleting a book by id an…
Dlafferty251 e2d7e41
fix: lombok dependency update (#600)
VicenteVigueras 01c63a3
feat: Adds get post and delete mapping for the MediaItemsController
Dlafferty251 7dd3a89
fix: gets rid of the valid parameter
Dlafferty251 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
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,18 +4,23 @@ | |
import com.codedifferently.lesson26.library.Library; | ||
import com.codedifferently.lesson26.library.MediaItem; | ||
import com.codedifferently.lesson26.library.search.SearchCriteria; | ||
import jakarta.validation.Valid; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
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; | ||
|
||
|
@@ -31,4 +36,44 @@ public ResponseEntity<GetMediaItemsResponse> getItems() { | |
var response = GetMediaItemsResponse.builder().items(responseItems).build(); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@GetMapping("/items/{id}") | ||
public ResponseEntity<MediaItemResponse> getItemById(@PathVariable UUID id) { | ||
SearchCriteria criteria = SearchCriteria.builder().id(id.toString()).build(); | ||
Set<MediaItem> items = library.search(criteria); | ||
|
||
if (items.isEmpty()) { | ||
return ResponseEntity.notFound().build(); | ||
} else { | ||
MediaItem item = items.iterator().next(); | ||
MediaItemResponse response = MediaItemResponse.from(item); | ||
return ResponseEntity.ok(response); | ||
} | ||
} | ||
|
||
@PostMapping("/items") | ||
public CreateMediaItemResponse addItem(@Valid @RequestBody CreateMediaItemRequest request) { | ||
MediaItemRequest itemCreation = request.getItem(); | ||
MediaItem item = MediaItemRequest.asMediaItem(itemCreation); | ||
library.addMediaItem(item, librarian); | ||
|
||
CreateMediaItemResponse response = | ||
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 a static method called from() in the MediaItemResponse class that would be more appropriate in this line: |
||
CreateMediaItemResponse.builder().item(getItemById(item.getId()).getBody()).build(); | ||
return response; | ||
} | ||
|
||
@DeleteMapping("/items/{id}") | ||
public ResponseEntity<Void> deleteItem(@PathVariable UUID id) { | ||
SearchCriteria criteria = SearchCriteria.builder().id(id.toString()).build(); | ||
Set<MediaItem> items = library.search(criteria); | ||
|
||
if (items.isEmpty()) { | ||
return ResponseEntity.notFound().build(); | ||
} else { | ||
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.
It looks like your branch is behind, there shouldn't be any modifications to this file