WHY ARE YOU NULL

This commit is contained in:
Denis Natusch 2023-11-19 18:56:45 +01:00
parent 56cf10af8c
commit 5915d20f71
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985
6 changed files with 87 additions and 6 deletions

View file

@ -5,13 +5,20 @@ import org.springframework.validation.Errors;
public class RegistrationForm { 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.username = username;
this.password = password; this.password = password;
this.address = address; this.address = address;
this.lastname = lastname;
this.firstname = firstname;
} }
public String getUsername() { public String getUsername() {
@ -26,6 +33,14 @@ public class RegistrationForm {
return address; return address;
} }
public String getLastname() {
return lastname;
}
public String getFirstname() {
return firstname;
}
public void validate(Errors errors) { public void validate(Errors errors) {
} }

View file

@ -51,6 +51,24 @@ public class User extends AbstractAggregateRoot<UserIdentifier> {
return true; 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(){ public UserAccount getUserAccount(){
return this.userAccount; return this.userAccount;
} }

View file

@ -44,7 +44,13 @@ public class UserController {
return "register"; return "register";
} else { } else {
model.addAttribute("userNameExists",false); 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"; return "redirect:/login";
} }
} }
@ -61,18 +67,33 @@ public class UserController {
return "redirect:/"; return "redirect:/";
} }
User user = userManagement.getUserByAccount(userAccount).get(); User user = userManagement.getUserByAccount(userAccount).get();
System.out.println(user.getLastname()); // WHY ARE YOU NULL?!!!!!
System.out.println(user.getFirstname());
model.addAttribute("user", user); model.addAttribute("user", user);
return "profile"; return "profile";
} }
@PostMapping("/profile") @PostMapping("/profile")
@PreAuthorize("isAuthenticated()") @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(); User user = userManagement.getUserByAccount(userAccount).get();
if (!username.isBlank()) { if (!username.isBlank()) {
user.setUsername(username); user.setUsername(username);
} }
if (!lastname.isBlank()) {
user.setLastname(lastname);
}
if (!firstname.isBlank()) {
user.setFirstname(firstname);
}
if (!address.isBlank()) { if (!address.isBlank()) {
user.setAddress(address); user.setAddress(address);
} }

View file

@ -23,8 +23,19 @@ public class UserManagement {
this.userAccounts = userAccounts; this.userAccounts = userAccounts;
} }
public User createCustomer(String name, String address, String password) { public User createCustomer(
return users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("CUSTOMER")), address)); 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) { public User createAdmin(String name, String address, String password) {

View file

@ -20,6 +20,14 @@
</div> </div>
<h2>Nutzerinformationen</h2> <h2>Nutzerinformationen</h2>
<div class="mb-3">
<label class="form-label" for="firstname">Vorname</label>
<input class="form-control" name="firstname" th:value="${user.firstname}" type="text">
</div>
<div class="mb-3">
<label class="form-label" for="lastname">Nachname</label>
<input class="form-control" name="lastname" th:value="${user.lastname}" type="text">
</div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="address">Adresse</label> <label class="form-label" for="address">Adresse</label>
<input class="form-control" name="address" th:value="${user.address}" type="text"> <input class="form-control" name="address" th:value="${user.address}" type="text">

View file

@ -15,6 +15,14 @@
<label class="form-label" for="password">Passwort</label> <label class="form-label" for="password">Passwort</label>
<input class="form-control" name="password" type="password" required> <input class="form-control" name="password" type="password" required>
</div> </div>
<div class="mb-3">
<label class="form-label" for="firstname">Vorname</label>
<input class="form-control" name="firstname" type="text" required>
</div>
<div class="mb-3">
<label class="form-label" for="lastname">Nachname</label>
<input class="form-control" name="lastname" type="text" required>
</div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="address">Adresse</label> <label class="form-label" for="address">Adresse</label>
<input class="form-control" name="address" type="text" required> <input class="form-control" name="address" type="text" required>