-
Notifications
You must be signed in to change notification settings - Fork 20
Feat: Added functions postItems, getMediaItem, and deleteMediaItem #362
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
Moibrahi7
wants to merge
13
commits into
code-differently:main
Choose a base branch
from
Moibrahi7:lesson_16
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
13 commits
Select commit
Hold shift + click to select a range
261da03
Feat: Added functions postItems, getMediaItem, and deleteMediaItem
78f1ab8
Merge branch 'code-differently:main' into lesson_16
Moibrahi7 5006fd3
Feat: Added patron controllers and tests
6395dbc
Fix: Modified test to fit needs of the code
2fe9251
Merge branch 'main' into lesson_16
Moibrahi7 035b0c9
Fix: changed formating
a960458
Fix: Re-ran spotless apply
ba08b1e
Fix: Modified tests to closer fit the needs
96b17b2
Fix: modified the test
99315c6
Fix: Changed formating
51d92f9
Fix: Changed json to be correct formating
cd1df88
Chore: Added comments to test and patron test
fc158d6
Fix: ran spotless apply to fix formating
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
17 changes: 17 additions & 0 deletions
17
...on_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/CreatePatronRequest.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,17 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class CreatePatronRequest { | ||
@NotNull(message = "patron is required") @Valid | ||
private PatronRequest patron; | ||
} |
10 changes: 10 additions & 0 deletions
10
...n_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/CreatePatronResponse.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,10 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class CreatePatronResponse { | ||
private PatronResponse patron; | ||
} |
12 changes: 12 additions & 0 deletions
12
lesson_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/GetPatronsResponse.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,12 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Singular; | ||
|
||
@Data | ||
@Builder | ||
public class GetPatronsResponse { | ||
@Singular private List<PatronResponse> patrons; | ||
} |
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
27 changes: 27 additions & 0 deletions
27
lesson_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/PatronRequest.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,27 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import com.codedifferently.lesson16.library.Patron; | ||
import jakarta.validation.constraints.NotBlank; | ||
import java.util.UUID; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class PatronRequest { | ||
private UUID id; | ||
|
||
@NotBlank(message = "Email is required") | ||
private String email; | ||
|
||
@NotBlank(message = "Name is required") | ||
private String name; | ||
|
||
public static Patron asPatron(PatronRequest request) { | ||
return new Patron(request.name, request.email); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
lesson_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/PatronResponse.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,25 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import com.codedifferently.lesson16.library.LibraryGuest; | ||
import jakarta.validation.constraints.NotBlank; | ||
import java.util.UUID; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class PatronResponse { | ||
private UUID id; | ||
|
||
@NotBlank(message = "Email is required") | ||
private String email; | ||
|
||
@NotBlank(message = "Name is required") | ||
private String name; | ||
|
||
public static PatronResponse from(LibraryGuest patron) { | ||
var result = | ||
PatronResponse.builder().id(patron.getId()).name(patron.getName()).email(patron.getEmail()); | ||
return result.build(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lesson_16/api/api_app/src/main/java/com/codedifferently/lesson16/web/PatronsController.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,73 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import com.codedifferently.lesson16.library.Library; | ||
import com.codedifferently.lesson16.library.LibraryGuest; | ||
import com.codedifferently.lesson16.library.Patron; | ||
import jakarta.validation.Valid; | ||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
// ___________________________________________________________ | ||
// THIS CODE WAS MADE IN COLLABORATION WITH VICENTE AND RICH | ||
// ___________________________________________________________ | ||
|
||
@RestController | ||
public class PatronsController { | ||
private final Library library; | ||
|
||
public PatronsController(Library library) throws IOException { | ||
this.library = library; | ||
} | ||
|
||
@GetMapping("/patrons") | ||
public GetPatronsResponse getPatrons() { | ||
Set<LibraryGuest> patrons = library.getPatrons(); | ||
List<PatronResponse> responsePatrons = patrons.stream().map(PatronResponse::from).toList(); | ||
var response = GetPatronsResponse.builder().patrons(responsePatrons).build(); | ||
return response; | ||
} | ||
|
||
@PostMapping("/patrons") | ||
public CreatePatronResponse postPatron(@Valid @RequestBody CreatePatronRequest req) { | ||
Patron guest = PatronRequest.asPatron(req.getPatron()); | ||
library.addLibraryGuest(guest); | ||
return CreatePatronResponse.builder().patron(PatronResponse.from(guest)).build(); | ||
} | ||
|
||
@GetMapping("/patrons/{id}") | ||
public GetPatronsResponse getPatron(@PathVariable("id") UUID id) { | ||
if (!library.hasLibraryGuest(id)) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Guest patron not found"); | ||
} | ||
Set<LibraryGuest> patrons = new HashSet<>(); | ||
for (LibraryGuest guest : library.getPatrons()) { | ||
if (guest.getId() == id) { | ||
patrons.add(guest); | ||
} | ||
} | ||
List<PatronResponse> responsePatrons = patrons.stream().map(PatronResponse::from).toList(); | ||
var response = GetPatronsResponse.builder().patrons(responsePatrons).build(); | ||
return response; | ||
} | ||
|
||
@DeleteMapping("/patrons/{id}") | ||
public ResponseEntity<Void> deletePatron(@PathVariable() UUID id) { | ||
if (!library.hasLibraryGuest(id)) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Guest patron not found"); | ||
} | ||
library.removeLibraryGuest(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
...n_16/api/api_app/src/test/java/com/codedifferently/lesson16/web/PatronControllerTest.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,136 @@ | ||
package com.codedifferently.lesson16.web; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.codedifferently.lesson16.Lesson16; | ||
import com.codedifferently.lesson16.library.Library; | ||
import com.codedifferently.lesson16.library.LibraryGuest; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
// ___________________________________________________________ | ||
// THIS CODE WAS MADE IN COLLABORATION WITH VICENTE AND RICH | ||
// ___________________________________________________________ | ||
|
||
@SpringBootTest | ||
@ContextConfiguration(classes = Lesson16.class) | ||
class PatronControllerTest { | ||
private static MockMvc mockMvc; | ||
@Autowired private Library library; | ||
|
||
private Library lib = library; | ||
|
||
@BeforeAll | ||
static void setUp(WebApplicationContext wac) { | ||
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); | ||
} | ||
|
||
@Test | ||
void testController_getsAnPatron() throws Exception { | ||
List<LibraryGuest> pat = library.getPatrons().stream().toList(); | ||
UUID ids = pat.get(3).getId(); | ||
|
||
mockMvc | ||
.perform(get("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void testController_getsAllPatrons() throws Exception { | ||
mockMvc | ||
.perform(get("/patrons").contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.patrons").isArray()) | ||
.andExpect(jsonPath("$.patrons.length()").value(5)); | ||
} | ||
|
||
@Test | ||
void testController_returnsNotFoundOnGetPatron() throws Exception { | ||
mockMvc | ||
.perform( | ||
get("/patrons/00000000-0000-0000-0000-000000000000") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void testController_reportsBadRequestOnAddPatron() throws Exception { | ||
String json = "{}"; | ||
|
||
mockMvc | ||
.perform(post("/patrons").contentType(MediaType.APPLICATION_JSON).content(json)) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.errors").isArray()) | ||
.andExpect(jsonPath("$.errors.length()").value(1)); | ||
} | ||
|
||
@Test | ||
void testController_addsPatron() throws Exception { | ||
String json = | ||
""" | ||
{ | ||
"patron":{ | ||
"name": "John Book", | ||
"email": "[email protected]" | ||
} | ||
} | ||
"""; | ||
|
||
mockMvc | ||
.perform(post("/patrons").contentType(MediaType.APPLICATION_JSON).content(json)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.patron.name").value("John Book")); | ||
} | ||
|
||
@Test | ||
void testController_returnsNotFoundOnDeletePatron() throws Exception { | ||
mockMvc | ||
.perform( | ||
delete("/patrons/00000000-0000-0000-0000-000000000000") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void testController_deletesPatron() throws Exception { | ||
Library lib = library; | ||
List<LibraryGuest> pat = library.getPatrons().stream().toList(); | ||
UUID ids = getGuestId(pat); | ||
|
||
mockMvc | ||
.perform(delete("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNoContent()); | ||
int i = 0; | ||
pat = library.getPatrons().stream().toList(); | ||
for (LibraryGuest guest : pat) { | ||
if (guest.getId() == ids) { | ||
i++; | ||
} | ||
} | ||
library = lib; | ||
assertThat(i).isEqualTo(0); | ||
} | ||
|
||
UUID getGuestId(List<LibraryGuest> list) { | ||
for (LibraryGuest guest : list) { | ||
if (guest.getCheckedOutMediaItems().size() == 0) { | ||
return guest.getId(); | ||
} | ||
} | ||
return list.get(4).getId(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.