Skip to content

Feat: adds lesson 26 , amiyah jones' MediaItemsController #626

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
wants to merge 86 commits into from
Closed
Changes from 79 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
8cc92d0
Create README.md
AmiyahJo Sep 24, 2024
50b1861
Delete lesson_00/amiyahjones/README.md
AmiyahJo Sep 24, 2024
27d57c0
Merge branch 'code-differently:main' into main
AmiyahJo Sep 25, 2024
fa3c700
Merge branch 'code-differently:main' into main
AmiyahJo Sep 26, 2024
7c2d053
Merge branch 'code-differently:main' into main
AmiyahJo Sep 26, 2024
faedd1b
Merge branch 'code-differently:main' into main
AmiyahJo Sep 26, 2024
19a4187
Merge branch 'code-differently:main' into main
AmiyahJo Sep 26, 2024
7711266
Merge branch 'code-differently:main' into main
AmiyahJo Sep 27, 2024
9a10c4c
Merge branch 'code-differently:main' into main
AmiyahJo Sep 27, 2024
3ea50c4
Merge branch 'code-differently:main' into main
AmiyahJo Sep 27, 2024
d7b772d
Merge branch 'code-differently:main' into main
AmiyahJo Sep 27, 2024
de46b67
Merge branch 'code-differently:main' into main
AmiyahJo Sep 27, 2024
b891561
Merge branch 'code-differently:main' into main
AmiyahJo Sep 28, 2024
7c9f2db
Merge branch 'code-differently:main' into main
AmiyahJo Sep 30, 2024
ff3ba35
Merge branch 'code-differently:main' into main
AmiyahJo Sep 30, 2024
457feea
Merge branch 'code-differently:main' into main
AmiyahJo Sep 30, 2024
5868f9d
Merge branch 'code-differently:main' into main
AmiyahJo Oct 1, 2024
28eacd4
Merge branch 'code-differently:main' into main
AmiyahJo Oct 1, 2024
bca4933
Merge branch 'code-differently:main' into main
AmiyahJo Oct 1, 2024
9ff0f9e
Merge branch 'code-differently:main' into main
AmiyahJo Oct 1, 2024
8d02849
Merge branch 'code-differently:main' into main
AmiyahJo Oct 2, 2024
6dd3a43
Merge branch 'code-differently:main' into main
AmiyahJo Oct 2, 2024
95f56a5
Merge branch 'code-differently:main' into main
AmiyahJo Oct 3, 2024
0f4dbac
Merge branch 'code-differently:main' into main
AmiyahJo Oct 4, 2024
a3b9984
Merge branch 'code-differently:main' into main
AmiyahJo Oct 4, 2024
54cd199
Merge branch 'code-differently:main' into main
AmiyahJo Oct 4, 2024
edbe7c0
Merge branch 'code-differently:main' into main
AmiyahJo Oct 4, 2024
7f0a084
Merge branch 'code-differently:main' into main
AmiyahJo Oct 7, 2024
a905b74
Merge branch 'code-differently:main' into main
AmiyahJo Oct 8, 2024
2d69531
Merge branch 'code-differently:main' into main
AmiyahJo Oct 8, 2024
df48fbd
Merge branch 'code-differently:main' into main
AmiyahJo Oct 8, 2024
c972d14
Merge branch 'code-differently:main' into main
AmiyahJo Oct 9, 2024
bc3907c
Merge branch 'code-differently:main' into main
AmiyahJo Oct 9, 2024
c3eabbb
Merge branch 'code-differently:main' into main
AmiyahJo Oct 9, 2024
9fff7d0
Merge branch 'code-differently:main' into main
AmiyahJo Oct 11, 2024
4c57926
Merge branch 'code-differently:main' into main
AmiyahJo Oct 11, 2024
8dd5d42
Merge branch 'code-differently:main' into main
AmiyahJo Oct 14, 2024
18f65ee
Merge branch 'code-differently:main' into main
AmiyahJo Oct 14, 2024
2dd1645
Merge branch 'code-differently:main' into main
AmiyahJo Oct 14, 2024
0de8601
Merge branch 'code-differently:main' into main
AmiyahJo Oct 14, 2024
85dd364
Merge branch 'code-differently:main' into main
AmiyahJo Oct 16, 2024
0afda26
Merge branch 'code-differently:main' into main
AmiyahJo Oct 16, 2024
f31e8d9
Merge branch 'code-differently:main' into main
AmiyahJo Oct 16, 2024
3e24069
Merge branch 'code-differently:main' into main
AmiyahJo Oct 17, 2024
8600f6c
Merge branch 'code-differently:main' into main
AmiyahJo Oct 17, 2024
563b076
Merge branch 'code-differently:main' into main
AmiyahJo Oct 18, 2024
b01a324
Merge branch 'code-differently:main' into main
AmiyahJo Oct 18, 2024
697a72d
Merge branch 'code-differently:main' into main
AmiyahJo Oct 21, 2024
92891f6
Merge branch 'code-differently:main' into main
AmiyahJo Oct 21, 2024
17fdee5
Merge branch 'code-differently:main' into main
AmiyahJo Oct 22, 2024
cf7e6f2
Merge branch 'code-differently:main' into main
AmiyahJo Oct 23, 2024
a131bdc
Merge branch 'code-differently:main' into main
AmiyahJo Oct 23, 2024
c46d32f
Merge branch 'code-differently:main' into main
AmiyahJo Oct 24, 2024
651fa01
Merge branch 'code-differently:main' into main
AmiyahJo Oct 26, 2024
e075fb7
Merge branch 'code-differently:main' into main
AmiyahJo Oct 27, 2024
ec7c3d1
Merge branch 'code-differently:main' into main
AmiyahJo Oct 28, 2024
186c10e
Merge branch 'code-differently:main' into main
AmiyahJo Oct 29, 2024
e13bae6
Merge remote-tracking branch 'refs/remotes/origin/main'
Oct 30, 2024
8f999c6
Merge remote-tracking branch 'refs/remotes/origin/main'
Oct 30, 2024
af811ee
Merge branch 'code-differently:main' into main
AmiyahJo Nov 1, 2024
8676657
Merge branch 'code-differently:main' into main
AmiyahJo Nov 1, 2024
25f01e6
Merge branch 'code-differently:main' into main
AmiyahJo Nov 3, 2024
e132f49
Merge branch 'code-differently:main' into main
AmiyahJo Nov 5, 2024
aecfedd
Merge branch 'code-differently:main' into main
AmiyahJo Nov 5, 2024
9730f82
Merge branch 'code-differently:main' into main
AmiyahJo Nov 6, 2024
4f181fd
Merge branch 'code-differently:main' into main
AmiyahJo Nov 11, 2024
d6c62f9
Merge branch 'code-differently:main' into main
AmiyahJo Nov 13, 2024
52373df
Merge branch 'code-differently:main' into main
AmiyahJo Nov 13, 2024
af515aa
Merge branch 'code-differently:main' into main
AmiyahJo Nov 13, 2024
8cb5bfb
Merge branch 'code-differently:main' into main
AmiyahJo Nov 14, 2024
2ef0eb8
Merge branch 'code-differently:main' into main
AmiyahJo Nov 18, 2024
f63b274
Merge branch 'code-differently:main' into main
AmiyahJo Nov 19, 2024
226cc27
Merge branch 'code-differently:main' into main
AmiyahJo Nov 20, 2024
6158bf8
Merge branch 'code-differently:main' into main
AmiyahJo Nov 20, 2024
21119ab
Merge branch 'code-differently:main' into main
AmiyahJo Nov 20, 2024
d11b3b9
Merge branch 'code-differently:main' into main
AmiyahJo Nov 22, 2024
f8b8f39
Merge branch 'code-differently:main' into main
AmiyahJo Nov 24, 2024
7e05f49
feat: adds media item controller methods
Nov 25, 2024
46c3e11
fix: ./gradlew :api_app:spotlessApply
Nov 25, 2024
301638e
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Nov 25, 2024
c0de66f
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Nov 25, 2024
27aa27d
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Nov 26, 2024
bb55835
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Nov 27, 2024
1928540
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Nov 29, 2024
13038e5
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Dec 2, 2024
33d0931
Merge branch 'code-differently:main' into feature/lesson_26
AmiyahJo Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
import com.codedifferently.lesson26.library.Librarian;
import com.codedifferently.lesson26.library.Library;
import com.codedifferently.lesson26.library.MediaItem;
import com.codedifferently.lesson26.library.exceptions.MediaItemCheckedOutException;
import com.codedifferently.lesson26.library.search.SearchCriteria;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpStatus;
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.RestController;

