From 5915d20f71d6832dc8b923c1922500229bda3b95 Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Sun, 19 Nov 2023 18:56:45 +0100 Subject: [PATCH] WHY ARE YOU NULL --- .../java/catering/users/RegistrationForm.java | 19 ++++++++++++-- src/main/java/catering/users/User.java | 18 +++++++++++++ .../java/catering/users/UserController.java | 25 +++++++++++++++++-- .../java/catering/users/UserManagement.java | 15 +++++++++-- src/main/resources/templates/profile.html | 8 ++++++ src/main/resources/templates/register.html | 8 ++++++ 6 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/main/java/catering/users/RegistrationForm.java b/src/main/java/catering/users/RegistrationForm.java index d1e4eb5..cd3a289 100644 --- a/src/main/java/catering/users/RegistrationForm.java +++ b/src/main/java/catering/users/RegistrationForm.java @@ -5,13 +5,20 @@ import org.springframework.validation.Errors; public class RegistrationForm { - private final @NotEmpty String username, password, address; + private final @NotEmpty String username, password, address, lastname, firstname; - public RegistrationForm(String username, String password, String address) { + public RegistrationForm( + String username, + String password, + String address, + String firstname, + String lastname) { this.username = username; this.password = password; this.address = address; + this.lastname = lastname; + this.firstname = firstname; } public String getUsername() { @@ -26,6 +33,14 @@ public class RegistrationForm { return address; } + public String getLastname() { + return lastname; + } + + public String getFirstname() { + return firstname; + } + public void validate(Errors errors) { } diff --git a/src/main/java/catering/users/User.java b/src/main/java/catering/users/User.java index 2d1bf24..63541cc 100644 --- a/src/main/java/catering/users/User.java +++ b/src/main/java/catering/users/User.java @@ -51,6 +51,24 @@ public class User extends AbstractAggregateRoot { return true; } + public String getLastname() { + return userAccount.getLastname(); + }; + + public boolean setLastname(String name){ + userAccount.setLastname(name); + return true; + } + + public String getFirstname() { + return userAccount.getFirstname(); + }; + + public boolean setFirstname(String name){ + userAccount.setFirstname(name); + return true; + } + public UserAccount getUserAccount(){ return this.userAccount; } diff --git a/src/main/java/catering/users/UserController.java b/src/main/java/catering/users/UserController.java index 2d3ae1b..9a7bb43 100644 --- a/src/main/java/catering/users/UserController.java +++ b/src/main/java/catering/users/UserController.java @@ -44,7 +44,13 @@ public class UserController { return "register"; } else { model.addAttribute("userNameExists",false); - userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword()); + userManagement.createCustomer( + form.getUsername(), + form.getAddress(), + form.getPassword(), + form.getLastname(), + form.getFirstname() + ); return "redirect:/login"; } } @@ -61,18 +67,33 @@ public class UserController { return "redirect:/"; } User user = userManagement.getUserByAccount(userAccount).get(); + System.out.println(user.getLastname()); // WHY ARE YOU NULL?!!!!! + System.out.println(user.getFirstname()); model.addAttribute("user", user); return "profile"; } @PostMapping("/profile") @PreAuthorize("isAuthenticated()") - public String editProfile(@LoggedIn UserAccount userAccount, @RequestParam String password, @RequestParam String address, @RequestParam String username) { + public String editProfile( + @LoggedIn UserAccount userAccount, + @RequestParam String password, + @RequestParam String address, + @RequestParam String username, + @RequestParam String lastname, + @RequestParam String firstname + ) { User user = userManagement.getUserByAccount(userAccount).get(); if (!username.isBlank()) { user.setUsername(username); } + if (!lastname.isBlank()) { + user.setLastname(lastname); + } + if (!firstname.isBlank()) { + user.setFirstname(firstname); + } if (!address.isBlank()) { user.setAddress(address); } diff --git a/src/main/java/catering/users/UserManagement.java b/src/main/java/catering/users/UserManagement.java index da74e67..fff7457 100644 --- a/src/main/java/catering/users/UserManagement.java +++ b/src/main/java/catering/users/UserManagement.java @@ -23,8 +23,19 @@ public class UserManagement { this.userAccounts = userAccounts; } - public User createCustomer(String name, String address, String password) { - return users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("CUSTOMER")), address)); + public User createCustomer( + String name, + String address, + String password, + String lastname, + String firstname + ) { + System.out.println(lastname); + User user = users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("CUSTOMER")),address)); + user.setLastname(lastname); + user.setFirstname(firstname); + users.save(user); + return user; } public User createAdmin(String name, String address, String password) { diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 0e96246..827d212 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -20,6 +20,14 @@

Nutzerinformationen

+
+ + +
+
+ + +
diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html index d4f5db4..79fa05c 100644 --- a/src/main/resources/templates/register.html +++ b/src/main/resources/templates/register.html @@ -15,6 +15,14 @@
+
+ + +
+
+ + +