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; 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 { public class Staff {
private int id; private String surname, name, job;
private String surname; private @Id @GeneratedValue Long id;
private String name;
private String job; protected Staff() {
// No-argument constructor for JPA
}
public Staff(String name, String surname, 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.name = name;
this.surname = surname; this.surname = surname;
this.job = job; this.job = job;
} }
public int getId() { public Long getId() {
return id; return id;
} }
@ -30,10 +38,6 @@ public class Staff {
return job; return job;
} }
public void setId(int id) {
this.id = id;
}
public void setSurname(String surname) { public void setSurname(String surname) {
this.surname = surname; this.surname = surname;
} }
@ -45,5 +49,4 @@ public class Staff {
public void setJob(String job) { public void setJob(String job) {
this.job = job; this.job = job;
} }
} }

View file

@ -1,55 +1,70 @@
package catering.staff; package catering.staff;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@Controller @Controller
public class StaffController { public class StaffController {
private final StaffRepository staffRepository; private final StaffManagement staffManagement;
public StaffController(StaffRepository staffRepository) { public StaffController(StaffManagement staffManagement) {
this.staffRepository = staffRepository; this.staffManagement = staffManagement;
} }
@GetMapping("/staff") @GetMapping("/staff")
@PreAuthorize("hasRole('ADMIN')")
public String getStaff(Model model) { public String getStaff(Model model) {
model.addAttribute("title", "Personalverwaltung"); model.addAttribute("title", "Personalverwaltung");
model.addAttribute("staffs", staffRepository.getStaffs()); model.addAttribute("staff", staffManagement.findAll());
return "staff"; return "staff";
} }
@PostMapping("/staff/remove") @PostMapping("/staff/remove")
public String removeStaff(@RequestParam("staffID") int staffID, Model model) { @PreAuthorize("hasRole('ADMIN')")
staffRepository.removeStaff(staffID); public String removeStaff(@RequestParam("staffID") Staff staff, Model model) {
staffManagement.deleteStaff(staff.getId());
return "redirect:/staff"; return "redirect:/staff";
} }
@PostMapping("/staff/add") @PostMapping("/staff/add")
@PreAuthorize("hasRole('ADMIN')")
public String addStaff( public String addStaff(
@RequestParam String name, @RequestParam String name,
@RequestParam String surname, @RequestParam String surname,
@RequestParam String job, @RequestParam String job,
Model model) { Model model
) {
Staff newStaff = new Staff(name, surname, job); Staff newStaff = new Staff(name, surname, job);
staffRepository.save(newStaff); staffManagement.save(newStaff);
return "redirect:/staff"; return "redirect:/staff";
} }
@GetMapping("/staff/edit/{id}") @GetMapping("/staff/edit/{id}")
public String editStaff(@PathVariable("id") int id, Model model) { @PreAuthorize("hasRole('ADMIN')")
staffRepository.findById(id).ifPresent(staff -> model.addAttribute("staff", staff)); public String editStaff(@PathVariable("id") Staff staff, Model model) {
model.addAttribute("staff", staff);
return "edit-staff"; return "edit-staff";
} }
@PostMapping("/staff/update") @PostMapping("/staff/edit/{id}")
public String updateStaff(@ModelAttribute Staff staff) { @PreAuthorize("hasRole('ADMIN')")
staffRepository.save(staff); 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"; 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; package catering.staff;
import org.springframework.stereotype.Component; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable;
import java.util.ArrayList; interface StaffRepository extends CrudRepository<Staff, Long> {
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();
}
@Override
Streamable<Staff> findAll();
} }

View file

@ -7,7 +7,7 @@
</head> </head>
<body> <body>
<div layout:fragment="content"> <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}" /> <input type="hidden" th:field="*{id}" />
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="name">Name:</label> <label class="form-label" for="name">Name:</label>

View file

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