mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Fix Me
This commit is contained in:
parent
c398c12592
commit
26aab5082a
|
@ -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";
|
||||
}
|
||||
|
|
28
src/main/java/catering/staff/StaffForm.java
Normal file
28
src/main/java/catering/staff/StaffForm.java
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,17 +7,18 @@
|
|||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<form th:object="${employee}" method="post">
|
||||
<input type="hidden" th:field="*{id}" />
|
||||
<form th:object="${form}" method="post">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="name">Name:</label>
|
||||
<input class="form-control" type="text" th:field="*{name}" required />
|
||||
<input class="form-control" type="text" th:field="*{name}" th:errorclass="is-invalid" required/>
|
||||
<div th:if="${#fields.hasErrors('name')}" class="invalid-feedback">Ungültiger Name</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="job">Beruf:</label>
|
||||
<select name="job" class="form-select">
|
||||
<option th:each="type : ${T(catering.staff.JobType).values()}" th:value="${type}" th:selected="${type.name() == employee.job.name()}" th:text="${type}" required></option>
|
||||
<select th:field="*{job}" class="form-select">
|
||||
<option th:each="type : ${T(catering.staff.JobType).values()}" th:value="${type}" th:selected="${type.name() == form.job.name()}" th:text="${type}" required></option>
|
||||
</select>
|
||||
<div th:if="${#fields.hasErrors('job')}" class="invalid-feedback">Ungültiger Job</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Speichern</button>
|
||||
<a th:href="@{/staff}"><button type="button" class="btn btn-danger">Abbrechen</button></a>
|
||||
|
|
|
@ -37,16 +37,18 @@
|
|||
</div>
|
||||
<div>
|
||||
<h2>Personal Hinzufügen</h2>
|
||||
<form action="/staff/add" method="post">
|
||||
<form th:object="${form}" th:action="@{/staff}" method="post">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="name">Name</label>
|
||||
<input class="form-control" type="text" name="name" required />
|
||||
<input class="form-control" type="text" th:field="*{name}" th:errorclass="is-invalid" required>
|
||||
<div th:if="${#fields.hasErrors('name')}" class="invalid-feedback">Ungültiger Name</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="job">Beruf:</label>
|
||||
<select name="job" class="form-select">
|
||||
<select th:field="*{job}" class="form-select" th:errorclass="is-invalid">
|
||||
<option th:each="type : ${T(catering.staff.JobType).values()}" th:value="${type}" th:text="${type}" required></option>
|
||||
</select>
|
||||
<div th:if="${#fields.hasErrors('job')}" class="invalid-feedback">Ungültiger Job</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Hinzufügen</button>
|
||||
</form>
|
||||
|
|
Loading…
Reference in a new issue