Commit 0bbe393da7f117487144d74b221c9ae5d3883317
1 parent
b4808f61
feature: lesen und schreiben mittels REST/Jackson
Showing
6 changed files
with
39 additions
and
12 deletions
src/main/java/net/ziemers/swxercise/lg/user/Profile.java
@@ -4,6 +4,7 @@ import javax.persistence.Entity; | @@ -4,6 +4,7 @@ import javax.persistence.Entity; | ||
4 | import javax.validation.constraints.NotNull; | 4 | import javax.validation.constraints.NotNull; |
5 | 5 | ||
6 | import net.ziemers.swxercise.db.BaseEntity; | 6 | import net.ziemers.swxercise.db.BaseEntity; |
7 | +import net.ziemers.swxercise.lg.user.enums.PasswordHashType; | ||
7 | 8 | ||
8 | @Entity | 9 | @Entity |
9 | public class Profile extends BaseEntity { | 10 | public class Profile extends BaseEntity { |
@@ -14,6 +15,9 @@ public class Profile extends BaseEntity { | @@ -14,6 +15,9 @@ public class Profile extends BaseEntity { | ||
14 | @NotNull | 15 | @NotNull |
15 | private String passwordHash; | 16 | private String passwordHash; |
16 | 17 | ||
18 | + @NotNull | ||
19 | + private PasswordHashType passwordHashType = PasswordHashType.Unknown; | ||
20 | + | ||
17 | private String mailaddress; | 21 | private String mailaddress; |
18 | 22 | ||
19 | public Profile() { | 23 | public Profile() { |
@@ -34,14 +38,22 @@ public class Profile extends BaseEntity { | @@ -34,14 +38,22 @@ public class Profile extends BaseEntity { | ||
34 | this.username = username; | 38 | this.username = username; |
35 | } | 39 | } |
36 | 40 | ||
37 | - public String getPasswordHash() { | 41 | + private String getPasswordHash() { |
38 | return passwordHash; | 42 | return passwordHash; |
39 | } | 43 | } |
40 | 44 | ||
41 | - public void setPasswordHash(String passwordHash) { | 45 | + private void setPasswordHash(String passwordHash) { |
42 | this.passwordHash = passwordHash; | 46 | this.passwordHash = passwordHash; |
43 | } | 47 | } |
44 | 48 | ||
49 | + private PasswordHashType getPasswordHashType() { | ||
50 | + return passwordHashType; | ||
51 | + } | ||
52 | + | ||
53 | + private void setPasswordHashType(PasswordHashType passwordHashType) { | ||
54 | + this.passwordHashType = passwordHashType; | ||
55 | + } | ||
56 | + | ||
45 | public String getMailaddress() { | 57 | public String getMailaddress() { |
46 | return mailaddress; | 58 | return mailaddress; |
47 | } | 59 | } |
src/main/java/net/ziemers/swxercise/lg/user/User.java
@@ -51,4 +51,9 @@ public class User extends BaseEntity { | @@ -51,4 +51,9 @@ public class User extends BaseEntity { | ||
51 | this.profile = profile; | 51 | this.profile = profile; |
52 | } | 52 | } |
53 | 53 | ||
54 | + public User withProfile(final Profile profile) { | ||
55 | + setProfile(profile); | ||
56 | + return this; | ||
57 | + } | ||
58 | + | ||
54 | } | 59 | } |
src/main/java/net/ziemers/swxercise/lg/user/enums/PasswordHashType.java
0 → 100644
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
@@ -6,7 +6,9 @@ import javax.ejb.Stateless; | @@ -6,7 +6,9 @@ import javax.ejb.Stateless; | ||
6 | import javax.inject.Inject; | 6 | import javax.inject.Inject; |
7 | 7 | ||
8 | import net.ziemers.swxercise.db.dao.GenericDao; | 8 | import net.ziemers.swxercise.db.dao.GenericDao; |
9 | +import net.ziemers.swxercise.lg.user.Profile; | ||
9 | import net.ziemers.swxercise.lg.user.User; | 10 | import net.ziemers.swxercise.lg.user.User; |
11 | +import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
10 | 12 | ||
11 | @Stateless | 13 | @Stateless |
12 | public class UserService { | 14 | public class UserService { |
@@ -20,7 +22,13 @@ public class UserService { | @@ -20,7 +22,13 @@ public class UserService { | ||
20 | return genericDao.findAll(User.class); | 22 | return genericDao.findAll(User.class); |
21 | } | 23 | } |
22 | 24 | ||
23 | - public Long saveUser(final User user) { | 25 | + public Long saveUser(final UserDto userDto) { |
26 | + final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); | ||
27 | + | ||
28 | + // wir füllen das User-Objekt mit Method Chaining | ||
29 | + final User user = new User(userDto.getFirstname(), userDto.getLastname()) | ||
30 | + .withProfile(profile); | ||
31 | + | ||
24 | return genericDao.save(user); | 32 | return genericDao.save(user); |
25 | } | 33 | } |
26 | 34 |
src/main/java/net/ziemers/swxercise/ui/RestViewController.java renamed to src/main/java/net/ziemers/swxercise/ui/UserViewController.java
@@ -13,8 +13,8 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; | @@ -13,8 +13,8 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
13 | import net.ziemers.swxercise.lg.user.service.UserService; | 13 | import net.ziemers.swxercise.lg.user.service.UserService; |
14 | 14 | ||
15 | @ApplicationScoped | 15 | @ApplicationScoped |
16 | -@Path(RestViewController.webContextPath) | ||
17 | -public class RestViewController { | 16 | +@Path(UserViewController.webContextPath) |
17 | +public class UserViewController { | ||
18 | 18 | ||
19 | static final String webContextPath = "/users"; | 19 | static final String webContextPath = "/users"; |
20 | 20 | ||
@@ -53,12 +53,7 @@ public class RestViewController { | @@ -53,12 +53,7 @@ public class RestViewController { | ||
53 | @Consumes(MediaType.APPLICATION_JSON) | 53 | @Consumes(MediaType.APPLICATION_JSON) |
54 | @Produces({MediaType.TEXT_PLAIN}) | 54 | @Produces({MediaType.TEXT_PLAIN}) |
55 | public String postUser(UserDto userDto) throws Exception { | 55 | public String postUser(UserDto userDto) throws Exception { |
56 | - final User user = new User(userDto.getFirstname(), userDto.getLastname()); | ||
57 | - final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); | ||
58 | - | ||
59 | - // TODO hierfür benötigen wir einen Mapper | ||
60 | - user.setProfile(profile); | ||
61 | - userService.saveUser(user); | 56 | + userService.saveUser(userDto); |
62 | 57 | ||
63 | return "Ok"; | 58 | return "Ok"; |
64 | } | 59 | } |
src/test/java/net/ziemers/swxercise/ui/RestViewControllerTest.java
@@ -10,7 +10,7 @@ import org.mockito.junit.MockitoJUnitRunner; | @@ -10,7 +10,7 @@ import org.mockito.junit.MockitoJUnitRunner; | ||
10 | public class RestViewControllerTest { | 10 | public class RestViewControllerTest { |
11 | 11 | ||
12 | @InjectMocks | 12 | @InjectMocks |
13 | - private RestViewController underTest; | 13 | + private UserViewController underTest; |
14 | 14 | ||
15 | @Test | 15 | @Test |
16 | public void test() { | 16 | public void test() { |