Skip to content

Commit

Permalink
feat: deletePatron method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
VicenteVigueras committed Apr 11, 2024
1 parent 484d8e8 commit 9364adf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.UUID;
import java.util.stream.Collectors;
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;
Expand All @@ -25,7 +26,7 @@ public PatronsController(Library library) throws IOException {
}

@GetMapping("/patrons")
public GetPatronsResponse getPatrons() {
public GetPatronsResponse getAllPatrons() {
Set<LibraryGuest> patrons = library.getPatrons();
List<PatronsResponse> responseItems =
patrons.stream().map(PatronsResponse::from).collect(Collectors.toList());
Expand All @@ -46,11 +47,19 @@ public ResponseEntity<PatronsResponse> getPatronbyId(@PathVariable UUID id) {
}

@PostMapping("/patrons")
public CreatePatronsResponse postPatron(@Valid @RequestBody CreatePatronsRequest request) {
public CreatePatronsResponse createPatron(@Valid @RequestBody CreatePatronsRequest request) {
LibraryGuest patron = PatronsRequest.asLibraryGuest(request.getPatron());
library.addLibraryGuest(patron);
return CreatePatronsResponse.builder().patron(PatronsResponse.from(patron)).build();
}

// @DeleteMapping("/{id}")
@DeleteMapping("/patrons/{id}")
public ResponseEntity<Void> deletePatron(@PathVariable("id") UUID id) {
try {
library.removeLibraryGuest(id);
return ResponseEntity.noContent().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -30,7 +29,7 @@ static void setUp(WebApplicationContext wac) {
}

@Test
void testController_getsAllPatrons() throws Exception {
void testController_getAllPatrons() throws Exception {
mockMvc
.perform(get("/patrons").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Expand All @@ -39,7 +38,7 @@ void testController_getsAllPatrons() throws Exception {
}

@Test
void testController_getsPatron() throws Exception {
void testController_getsPatronById() throws Exception {
mockMvc
.perform(
get("/patrons/31616162-3831-3832-2d34-3334352d3465")
Expand All @@ -48,7 +47,7 @@ void testController_getsPatron() throws Exception {
}

@Test
void testController_addsPatron() throws Exception {
void testController_createPatron() throws Exception {
String json =
"""
{
Expand All @@ -64,4 +63,13 @@ void testController_addsPatron() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.patron.name").value("vicente"));
}

@Test
void testController_deletePatron() throws Exception {
mockMvc
.perform(
delete("/patrons/32623932-6566-3364-2d62-3232342d3435")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
}

0 comments on commit 9364adf

Please sign in to comment.