Add staff overview prototype

This commit is contained in:
Eren Asker 2023-11-10 17:09:39 +01:00 committed by Simon Bruder
parent 9238218cca
commit 5c1bdd2d25
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
8 changed files with 367 additions and 0 deletions

View file

@ -0,0 +1,49 @@
package catering.staff;
public class Staff {
private int id;
private String surname;
private String name;
private String job;
public Staff(String name, String surname, String job) {
this.id = (int) (Math.random() * 10 * Math.random() * 10 + Math.random() * 10);
this.name = name;
this.surname = surname;
this.job = job;
}
public int getId() {
return id;
}
public String getSurname() {
return surname;
}
public String getName() {
return name;
}
public String getJob() {
return job;
}
public void setId(int id) {
this.id = id;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setName(String name) {
this.name = name;
}
public void setJob(String job) {
this.job = job;
}
}

View file

@ -0,0 +1,55 @@
package catering.staff;
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;
import org.springframework.ui.Model;
@Controller
public class StaffController {
private final StaffRepository staffRepository;
public StaffController(StaffRepository staffRepository) {
this.staffRepository = staffRepository;
}
@GetMapping("/staff")
public String getStaff(Model model) {
model.addAttribute("title", "Personalverwaltung");
model.addAttribute("staffs", staffRepository.getStaffs());
return "staff";
}
@PostMapping("/staff/remove")
public String removeStaff(@RequestParam("staffID") int staffID, Model model) {
staffRepository.removeStaff(staffID);
return "redirect:/staff";
}
@PostMapping("/staff/add")
public String addStaff(
@RequestParam String name,
@RequestParam String surname,
@RequestParam String job,
Model model) {
Staff newStaff = new Staff(name, surname, job);
staffRepository.save(newStaff);
return "redirect:/staff";
}
@GetMapping("/staff/edit/{id}")
public String editStaff(@PathVariable("id") int id, Model model) {
staffRepository.findById(id).ifPresent(staff -> model.addAttribute("staff", staff));
return "edit-staff";
}
@PostMapping("/staff/update")
public String updateStaff(@ModelAttribute Staff staff) {
staffRepository.save(staff);
return "redirect:/staff";
}
}

View file

@ -0,0 +1,35 @@
package catering.staff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import jakarta.annotation.PostConstruct;
@Component
public class StaffDataInitializer {
private static final Logger LOG = LoggerFactory.getLogger(StaffDataInitializer.class);
private final StaffRepository staffRepository;
public StaffDataInitializer(StaffRepository staffRepository) {
Assert.notNull(staffRepository, "StaffRepository must not be null!");
this.staffRepository = staffRepository;
}
@PostConstruct
public void initialize() {
LOG.info("Creating default staff.");
staffRepository.save(new Staff("Max", "Musterkoch", "Koch"));
staffRepository.save(new Staff("Julia", "Musterfrau", "Servicekraft"));
LOG.info("Default staff created");
}
}

View file

@ -0,0 +1,53 @@
package catering.staff;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
@Component
public class StaffRepository {
private Set<Staff> staffs = new HashSet<>();
public StaffRepository() {
}
public boolean addStaff(Staff staff) {
return this.staffs.add(staff);
}
private int nextId = (int) (Math.random() * 10 * Math.random() * 10 + Math.random() * 10);
public void save(Staff staff) {
if (staff.getId() == 0) {
staff.setId(nextId++);
} else {
this.staffs.removeIf(p -> p.getId() == staff.getId());
}
this.staffs.add(staff);
}
public long count() {
return this.staffs.size();
}
public boolean removeStaff(int staffID) {
return this.staffs.removeIf(staff -> staff.getId() == staffID);
}
public Collection<Staff> getStaffs() {
return new ArrayList<>(this.staffs);
}
public Optional<Staff> findById(int id) {
return this.staffs.stream().filter(staff -> staff.getId() == id).findFirst();
}
}

View file

@ -0,0 +1,21 @@
body {
font: 13px/22px Helvetica, Arial, sans-serif;
background: #f0f0f0;
}
h2 {
text-decoration: underline;
}
.button {
cursor: pointer;
font-size: 13px;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #333333;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #333333;
}

View file

@ -0,0 +1,70 @@
/* CSS RESET */
body {
margin: 0 auto;
width: 940px;
font: 13px/22px Helvetica, Arial, sans-serif;
background: #f0f0f0;
}
h1 {
font-size: 3em;
line-height: 3.2em;
text-align:center;
}
h2 {
text-decoration: underline;
}
table {
border-collapse: collapse;
width: 95%;
margin-top: 20px;
}
th,
td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
text-decoration: underline;
font-size: 19px;
}
tr {
font-size: 17px;
}
td:last-child,
td:nth-child(5) {
width: 50px;
text-align: center;
}
tr:nth-child(even) {
background-color: rgb(214, 236, 216);
}
.add-button {
margin-top: 20px;
font-size: 16px;
}
button {
cursor: pointer;
font-size: 13px;
margin-left: 4px;
margin-right: 4px;
}
.button {
cursor: pointer;
font-size: 13px;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #333333;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #333333;
}

View file

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Personal Bearbeiten</title>
<link rel="stylesheet" th:href="@{/resources/css/staff/edit-style.css}" />
</head>
<body>
<div class="edit-staff-form">
<h2>Personal Bearbeiten</h2>
<form th:action="@{/staff/update}" th:object="${staff}" method="post">
<input type="hidden" th:field="*{id}" />
<label for="name">Name:</label>
<input type="text" th:field="*{name}" required />
<label for="surname">Nachname:</label>
<input type="text" th:field="*{surname}" required />
<label for="job">Beruf:</label>
<input type="text" th:field="*{job}" required />
<button type="submit">Save</button>
</form>
<a th:href="@{/staff}" class="button">Cancel</a>
</div>
</body>
</html>

View file

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" th:href="@{/resources/css/staff/style.css}" />
<title th:text="${title}"></title>
</head>
<body>
<div class="verwaltung">
<h1>Personalverwaltung</h1>
<h2>Mitarbeiterdetails</h2>
<table>
<tr>
<th>ID</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Beruf</th>
</tr>
<tr th:each="staff : ${staffs}">
<td th:text="${staff.id}">01</td>
<td th:text="${staff.surname}">Musterkoch</td>
<td th:text="${staff.name}">Max</td>
<td th:text="${staff.job}">Koch</td>
<td>
<a th:href="@{'/staff/edit/' + ${staff.id}}" class="button"
>Bearbeiten</a
>
</td>
<td>
<form th:action="@{/staff/remove}" method="post">
<input type="hidden" th:name="staffID" th:value="${staff.id}" />
<button type="submit" class="delete-icon">Entfernen</button>
</form>
</td>
</tr>
</table>
</div>
<div class="add-staff-form">
<h2>Personal Hinzufügen</h2>
<form action="/staff/add" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required />
<label for="surname">Nachname:</label>
<input type="text" id="surname" name="surname" required />
<label for="job">Beruf:</label>
<input type="text" id="job" name="job" required />
<button type="submit">Hinzufügen</button>
</form>
</div>
</body>
</html>