diff --git a/src/main/java/catering/staff/StaffController.java b/src/main/java/catering/staff/StaffController.java index c8c4a19..ee09e5b 100644 --- a/src/main/java/catering/staff/StaffController.java +++ b/src/main/java/catering/staff/StaffController.java @@ -3,6 +3,7 @@ package catering.staff; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +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; @@ -24,12 +25,10 @@ public class StaffController { @GetMapping("/staff") @PreAuthorize("hasRole('ADMIN')") public String getStaff(Model model) { - model.addAttribute("staff", staffManagement.findAll()); - model.addAttribute("form", StaffForm.empty()); - return "staff"; + return getStaff(model, StaffForm.empty()); } - public String getStaff(Model model, StaffForm form) { + public String getStaff(Model model, @Valid StaffForm form) { model.addAttribute("staff", staffManagement.findAll()); model.addAttribute("form", form); return "staff"; @@ -55,16 +54,21 @@ public class StaffController { @GetMapping("/staff/edit/{id}") @PreAuthorize("hasRole('ADMIN')") public String editEmployee(@PathVariable("id") Employee employee, Model model) { - model.addAttribute("employee", employee); - model.addAttribute("form", new StaffForm(employee.getName(), employee.getJob())); + return editEmployee(model, new StaffForm(employee.getName(), employee.getJob())); + } + + public String editEmployee(Model model, @Valid StaffForm form) { + model.addAttribute("form", form); return "edit-staff"; } @PostMapping("/staff/edit/{id}") @PreAuthorize("hasRole('ADMIN')") - public String updateEmployee(@PathVariable("id") Employee employee, @Valid StaffForm form, Errors result) { + public String updateEmployee(@PathVariable("id") Employee employee, @Valid @ModelAttribute("form") StaffForm form, Errors result, Model model) { if (result.hasErrors()){ - return "staff/edit/" + employee.getId(); + System.out.println(form); + System.out.println(result); + return editEmployee(model, form); } employee.setJob(form.getJob()); employee.setName(form.getName());