Commit b4808f612a7e42cc01f6c6edfc2d4515d26579b6
1 parent
ab335a1a
feature: lesen und schreiben mittels REST/Jackson
Showing
7 changed files
with
130 additions
and
13 deletions
pom.xml
| @@ -18,6 +18,7 @@ | @@ -18,6 +18,7 @@ | ||
| 18 | <mockito-version>2.7.22</mockito-version> | 18 | <mockito-version>2.7.22</mockito-version> |
| 19 | <dbunit-version>2.5.1</dbunit-version> | 19 | <dbunit-version>2.5.1</dbunit-version> |
| 20 | <slf4j-version>1.8.0-alpha0</slf4j-version> | 20 | <slf4j-version>1.8.0-alpha0</slf4j-version> |
| 21 | + <jackson-version>2.8.8.1</jackson-version> | ||
| 21 | </properties> | 22 | </properties> |
| 22 | <dependencies> | 23 | <dependencies> |
| 23 | <dependency> | 24 | <dependency> |
| @@ -62,6 +63,11 @@ | @@ -62,6 +63,11 @@ | ||
| 62 | <artifactId>mysql-connector-java</artifactId> | 63 | <artifactId>mysql-connector-java</artifactId> |
| 63 | <version>${mysql-version}</version> | 64 | <version>${mysql-version}</version> |
| 64 | </dependency> | 65 | </dependency> |
| 66 | + <dependency> | ||
| 67 | + <groupId>com.fasterxml.jackson.core</groupId> | ||
| 68 | + <artifactId>jackson-databind</artifactId> | ||
| 69 | + <version>${jackson-version}</version> | ||
| 70 | + </dependency> | ||
| 65 | </dependencies> | 71 | </dependencies> |
| 66 | <build> | 72 | <build> |
| 67 | <finalName>swXercise</finalName> | 73 | <finalName>swXercise</finalName> |
src/main/java/net/ziemers/swxercise/db/BaseEntity.java
| @@ -6,13 +6,17 @@ import javax.persistence.MappedSuperclass; | @@ -6,13 +6,17 @@ import javax.persistence.MappedSuperclass; | ||
| 6 | 6 | ||
| 7 | @MappedSuperclass | 7 | @MappedSuperclass |
| 8 | public class BaseEntity { | 8 | public class BaseEntity { |
| 9 | - | 9 | + |
| 10 | + protected Long id; | ||
| 11 | + | ||
| 10 | @Id | 12 | @Id |
| 11 | @GeneratedValue | 13 | @GeneratedValue |
| 12 | - protected Long id; | ||
| 13 | - | ||
| 14 | public Long getId() { | 14 | public Long getId() { |
| 15 | return id; | 15 | return id; |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | + public void setId(Long id) { | ||
| 19 | + this.id = id; | ||
| 20 | + } | ||
| 21 | + | ||
| 18 | } | 22 | } |
| 19 | \ No newline at end of file | 23 | \ No newline at end of file |
src/main/java/net/ziemers/swxercise/lg/user/Profile.java
| 1 | package net.ziemers.swxercise.lg.user; | 1 | package net.ziemers.swxercise.lg.user; |
| 2 | 2 | ||
| 3 | import javax.persistence.Entity; | 3 | import javax.persistence.Entity; |
| 4 | +import javax.validation.constraints.NotNull; | ||
| 4 | 5 | ||
| 5 | import net.ziemers.swxercise.db.BaseEntity; | 6 | import net.ziemers.swxercise.db.BaseEntity; |
| 6 | 7 | ||
| 7 | @Entity | 8 | @Entity |
| 8 | public class Profile extends BaseEntity { | 9 | public class Profile extends BaseEntity { |
| 9 | 10 | ||
| 11 | + @NotNull | ||
| 12 | + private String username; | ||
| 13 | + | ||
| 14 | + @NotNull | ||
| 10 | private String passwordHash; | 15 | private String passwordHash; |
| 11 | 16 | ||
| 12 | private String mailaddress; | 17 | private String mailaddress; |
| 13 | 18 | ||
| 19 | + public Profile() { | ||
| 20 | + super(); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public Profile(final String username, final String password) { | ||
| 24 | + this.username = username; | ||
| 25 | + // TODO Kennwort verhashen | ||
| 26 | + this.passwordHash = password; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + public String getUsername() { | ||
| 30 | + return username; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + public void setUsername(String username) { | ||
| 34 | + this.username = username; | ||
| 35 | + } | ||
| 36 | + | ||
| 14 | public String getPasswordHash() { | 37 | public String getPasswordHash() { |
| 15 | return passwordHash; | 38 | return passwordHash; |
| 16 | } | 39 | } |
src/main/java/net/ziemers/swxercise/lg/user/User.java
| 1 | package net.ziemers.swxercise.lg.user; | 1 | package net.ziemers.swxercise.lg.user; |
| 2 | 2 | ||
| 3 | +import javax.persistence.CascadeType; | ||
| 3 | import javax.persistence.Entity; | 4 | import javax.persistence.Entity; |
| 5 | +import javax.persistence.OneToOne; | ||
| 6 | +import javax.validation.constraints.NotNull; | ||
| 4 | 7 | ||
| 5 | import net.ziemers.swxercise.db.BaseEntity; | 8 | import net.ziemers.swxercise.db.BaseEntity; |
| 6 | 9 | ||
| @@ -11,6 +14,18 @@ public class User extends BaseEntity { | @@ -11,6 +14,18 @@ public class User extends BaseEntity { | ||
| 11 | 14 | ||
| 12 | private String lastname; | 15 | private String lastname; |
| 13 | 16 | ||
| 17 | + @NotNull | ||
| 18 | + private Profile profile; | ||
| 19 | + | ||
| 20 | + public User() { | ||
| 21 | + super(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public User(final String firstname, final String lastname) { | ||
| 25 | + this.firstname = firstname; | ||
| 26 | + this.lastname = lastname; | ||
| 27 | + } | ||
| 28 | + | ||
| 14 | public String getFirstname() { | 29 | public String getFirstname() { |
| 15 | return firstname; | 30 | return firstname; |
| 16 | } | 31 | } |
| @@ -27,4 +42,13 @@ public class User extends BaseEntity { | @@ -27,4 +42,13 @@ public class User extends BaseEntity { | ||
| 27 | this.lastname = lastname; | 42 | this.lastname = lastname; |
| 28 | } | 43 | } |
| 29 | 44 | ||
| 45 | + @OneToOne(cascade = {CascadeType.ALL}) | ||
| 46 | + public Profile getProfile() { | ||
| 47 | + return profile; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + public void setProfile(Profile profile) { | ||
| 51 | + this.profile = profile; | ||
| 52 | + } | ||
| 53 | + | ||
| 30 | } | 54 | } |
src/main/java/net/ziemers/swxercise/lg/user/dto/UserDto.java
0 → 100644
| 1 | +package net.ziemers.swxercise.lg.user.dto; | ||
| 2 | + | ||
| 3 | +import javax.validation.constraints.NotNull; | ||
| 4 | + | ||
| 5 | +public class UserDto { | ||
| 6 | + | ||
| 7 | + @NotNull | ||
| 8 | + private String username; | ||
| 9 | + | ||
| 10 | + @NotNull | ||
| 11 | + private String password; | ||
| 12 | + | ||
| 13 | + private String firstname; | ||
| 14 | + | ||
| 15 | + private String lastname; | ||
| 16 | + | ||
| 17 | + private String mailaddress; | ||
| 18 | + | ||
| 19 | + public String getUsername() { | ||
| 20 | + return username; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public String getPassword() { | ||
| 24 | + return password; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public String getFirstname() { | ||
| 28 | + return firstname; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public String getLastname() { | ||
| 32 | + return lastname; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public String getMailaddress() { | ||
| 36 | + return mailaddress; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | +} |
src/main/java/net/ziemers/swxercise/ui/RestController.java renamed to src/main/java/net/ziemers/swxercise/ui/RestViewController.java
| @@ -4,20 +4,19 @@ import java.util.Collection; | @@ -4,20 +4,19 @@ import java.util.Collection; | ||
| 4 | 4 | ||
| 5 | import javax.enterprise.context.ApplicationScoped; | 5 | import javax.enterprise.context.ApplicationScoped; |
| 6 | import javax.inject.Inject; | 6 | import javax.inject.Inject; |
| 7 | -import javax.ws.rs.GET; | ||
| 8 | -import javax.ws.rs.Path; | ||
| 9 | -import javax.ws.rs.PathParam; | ||
| 10 | -import javax.ws.rs.Produces; | 7 | +import javax.ws.rs.*; |
| 11 | import javax.ws.rs.core.MediaType; | 8 | import javax.ws.rs.core.MediaType; |
| 12 | 9 | ||
| 10 | +import net.ziemers.swxercise.lg.user.Profile; | ||
| 13 | import net.ziemers.swxercise.lg.user.User; | 11 | import net.ziemers.swxercise.lg.user.User; |
| 12 | +import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
| 14 | import net.ziemers.swxercise.lg.user.service.UserService; | 13 | import net.ziemers.swxercise.lg.user.service.UserService; |
| 15 | 14 | ||
| 16 | @ApplicationScoped | 15 | @ApplicationScoped |
| 17 | -@Path(RestController.webContextPath) | ||
| 18 | -public class RestController { | 16 | +@Path(RestViewController.webContextPath) |
| 17 | +public class RestViewController { | ||
| 19 | 18 | ||
| 20 | - static final String webContextPath = "/user"; | 19 | + static final String webContextPath = "/users"; |
| 21 | 20 | ||
| 22 | @Inject | 21 | @Inject |
| 23 | private UserService userService; | 22 | private UserService userService; |
| @@ -36,10 +35,32 @@ public class RestController { | @@ -36,10 +35,32 @@ public class RestController { | ||
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | @GET | 37 | @GET |
| 39 | - @Path("/get") | 38 | + @Path("/all") |
| 40 | @Produces(MediaType.APPLICATION_JSON) | 39 | @Produces(MediaType.APPLICATION_JSON) |
| 41 | public Collection<User> getAllUsers() { | 40 | public Collection<User> getAllUsers() { |
| 42 | return userService.findAllUsers(); | 41 | return userService.findAllUsers(); |
| 43 | } | 42 | } |
| 44 | 43 | ||
| 44 | + @GET | ||
| 45 | + @Path("{id}") | ||
| 46 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 47 | + public User getById(@PathParam("id") Long id) { | ||
| 48 | + return userService.findUser(id); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @PUT | ||
| 52 | + @Path("/user") | ||
| 53 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 54 | + @Produces({MediaType.TEXT_PLAIN}) | ||
| 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); | ||
| 62 | + | ||
| 63 | + return "Ok"; | ||
| 64 | + } | ||
| 65 | + | ||
| 45 | } | 66 | } |
src/test/java/net/ziemers/swxercise/ui/RestControllerTest.java renamed to src/test/java/net/ziemers/swxercise/ui/RestViewControllerTest.java
| @@ -7,10 +7,10 @@ import org.mockito.InjectMocks; | @@ -7,10 +7,10 @@ import org.mockito.InjectMocks; | ||
| 7 | import org.mockito.junit.MockitoJUnitRunner; | 7 | import org.mockito.junit.MockitoJUnitRunner; |
| 8 | 8 | ||
| 9 | @RunWith(MockitoJUnitRunner.class) | 9 | @RunWith(MockitoJUnitRunner.class) |
| 10 | -public class RestControllerTest { | 10 | +public class RestViewControllerTest { |
| 11 | 11 | ||
| 12 | @InjectMocks | 12 | @InjectMocks |
| 13 | - private RestController underTest; | 13 | + private RestViewController underTest; |
| 14 | 14 | ||
| 15 | @Test | 15 | @Test |
| 16 | public void test() { | 16 | public void test() { |