diff --git a/src/main/java/catering/staff/StaffController.java b/src/main/java/catering/staff/StaffController.java index 8a478b6..c8c4a19 100644 --- a/src/main/java/catering/staff/StaffController.java +++ b/src/main/java/catering/staff/StaffController.java @@ -7,7 +7,10 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; +import jakarta.validation.Valid; + import org.springframework.ui.Model; +import org.springframework.validation.Errors; @Controller public class StaffController { @@ -22,9 +25,26 @@ public class StaffController { @PreAuthorize("hasRole('ADMIN')") public String getStaff(Model model) { model.addAttribute("staff", staffManagement.findAll()); + model.addAttribute("form", StaffForm.empty()); return "staff"; } + public String getStaff(Model model, StaffForm form) { + model.addAttribute("staff", staffManagement.findAll()); + model.addAttribute("form", form); + return "staff"; + } + + @PostMapping("/staff") + @PreAuthorize("hasRole('ADMIN')") + public String addEmployee(@Valid StaffForm form, Errors result, Model model) { + if (result.hasErrors()){ + return getStaff(model, form); + } + staffManagement.save(new Employee(form.getName(), form.getJob())); + return "redirect:/staff"; + } + @PostMapping("/staff/remove") @PreAuthorize("hasRole('ADMIN')") public String removeEmployee(@RequestParam("id") Employee employee, Model model) { @@ -32,25 +52,22 @@ public class StaffController { return "redirect:/staff"; } - @PostMapping("/staff/add") - @PreAuthorize("hasRole('ADMIN')") - public String addEmployee(@RequestParam String name, @RequestParam JobType job) { - staffManagement.save(new Employee(name, job)); - return "redirect:/staff"; - } - @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 "edit-staff"; } @PostMapping("/staff/edit/{id}") @PreAuthorize("hasRole('ADMIN')") - public String updateEmployee(@PathVariable("id") Employee employee, @RequestParam String name, @RequestParam JobType job) { - employee.setJob(job); - employee.setName(name); + public String updateEmployee(@PathVariable("id") Employee employee, @Valid StaffForm form, Errors result) { + if (result.hasErrors()){ + return "staff/edit/" + employee.getId(); + } + employee.setJob(form.getJob()); + employee.setName(form.getName()); staffManagement.save(employee); return "redirect:/staff"; } diff --git a/src/main/java/catering/staff/StaffForm.java b/src/main/java/catering/staff/StaffForm.java new file mode 100644 index 0000000..b7924da --- /dev/null +++ b/src/main/java/catering/staff/StaffForm.java @@ -0,0 +1,28 @@ +package catering.staff; + +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; + +public class StaffForm { + + private final @NotEmpty String name; + private final @NotNull JobType job; + + public StaffForm(String name, JobType job) { + this.name = name; + this.job = job; + } + + public String getName() { + return name; + } + + public JobType getJob() { + return job; + } + + public static StaffForm empty() { + return new StaffForm("", JobType.SERVICE); + } + +} diff --git a/src/main/resources/templates/edit-staff.html b/src/main/resources/templates/edit-staff.html index 38249b7..e94836e 100644 --- a/src/main/resources/templates/edit-staff.html +++ b/src/main/resources/templates/edit-staff.html @@ -7,17 +7,18 @@