@RestController
Expand All @@ -29,4 +38,53 @@ public GetMediaItemsResponse getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return response;
}

@GetMapping("/items/{id}")
public ResponseEntity<MediaItemResponse> getItemById(@PathVariable("id") String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
if (items.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
MediaItem item = items.iterator().next();
MediaItemResponse response = MediaItemResponse.from(item);
return ResponseEntity.ok(response);
}

@PostMapping("/items")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only error is that it expects the status 200 , but gets 400. Fixing this has been an issue
chatgpt expects me to manually include a book if statement , or a bindingResult <- both still gives the same 400 error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you had two problems. For one, you were using the wrong @RequestBody annotation so the request message was not being read correctly. Secondly, you should have preferred to accept the CreateMediaItemRequest type which is the type of request the test anticipates sending.

public ResponseEntity<?> addItem(@Valid @RequestBody MediaItemRequest request) {
if (request == null) {
return ResponseEntity.badRequest().body(Map.of("errors", List.of("Missing required fields")));
}

MediaItem newItem;
try {
newItem = MediaItemRequest.asMediaItem(request);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest()
.body(Map.of("errors", List.of("Unknown media item type: " + request.getType())));
}

library.addMediaItem(newItem, new Librarian("Jane Doe", "[email protected]"));

return ResponseEntity.ok(Map.of("item", Map.of("id", newItem.getId().toString())));
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Map<String, List<String>>> deleteItem(@PathVariable("id") String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
if (items.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

MediaItem item = items.iterator().next();

try {
library.removeMediaItem(item, librarian);
} catch (MediaItemCheckedOutException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("errors", List.of(e.getMessage())));
}

return ResponseEntity.noContent().build();
}
}
Loading