Commit 7a765da25098cb6c12f625e6037af0ae0f0162af
1 parent
493ca844
refactor: updateUser mit Kontext und Mapper re-implementiert
Showing
7 changed files
with
21 additions
and
6 deletions
src/main/java/net/ziemers/swxercise/lg/model/user/Profile.java
| @@ -145,7 +145,7 @@ public class Profile extends BaseEntity { | @@ -145,7 +145,7 @@ public class Profile extends BaseEntity { | ||
| 145 | return passwordHash; | 145 | return passwordHash; |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | - private void setPassword(String password) { | 148 | + public void setPassword(String password) { |
| 149 | // das Klartextkennwort wird niemals gespeichert! | 149 | // das Klartextkennwort wird niemals gespeichert! |
| 150 | this.passwordHash = cryptString(password); | 150 | this.passwordHash = cryptString(password); |
| 151 | } | 151 | } |
src/main/java/net/ziemers/swxercise/lg/model/user/User.java
| @@ -18,7 +18,6 @@ public class User extends BaseEntity { | @@ -18,7 +18,6 @@ public class User extends BaseEntity { | ||
| 18 | 18 | ||
| 19 | private String lastname; | 19 | private String lastname; |
| 20 | 20 | ||
| 21 | - @NotNull | ||
| 22 | private Profile profile; | 21 | private Profile profile; |
| 23 | 22 | ||
| 24 | private Address address; | 23 | private Address address; |
src/main/java/net/ziemers/swxercise/lg/user/dto/UserDto.java
| @@ -14,7 +14,7 @@ public class UserDto { | @@ -14,7 +14,7 @@ public class UserDto { | ||
| 14 | private String username; // aus dem Profile | 14 | private String username; // aus dem Profile |
| 15 | 15 | ||
| 16 | @NotNull | 16 | @NotNull |
| 17 | - private String password; // aus dem Profile | 17 | + private String password = ""; // aus dem Profile |
| 18 | 18 | ||
| 19 | private String firstname; | 19 | private String firstname; |
| 20 | 20 |
src/main/java/net/ziemers/swxercise/lg/user/service/SessionContext.java
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityContextService.java
| @@ -23,7 +23,7 @@ public class UserDtoToEntityContextService { | @@ -23,7 +23,7 @@ public class UserDtoToEntityContextService { | ||
| 23 | public UserDtoToEntityContext createContext(final UserDto dto) { | 23 | public UserDtoToEntityContext createContext(final UserDto dto) { |
| 24 | final UserDtoToEntityContext ctx = new UserDtoToEntityContext(); | 24 | final UserDtoToEntityContext ctx = new UserDtoToEntityContext(); |
| 25 | 25 | ||
| 26 | - // das UserDTO in den Kontext füllen | 26 | + // das übergebene UserDto in den Kontext füllen |
| 27 | ctx.dto = dto; | 27 | ctx.dto = dto; |
| 28 | 28 | ||
| 29 | // einen neuen oder einen bereits existierenden Benutzer in den Kontext füllen | 29 | // einen neuen oder einen bereits existierenden Benutzer in den Kontext füllen |
| @@ -35,6 +35,9 @@ public class UserDtoToEntityContextService { | @@ -35,6 +35,9 @@ public class UserDtoToEntityContextService { | ||
| 35 | // wir füllen das User-Objekt mit Method Chaining | 35 | // wir füllen das User-Objekt mit Method Chaining |
| 36 | ctx.user = new User(dto.getFirstname(), dto.getLastname()) | 36 | ctx.user = new User(dto.getFirstname(), dto.getLastname()) |
| 37 | .withProfile(ctx.profile); | 37 | .withProfile(ctx.profile); |
| 38 | + } else { | ||
| 39 | + ctx.profile = ctx.user.getProfile(); | ||
| 40 | + ctx.address = ctx.user.getAddress(); | ||
| 38 | } | 41 | } |
| 39 | return ctx; | 42 | return ctx; |
| 40 | } | 43 | } |
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityMapper.java
| @@ -17,11 +17,20 @@ public class UserDtoToEntityMapper { | @@ -17,11 +17,20 @@ public class UserDtoToEntityMapper { | ||
| 17 | * @return den Kontext. | 17 | * @return den Kontext. |
| 18 | */ | 18 | */ |
| 19 | public UserDtoToEntityContext map(UserDtoToEntityContext ctx) { | 19 | public UserDtoToEntityContext map(UserDtoToEntityContext ctx) { |
| 20 | + // User-Objekt mappen | ||
| 20 | ctx.user.setFirstname(ctx.dto.getFirstname()); | 21 | ctx.user.setFirstname(ctx.dto.getFirstname()); |
| 21 | ctx.user.setLastname(ctx.dto.getLastname()); | 22 | ctx.user.setLastname(ctx.dto.getLastname()); |
| 23 | + | ||
| 24 | + // Profile-Objekt mappen, falls gegeben | ||
| 22 | if (ctx.profile != null) { | 25 | if (ctx.profile != null) { |
| 23 | ctx.user.setProfile(ctx.profile); | 26 | ctx.user.setProfile(ctx.profile); |
| 27 | + if(ctx.dto.getPassword().length() > 0) { | ||
| 28 | + ctx.profile.setPassword(ctx.dto.getPassword()); | ||
| 29 | + } | ||
| 30 | + ctx.profile.setMailaddress(ctx.dto.getMailaddress()); | ||
| 24 | } | 31 | } |
| 32 | + | ||
| 33 | + // Address-Objekt mappen, falls gegeben | ||
| 25 | if (ctx.address != null) { | 34 | if (ctx.address != null) { |
| 26 | ctx.user.setAddress(ctx.address); | 35 | ctx.user.setAddress(ctx.address); |
| 27 | } | 36 | } |
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
| @@ -123,8 +123,11 @@ public class UserService { | @@ -123,8 +123,11 @@ public class UserService { | ||
| 123 | // ist zurzeit ein Benutzer angemeldet, können wir ihn aktualisieren | 123 | // ist zurzeit ein Benutzer angemeldet, können wir ihn aktualisieren |
| 124 | final User user = sessionContext.getUser(); | 124 | final User user = sessionContext.getUser(); |
| 125 | if (user != null) { | 125 | if (user != null) { |
| 126 | - // TODO noch zu implementieren | ||
| 127 | - return false; | 126 | + // der Benutzername darf sich beim Aktualisieren nicht mehr ändern! |
| 127 | + dto.withUsername(user.getProfile().getUsername()); | ||
| 128 | + final UserDtoToEntityContext ctx = ctxService.createContext(dto); | ||
| 129 | + mapper.map(ctx); | ||
| 130 | + return dao.saveOrUpdate(ctx.user) != null; | ||
| 128 | } | 131 | } |
| 129 | return false; | 132 | return false; |
| 130 | } | 133 | } |