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.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());