- Delete Button for Owners

- on cascade delete for pets
This commit is contained in:
MoechtegernMoe 2023-11-23 14:26:04 +01:00
parent 187c4a367c
commit d37fba0dfc
3 changed files with 16 additions and 6 deletions

View file

@ -25,12 +25,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -144,6 +139,12 @@ class OwnerController {
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
@GetMapping("/owners/{ownerId}/delete")
public String deleteOwner(@PathVariable("ownerId") int ownerId) {
this.owners.deleteOwnerById(ownerId);
return "redirect:/owners";
}
/** /**
* Custom handler for displaying an owner. * Custom handler for displaying an owner.
* @param ownerId the ID of the owner to display * @param ownerId the ID of the owner to display

View file

@ -72,6 +72,9 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
*/ */
void save(Owner owner); void save(Owner owner);
@Transactional
void deleteOwnerById(int ownerId);
/** /**
* Returns all the owners from data store * Returns all the owners from data store
**/ **/

View file

@ -34,6 +34,12 @@
<a th:href="@{__${owner.id}__/edit}" class="btn btn-primary">Edit <a th:href="@{__${owner.id}__/edit}" class="btn btn-primary">Edit
Owner</a> Owner</a>
<a th:href="@{__${owner.id}__/delete}" class="btn btn-primary" th:method="delete">Delete
Owner</a>
<a th:href="@{__${owner.id}__/pets/new}" class="btn btn-primary">Add <a th:href="@{__${owner.id}__/pets/new}" class="btn btn-primary">Add
New Pet</a> New Pet</a>