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 @Entity
public class Staff { public class Staff {
private String name, job; private String name;
private JobType job;
private @Id @GeneratedValue Long id; private @Id @GeneratedValue Long id;
protected Staff() { protected Staff() {
// No-argument constructor for JPA // No-argument constructor for JPA
} }
public Staff(String name, String job) { public Staff(String name, JobType job) {
this.name = name; this.name = name;
this.job = job; this.job = job;
} }
@ -27,7 +28,7 @@ public class Staff {
return name; return name;
} }
public String getJob() { public JobType getJob() {
return job; return job;
} }
@ -36,7 +37,7 @@ public class Staff {
this.name = name; this.name = name;
} }
public void setJob(String job) { public void setJob(JobType job) {
this.job = job; this.job = job;
} }
} }

View file

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

View file

@ -15,7 +15,9 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="job">Beruf:</label> <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> </div>
<button class="btn btn-primary" type="submit">Speichern</button> <button class="btn btn-primary" type="submit">Speichern</button>
<a th:href="@{/staff}"><button type="button" class="btn btn-danger">Abbrechen</button></a> <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 /> <input class="form-control" type="text" name="name" required />
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="job">Beruf</label> <label class="form-label" for="job">Beruf:</label>
<input class="form-control" type="text" name="job" required /> <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> </div>
<button class="btn btn-primary" type="submit">Hinzufügen</button> <button class="btn btn-primary" type="submit">Hinzufügen</button>
</form> </form>