diff --git a/src/main/java/catering/staff/Staff.java b/src/main/java/catering/staff/Staff.java new file mode 100644 index 0000000..c0a8b9a --- /dev/null +++ b/src/main/java/catering/staff/Staff.java @@ -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; + } + +} diff --git a/src/main/java/catering/staff/StaffController.java b/src/main/java/catering/staff/StaffController.java new file mode 100644 index 0000000..7b40c64 --- /dev/null +++ b/src/main/java/catering/staff/StaffController.java @@ -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"; + } +} diff --git a/src/main/java/catering/staff/StaffDataInitializer.java b/src/main/java/catering/staff/StaffDataInitializer.java new file mode 100644 index 0000000..6a840fd --- /dev/null +++ b/src/main/java/catering/staff/StaffDataInitializer.java @@ -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"); + + } +} diff --git a/src/main/java/catering/staff/StaffRepository.java b/src/main/java/catering/staff/StaffRepository.java new file mode 100644 index 0000000..fcc0d17 --- /dev/null +++ b/src/main/java/catering/staff/StaffRepository.java @@ -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 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 getStaffs() { + return new ArrayList<>(this.staffs); + } + + public Optional findById(int id) { + return this.staffs.stream().filter(staff -> staff.getId() == id).findFirst(); + } + +} diff --git a/src/main/resources/static/resources/css/staff/edit-style.css b/src/main/resources/static/resources/css/staff/edit-style.css new file mode 100644 index 0000000..f102890 --- /dev/null +++ b/src/main/resources/static/resources/css/staff/edit-style.css @@ -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; +} diff --git a/src/main/resources/static/resources/css/staff/style.css b/src/main/resources/static/resources/css/staff/style.css new file mode 100644 index 0000000..faba771 --- /dev/null +++ b/src/main/resources/static/resources/css/staff/style.css @@ -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; +} diff --git a/src/main/resources/templates/edit-staff.html b/src/main/resources/templates/edit-staff.html new file mode 100644 index 0000000..4fa6085 --- /dev/null +++ b/src/main/resources/templates/edit-staff.html @@ -0,0 +1,29 @@ + + + + + Personal Bearbeiten + + + + +
+

Personal Bearbeiten

+
+ + + + + + + + + + + + +
+ Cancel +
+ + diff --git a/src/main/resources/templates/staff.html b/src/main/resources/templates/staff.html new file mode 100644 index 0000000..d965484 --- /dev/null +++ b/src/main/resources/templates/staff.html @@ -0,0 +1,55 @@ + + + + + + + + +
+

Personalverwaltung

+

Mitarbeiterdetails

+ + + + + + + + + + + + + + + + +
IDNachnameVornameBeruf
01MusterkochMaxKoch + Bearbeiten + +
+ + +
+
+
+
+

Personal Hinzufügen

+
+ + + + + + + + + + +
+
+ +