diff --git a/src/main/java/catering/users/UserController.java b/src/main/java/catering/users/UserController.java index c9cee89..2d3ae1b 100644 --- a/src/main/java/catering/users/UserController.java +++ b/src/main/java/catering/users/UserController.java @@ -35,12 +35,18 @@ public class UserController { } @PostMapping("/register") - String register(@Valid RegistrationForm form, Errors result) { + String register(@Valid RegistrationForm form, Errors result, Model model) { if (result.hasErrors()){ return "register"; } - userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword()); - return "redirect:/login"; + if (userManagement.userNameExists(form.getUsername())) { + model.addAttribute("userNameExists",true); + return "register"; + } else { + model.addAttribute("userNameExists",false); + userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword()); + return "redirect:/login"; + } } @GetMapping("/login") diff --git a/src/main/java/catering/users/UserManagement.java b/src/main/java/catering/users/UserManagement.java index a9bec49..da74e67 100644 --- a/src/main/java/catering/users/UserManagement.java +++ b/src/main/java/catering/users/UserManagement.java @@ -51,4 +51,12 @@ public class UserManagement { public Optional getUserByAccount(UserAccount userAccount) { return users.findAll().stream().filter(u -> u.getUserAccount().equals(userAccount)).findFirst(); } + + public boolean userNameExists(String username){ + return !users.findAll().stream() + .filter(u -> u.getUsername().equals(username)) + .toList() + .isEmpty(); + } + } diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html index 340b0c6..d4f5db4 100644 --- a/src/main/resources/templates/register.html +++ b/src/main/resources/templates/register.html @@ -9,6 +9,7 @@
+

Nutzername vergeben