Use enum for job types

This commit is contained in:
Denis Natusch 2023-11-21 16:02:42 +01:00 committed by Simon Bruder
parent 5aeaf01e0e
commit 6c8daa5287
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
5 changed files with 20 additions and 9 deletions

View file

@ -0,0 +1,6 @@
package catering.staff;
public enum JobType {
COOK,
SERVICE
}

View file

@ -7,14 +7,15 @@ import jakarta.persistence.Id;
@Entity
public class Staff {
private String name, job;
private String name;
private JobType job;
private @Id @GeneratedValue Long id;
protected Staff() {
// No-argument constructor for JPA
}
public Staff(String name, String job) {
public Staff(String name, JobType job) {
this.name = name;
this.job = job;
}
@ -27,7 +28,7 @@ public class Staff {
return name;
}
public String getJob() {
public JobType getJob() {
return job;
}
@ -36,7 +37,7 @@ public class Staff {
this.name = name;
}
public void setJob(String job) {
public void setJob(JobType job) {
this.job = job;
}
}

View file

@ -37,7 +37,7 @@ public class StaffController {
@PreAuthorize("hasRole('ADMIN')")
public String addStaff(
@RequestParam String name,
@RequestParam String job,
@RequestParam JobType job,
Model model
) {
Staff newStaff = new Staff(name, job);
@ -57,7 +57,7 @@ public class StaffController {
public String updateStaff(
@PathVariable("id") Staff staff,
@RequestParam String name,
@RequestParam String job
@RequestParam JobType job
) {
staff.setJob(job);
staff.setName(name);

View file

@ -15,7 +15,9 @@
</div>
<div class="mb-3">
<label class="form-label" for="job">Beruf:</label>
<input class="form-control" type="text" th:field="*{job}" required />
<select name="job" class="form-select">
<option th:each="type : ${T(catering.staff.JobType).values()}" th:value="${type}" th:selected="${type.name() == staff.job.name()}" th:text="${type}" required></option>
</select>
</div>
<button class="btn btn-primary" type="submit">Speichern</button>
<a th:href="@{/staff}"><button type="button" class="btn btn-danger">Abbrechen</button></a>

View file

@ -43,8 +43,10 @@
<input class="form-control" type="text" name="name" required />
</div>
<div class="mb-3">
<label class="form-label" for="job">Beruf</label>
<input class="form-control" type="text" name="job" required />
<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:text="${type}" required></option>
</select>
</div>
<button class="btn btn-primary" type="submit">Hinzufügen</button>
</form>