Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -60,14 +60,6 @@ public PatientDTO updatePatient(Actor actor, UUID id, PatientUpdateDTO dto) {
return patientMapper.toDTO(patientRepository.save(entity));
}

@Transactional
public void deletePatient(Actor actor, UUID id) {
requireCanManagePatients(actor);
PatientEntity entity = patientRepository.findById(id)
.orElseThrow(() -> new BadRequestException("PATIENT_NOT_FOUND", "Patient not found"));
patientRepository.delete(entity);
}

@Transactional(readOnly = true)
public Optional<PatientDTO> getPatient(UUID id) {
return patientRepository.findById(id).map(patientMapper::toDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ public ResponseEntity<PatientDTO> updatePatient(@PathVariable UUID id, @RequestB
return ResponseEntity.ok(patientService.updatePatient(securityActorAdapter.currentUser(), id, patientDTO));
}

@DeleteMapping("/{id}")
@PreAuthorize("hasRole('MANAGER')")
public ResponseEntity<Void> deletePatient(@PathVariable UUID id) {
patientService.deletePatient(securityActorAdapter.currentUser(), id);
return ResponseEntity.noContent().build();
}

@GetMapping("/{id}")
public ResponseEntity<PatientDTO> getPatient(@PathVariable UUID id) {
return patientService.getPatient(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,4 @@ public String updatePatient(@PathVariable UUID id, @Valid @ModelAttribute("patie
return "redirect:/ui/patients";
}

@PostMapping("/ui/patients/delete/{id}")
@PreAuthorize("hasRole('MANAGER')")
public String deletePatient(@PathVariable UUID id) {
patientService.deletePatient(securityActorAdapter.currentUser(), id);
return "redirect:/ui/patients";
}
}
4 changes: 0 additions & 4 deletions src/main/resources/templates/patients/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ <h1>Registered Patients</h1>
<td th:text="${p.createdAt}">Date</td>
<td>
<a class="button button-small" th:href="@{/ui/patients/edit/{id}(id=${p.id})}">Edit</a>
<form th:action="@{/ui/patients/delete/{id}(id=${p.id})}" method="post" style="display:inline"
onsubmit="return confirm('Are you sure you want to delete this patient?')">
<button class="button button-small button-danger" type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.example.projektarendehantering.application.service;

import org.example.projektarendehantering.common.Actor;
import org.example.projektarendehantering.common.BadRequestException;
import org.example.projektarendehantering.common.ConflictException;
import org.example.projektarendehantering.common.NotAuthorizedException;
import org.example.projektarendehantering.common.Role;
Expand Down Expand Up @@ -110,25 +109,4 @@ void updatePatient_shouldThrowIfForbidden() {
.isInstanceOf(NotAuthorizedException.class);
}

@Test
void deletePatient_shouldDeleteIfFound() {
UUID id = UUID.randomUUID();
PatientEntity entity = new PatientEntity();

when(patientRepository.findById(id)).thenReturn(Optional.of(entity));

patientService.deletePatient(managerActor, id);

verify(patientRepository).delete(entity);
}

@Test
void deletePatient_shouldThrowIfNotFound() {
UUID id = UUID.randomUUID();

when(patientRepository.findById(id)).thenReturn(Optional.empty());

assertThatThrownBy(() -> patientService.deletePatient(managerActor, id))
.isInstanceOf(BadRequestException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
Expand Down Expand Up @@ -92,15 +91,4 @@ void updatePatient_shouldReturnOk() throws Exception {
.andExpect(jsonPath("$.firstName").value("Jane"));
}

@Test
@WithMockUser(roles = "MANAGER")
void deletePatient_shouldReturnNoContent() throws Exception {
UUID id = UUID.randomUUID();

mockMvc.perform(delete("/api/patients/{id}", id)
.with(csrf()))
.andExpect(status().isNoContent());

verify(patientService).deletePatient(managerActor, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import java.util.UUID;

import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
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.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

Expand Down Expand Up @@ -63,12 +61,4 @@ void editPatient_shouldReturnView() throws Exception {
.andExpect(model().attributeExists("patientUpdateDTO"));
}

@Test
@WithMockUser(roles = "MANAGER")
void deletePatient_shouldRedirect() throws Exception {
UUID id = UUID.randomUUID();
mockMvc.perform(post("/ui/patients/delete/{id}", id).with(csrf()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/ui/patients"));
}
}