Add more attributes to user/customer

Closes #42
This commit is contained in:
Denis Natusch 2023-11-20 21:38:30 +01:00 committed by Simon Bruder
parent a8a67a9626
commit ea24c27d6e
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
7 changed files with 82 additions and 24 deletions

View file

@ -0,0 +1,38 @@
package catering.users;
import jakarta.validation.constraints.NotEmpty;
import java.util.Optional;
import org.springframework.validation.Errors;
public class ProfileForm {
private final @NotEmpty String username, address, fullName;
private final Optional<String> password;
public ProfileForm(String username, Optional<String> password, String address, String fullName) {
this.username = username;
this.address = address;
this.fullName = fullName;
this.password = password;
}
public String getUsername() {
return username;
}
public Optional<String> getPassword() {
return password;
}
public String getAddress() {
return address;
}
public String getFullName() {
return fullName;
}
public void validate(Errors errors) {
}
}

View file

@ -5,13 +5,13 @@ import org.springframework.validation.Errors;
public class RegistrationForm { public class RegistrationForm {
private final @NotEmpty String username, password, address; private final @NotEmpty String password, username, address, fullName;
public RegistrationForm(String username, String password, String address) {
public RegistrationForm(String username, String password, String address, String fullName) {
this.username = username; this.username = username;
this.password = password;
this.address = address; this.address = address;
this.fullName = fullName;
this.password = password;
} }
public String getUsername() { public String getUsername() {
@ -26,6 +26,10 @@ public class RegistrationForm {
return address; return address;
} }
public String getFullName() {
return fullName;
}
public void validate(Errors errors) { public void validate(Errors errors) {
} }

View file

@ -18,7 +18,7 @@ import org.jmolecules.ddd.types.Identifier;
public class User extends AbstractAggregateRoot<UserIdentifier> { public class User extends AbstractAggregateRoot<UserIdentifier> {
private @EmbeddedId UserIdentifier id = new UserIdentifier(); private @EmbeddedId UserIdentifier id = new UserIdentifier();
private String address; private String address, fullName;
@OneToOne @OneToOne
private UserAccount userAccount; private UserAccount userAccount;
@ -26,8 +26,9 @@ public class User extends AbstractAggregateRoot<UserIdentifier> {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private User() {} private User() {}
public User(UserAccount userAccount,String address){ public User(UserAccount userAccount, String address, String fullName){
this.address = address; this.address = address;
this.fullName = fullName;
this.userAccount = userAccount; this.userAccount = userAccount;
}; };
@ -51,6 +52,15 @@ public class User extends AbstractAggregateRoot<UserIdentifier> {
return true; return true;
} }
public String getFullName() {
return fullName;
};
public boolean setFullName(String name){
fullName = name;
return true;
}
public UserAccount getUserAccount(){ public UserAccount getUserAccount(){
return this.userAccount; return this.userAccount;
} }

View file

@ -39,7 +39,7 @@ public class UserController {
if (result.hasErrors()){ if (result.hasErrors()){
return "register"; return "register";
} }
userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword()); userManagement.createCustomer(form.getUsername(),form.getAddress(),form.getPassword(),form.getFullName());
return "redirect:/login"; return "redirect:/login";
} }
@ -61,25 +61,18 @@ public class UserController {
@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, @Valid ProfileForm form) {
User user = userManagement.getUserByAccount(userAccount).get(); User user = userManagement.getUserByAccount(userAccount).get();
if (!username.isBlank()) { user.setUsername(form.getUsername());
user.setUsername(username); user.setFullName(form.getFullName());
user.setAddress(form.getAddress());
if (!form.getPassword().get().isEmpty()) {
userManagement.setPassword(form.getPassword().get(), user.getUserAccount());
} }
if (!address.isBlank()) {
user.setAddress(address);
}
if (!password.isBlank()) {
userManagement.setPassword(password, user.getUserAccount());
}
userManagement.save(user); userManagement.save(user);
if (!username.isBlank()){ // by default the user gets logged out by salespoint after changing the user name
return "redirect:/logout";
}
return "redirect:/profile"; return "redirect:/profile";
} }

View file

@ -23,12 +23,17 @@ 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 username,
String address,
String password,
String fullName
) {
return users.save(new User(userAccounts.create(username,UnencryptedPassword.of(password),Role.of("CUSTOMER")),address,fullName));
} }
public User createAdmin(String name, String address, String password) { public User createAdmin(String name, String address, String password) {
return users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("ADMIN")), address)); return users.save(new User(userAccounts.create(name,UnencryptedPassword.of(password),Role.of("ADMIN")), address, name));
} }
public User save(User user) { public User save(User user) {

View file

@ -20,6 +20,10 @@
</div> </div>
<h2>Nutzerinformationen</h2> <h2>Nutzerinformationen</h2>
<div class="mb-3">
<label class="form-label" for="fullName">Name</label>
<input class="form-control" name="fullName" th:value="${user.fullName}" 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

@ -14,6 +14,10 @@
<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="fullName">Name</label>
<input class="form-control" name="fullName" 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>