This commit is contained in:
Denis Natusch 2023-11-28 15:06:19 +01:00
parent 26aab5082a
commit bd862ad9f6
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985

View file

@ -3,6 +3,7 @@ package catering.staff;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -24,12 +25,10 @@ public class StaffController {
@GetMapping("/staff") @GetMapping("/staff")
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
public String getStaff(Model model) { public String getStaff(Model model) {
model.addAttribute("staff", staffManagement.findAll()); return getStaff(model, StaffForm.empty());
model.addAttribute("form", StaffForm.empty());
return "staff";
} }
public String getStaff(Model model, StaffForm form) { public String getStaff(Model model, @Valid StaffForm form) {
model.addAttribute("staff", staffManagement.findAll()); model.addAttribute("staff", staffManagement.findAll());
model.addAttribute("form", form); model.addAttribute("form", form);
return "staff"; return "staff";
@ -55,16 +54,21 @@ public class StaffController {
@GetMapping("/staff/edit/{id}") @GetMapping("/staff/edit/{id}")
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
public String editEmployee(@PathVariable("id") Employee employee, Model model) { public String editEmployee(@PathVariable("id") Employee employee, Model model) {
model.addAttribute("employee", employee); return editEmployee(model, new StaffForm(employee.getName(), employee.getJob()));
model.addAttribute("form", new StaffForm(employee.getName(), employee.getJob())); }
public String editEmployee(Model model, @Valid StaffForm form) {
model.addAttribute("form", form);
return "edit-staff"; return "edit-staff";
} }
@PostMapping("/staff/edit/{id}") @PostMapping("/staff/edit/{id}")
@PreAuthorize("hasRole('ADMIN')") @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()){ 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.setJob(form.getJob());
employee.setName(form.getName()); employee.setName(form.getName());