mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
fixup! Implement per-month employee working hours
This commit is contained in:
parent
fd7d05a0f6
commit
2f221da2f4
43
src/main/java/catering/staff/DateForm.java
Normal file
43
src/main/java/catering/staff/DateForm.java
Normal file
|
@ -0,0 +1,43 @@
|
|||
package catering.staff;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class DateForm {
|
||||
|
||||
private @NotNull int year;
|
||||
private @NotNull int month;
|
||||
|
||||
public DateForm() {
|
||||
this.year = LocalDateTime.now().getYear();
|
||||
this.month = LocalDateTime.now().getMonth().getValue();
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public int getMonth() {
|
||||
return month;
|
||||
}
|
||||
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public void setMonth(int month) {
|
||||
this.month = month;
|
||||
}
|
||||
|
||||
public void validate(Errors e) {
|
||||
if (year < 2023 || year > 2100) {
|
||||
e.rejectValue("year", "year is not valid");
|
||||
}
|
||||
if (month < 1 || month > 12) {
|
||||
e.rejectValue("month", "month is not valid");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,6 +33,30 @@ public class StaffController {
|
|||
public String getStaff(Model model, @Valid StaffForm form) {
|
||||
model.addAttribute("staff", staffManagement.findAll());
|
||||
model.addAttribute("form", form);
|
||||
model.addAttribute("management", staffManagement);
|
||||
model.addAttribute("dateForm", new DateForm());
|
||||
return "staff";
|
||||
}
|
||||
|
||||
public String getStaff(Model model, @Valid DateForm form) {
|
||||
model.addAttribute("staff", staffManagement.findAll());
|
||||
model.addAttribute("form", new StaffForm());
|
||||
model.addAttribute("management", staffManagement);
|
||||
model.addAttribute("dateForm", form);
|
||||
return "staff";
|
||||
}
|
||||
|
||||
@PostMapping("/staff/updateYearMonth")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public String getStaff(Model model, @Valid DateForm form, Errors result) {
|
||||
form.validate(result);
|
||||
if (result.hasErrors()) {
|
||||
return getStaff(model, form);
|
||||
}
|
||||
model.addAttribute("staff", staffManagement.findAll());
|
||||
model.addAttribute("form", new StaffForm());
|
||||
model.addAttribute("management", staffManagement);
|
||||
model.addAttribute("dateForm", form);
|
||||
return "staff";
|
||||
}
|
||||
|
||||
|
|
|
@ -111,4 +111,8 @@ public class StaffManagement {
|
|||
.map(order -> order.getDurationInSecondsDuringMonth(month))
|
||||
.collect(Collectors.summingDouble(seconds -> (double) seconds / 3600));
|
||||
}
|
||||
|
||||
public double getWorkingHoursByEmployee(Employee e, int year, int month) {
|
||||
return getWorkingHoursByEmployee(e, YearMonth.of(year, month));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,11 +10,25 @@
|
|||
<div layout:fragment="content">
|
||||
<div>
|
||||
<h2>Mitarbeiterdetails</h2>
|
||||
<form th:object="${dateForm}" th:action="@{/staff/updateYearMonth}" method="post">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="year">Jahr:</label>
|
||||
<input class="form-control" type="year" th:field="*{year}" th:errorclass="is-invalid" required>
|
||||
<div th:if="${#fields.hasErrors('year')}" class="invalid-feedback">Ungültiges Jahr</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="month">Monat:</label>
|
||||
<input class="form-control" type="month" th:field="*{month}" th:errorclass="is-invalid" required>
|
||||
<div th:if="${#fields.hasErrors('month')}" class="invalid-feedback">Ungültiger Monat</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Zeitangabe aktualisieren</button>
|
||||
</form>
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Beruf</th>
|
||||
<th>Lohn</th>
|
||||
<th>Arbeitszeit</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
@ -23,6 +37,7 @@
|
|||
<td th:text="${employee.name}">Max</td>
|
||||
<td th:text="${employee.job}">Koch</td>
|
||||
<td th:text="${employee.wage}"></td>
|
||||
<td th:with="year=${dataForm.getYear},month=${dataForm.getMonth}" th:text="${management.getWorkingHoursByEmployee(employee, year, month)}"></td>
|
||||
<td>
|
||||
<a th:href="@{'/staff/edit/' + ${employee.id}}"
|
||||
><button class="btn btn-warning">Bearbeiten</button></a
|
||||
|
|
Loading…
Reference in a new issue