Implement staff with salespoint

Co-authored-by: Eren Asker <eren.asker@mailbox.tu-dresden.de>
This commit is contained in:
Denis Natusch 2023-11-16 17:24:49 +01:00 committed by Simon Bruder
parent eccff8d2b0
commit 6759841742
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
7 changed files with 103 additions and 123 deletions

View file

@ -1,20 +1,28 @@
package catering.staff;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.springframework.data.domain.AbstractAggregateRoot;
@Entity
public class Staff {
private int id;
private String surname;
private String name;
private String job;
private String surname, name, job;
private @Id @GeneratedValue Long id;
protected Staff() {
// No-argument constructor for JPA
}
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() {
public Long getId() {
return id;
}
@ -30,10 +38,6 @@ public class Staff {
return job;
}
public void setId(int id) {
this.id = id;
}
public void setSurname(String surname) {
this.surname = surname;
}
@ -45,5 +49,4 @@ public class Staff {
public void setJob(String job) {
this.job = job;
}
}

View file

@ -1,55 +1,70 @@
package catering.staff;
import org.springframework.security.access.prepost.PreAuthorize;
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;
private final StaffManagement staffManagement;
public StaffController(StaffRepository staffRepository) {
this.staffRepository = staffRepository;
public StaffController(StaffManagement staffManagement) {
this.staffManagement = staffManagement;
}
@GetMapping("/staff")
@PreAuthorize("hasRole('ADMIN')")
public String getStaff(Model model) {
model.addAttribute("title", "Personalverwaltung");
model.addAttribute("staffs", staffRepository.getStaffs());
model.addAttribute("staff", staffManagement.findAll());
return "staff";
}
@PostMapping("/staff/remove")
public String removeStaff(@RequestParam("staffID") int staffID, Model model) {
staffRepository.removeStaff(staffID);
@PreAuthorize("hasRole('ADMIN')")
public String removeStaff(@RequestParam("staffID") Staff staff, Model model) {
staffManagement.deleteStaff(staff.getId());
return "redirect:/staff";
}
@PostMapping("/staff/add")
@PreAuthorize("hasRole('ADMIN')")
public String addStaff(
@RequestParam String name,
@RequestParam String surname,
@RequestParam String job,
Model model) {
Model model
) {
Staff newStaff = new Staff(name, surname, job);
staffRepository.save(newStaff);
staffManagement.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));
@PreAuthorize("hasRole('ADMIN')")
public String editStaff(@PathVariable("id") Staff staff, Model model) {
model.addAttribute("staff", staff);
return "edit-staff";
}
@PostMapping("/staff/update")
public String updateStaff(@ModelAttribute Staff staff) {
staffRepository.save(staff);
@PostMapping("/staff/edit/{id}")
@PreAuthorize("hasRole('ADMIN')")
public String updateStaff(
@PathVariable("id") Staff staff,
@RequestParam String name,
@RequestParam String surname,
@RequestParam String job
) {
staff.setJob(job);
staff.setName(name);
staff.setSurname(surname);
staffManagement.save(staff);
return "redirect:/staff";
}
}

View file

@ -1,35 +0,0 @@
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,39 @@
package catering.staff;
import java.util.Optional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.stereotype.Component;
@Component
@Transactional
@SessionAttributes("staff")
public class StaffManagement {
private final StaffRepository staffRepository;
public StaffManagement(StaffRepository staffRepository) {
this.staffRepository = staffRepository;
}
public Staff addStaff(Staff staff) {
return staffRepository.save(staff);
}
public Optional<Staff> findById(Long staffID) {
return staffRepository.findById(staffID);
}
public void save(Staff staff) {
staffRepository.save(staff);
}
public Iterable<Staff> findAll() {
return staffRepository.findAll();
}
public void deleteStaff(Long staffID) {
staffRepository.deleteById(staffID);
}
}

View file

@ -1,53 +1,10 @@
package catering.staff;
import org.springframework.stereotype.Component;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable;
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();
}
interface StaffRepository extends CrudRepository<Staff, Long> {
@Override
Streamable<Staff> findAll();
}

View file

@ -7,7 +7,7 @@
</head>
<body>
<div layout:fragment="content">
<form th:action="@{/staff/update}" th:object="${staff}" method="post">
<form th:object="${staff}" method="post">
<input type="hidden" th:field="*{id}" />
<div class="mb-3">
<label class="form-label" for="name">Name:</label>

View file

@ -1,31 +1,32 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
<html
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout.html(title='Angestellte')}">
<head>
</head>
layout:decorate="~{layout.html(title='Angestellte')}"
>
<head> </head>
<body>
<div layout:fragment="content">
<div>
<h2>Mitarbeiterdetails</h2>
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Beruf</th>
<th></th>
<th></th>
</tr>
<tr th:each="staff : ${staffs}">
<td th:text="${staff.id}">01</td>
<td th:text="${staff.surname}">Musterkoch</td>
<tr th:each="staff : ${staff}">
<td th:text="${staff.name}">Max</td>
<td th:text="${staff.surname}">Musterkoch</td>
<td th:text="${staff.job}">Koch</td>
<td>
<a th:href="@{'/staff/edit/' + ${staff.id}}"><button class="btn btn-warning">Bearbeiten</button></a>
<a th:href="@{'/staff/edit/' + ${staff.id}}"
><button class="btn btn-warning">Bearbeiten</button></a
>
</td>
<td>
<form th:action="@{/staff/remove}" method="post">