Commit 7e738cb7fc72ed327f16171758809aacb155a8d7
1 parent
2392615c
refactor: die REST-Methoden schicker gemacht
Showing
4 changed files
with
71 additions
and
24 deletions
src/main/java/net/ziemers/swxercise/db/dao/GenericDao.java
| @@ -35,6 +35,13 @@ public class GenericDao { | @@ -35,6 +35,13 @@ public class GenericDao { | ||
| 35 | return entityManager.merge(entity); | 35 | return entityManager.merge(entity); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | + public <T extends BaseEntity> T remove(Class<T> entityType, Long primaryKey) { | ||
| 39 | + final T entity = findById(entityType, primaryKey); | ||
| 40 | + | ||
| 41 | + entityManager.remove(entity); | ||
| 42 | + return entity; | ||
| 43 | + } | ||
| 44 | + | ||
| 38 | /** | 45 | /** |
| 39 | * @param entityType Entity-Typ. | 46 | * @param entityType Entity-Typ. |
| 40 | * @param primaryKey Id | 47 | * @param primaryKey Id |
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
| @@ -5,7 +5,6 @@ import java.util.Collection; | @@ -5,7 +5,6 @@ import java.util.Collection; | ||
| 5 | import javax.ejb.Stateless; | 5 | import javax.ejb.Stateless; |
| 6 | import javax.inject.Inject; | 6 | import javax.inject.Inject; |
| 7 | 7 | ||
| 8 | -import net.ziemers.swxercise.db.dao.GenericDao; | ||
| 9 | import net.ziemers.swxercise.db.dao.user.UserDao; | 8 | import net.ziemers.swxercise.db.dao.user.UserDao; |
| 10 | import net.ziemers.swxercise.lg.model.user.Profile; | 9 | import net.ziemers.swxercise.lg.model.user.Profile; |
| 11 | import net.ziemers.swxercise.lg.model.user.User; | 10 | import net.ziemers.swxercise.lg.model.user.User; |
| @@ -25,7 +24,7 @@ public class UserService { | @@ -25,7 +24,7 @@ public class UserService { | ||
| 25 | return dao.findAll(User.class); | 24 | return dao.findAll(User.class); |
| 26 | } | 25 | } |
| 27 | 26 | ||
| 28 | - public Long saveUser(final UserDto userDto) { | 27 | + public Long createUser(final UserDto userDto) { |
| 29 | final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); | 28 | final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword()); |
| 30 | 29 | ||
| 31 | // wir füllen das User-Objekt mit Method Chaining | 30 | // wir füllen das User-Objekt mit Method Chaining |
| @@ -35,4 +34,8 @@ public class UserService { | @@ -35,4 +34,8 @@ public class UserService { | ||
| 35 | return dao.save(user); | 34 | return dao.save(user); |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 37 | + public void deleteUser(final Long id) { | ||
| 38 | + dao.remove(User.class, id); | ||
| 39 | + } | ||
| 40 | + | ||
| 38 | } | 41 | } |
src/main/java/net/ziemers/swxercise/ui/UserViewController.java
| @@ -21,25 +21,25 @@ public class UserViewController { | @@ -21,25 +21,25 @@ public class UserViewController { | ||
| 21 | private UserService userService; | 21 | private UserService userService; |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| 24 | - * Schickt eine Hello-Nachricht zum Aufrufer zurück. | 24 | + * Liefert alle User-Objekte zurück. |
| 25 | * | 25 | * |
| 26 | - * @param name der Name, der in der Hallo-Nachricht angegeben wird | ||
| 27 | - * @return eine Hallo-Nachricht | 26 | + * @return die User-Objekte. |
| 28 | */ | 27 | */ |
| 29 | @GET | 28 | @GET |
| 30 | - @Path("/hello/{name}") | ||
| 31 | - @Produces(MediaType.TEXT_PLAIN) | ||
| 32 | - public String helloPath(@PathParam("name") String name) { | ||
| 33 | - return String.format("Hello %s", name); | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - @GET | ||
| 37 | - @Path("/all") | ||
| 38 | @Produces(MediaType.APPLICATION_JSON) | 29 | @Produces(MediaType.APPLICATION_JSON) |
| 39 | public Collection<User> getAllUsers() { | 30 | public Collection<User> getAllUsers() { |
| 40 | return userService.findAllUsers(); | 31 | return userService.findAllUsers(); |
| 41 | } | 32 | } |
| 42 | 33 | ||
| 34 | + /** | ||
| 35 | + * Liefert das User-Objekt mit der gewünschten Id zurück. | ||
| 36 | + * | ||
| 37 | + * Aufruf: | ||
| 38 | + * GET http://localhost:8080/swxercise/rest/users/42 | ||
| 39 | + * | ||
| 40 | + * @param id die Id des gewünschten User-Objekts | ||
| 41 | + * @return das User-Objekt. | ||
| 42 | + */ | ||
| 43 | @GET | 43 | @GET |
| 44 | @Path("{id}") | 44 | @Path("{id}") |
| 45 | @Produces(MediaType.APPLICATION_JSON) | 45 | @Produces(MediaType.APPLICATION_JSON) |
| @@ -47,12 +47,54 @@ public class UserViewController { | @@ -47,12 +47,54 @@ public class UserViewController { | ||
| 47 | return userService.findUser(id); | 47 | return userService.findUser(id); |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | + /** | ||
| 51 | + * Erstellt ein neues User-Objekt mit den gewünschten Eigenschaften, welche mittels {@link UserDto} definiert werden. | ||
| 52 | + * | ||
| 53 | + * Aufruf: | ||
| 54 | + * POST http://localhost:8080/swxercise/rest/users | ||
| 55 | + * | ||
| 56 | + * @param dto das mittels der als JSON-Objekt übergebenenen Eigenschaften zu füllende {@link UserDto} | ||
| 57 | + * @return "Ok", wenn die Erstellung des User-Objekts erfolgreich war. | ||
| 58 | + * @throws Exception wirft eine Exception, wenn das Erstellen des UserDto fehlschlug | ||
| 59 | + */ | ||
| 60 | + @POST | ||
| 61 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 62 | + @Produces({MediaType.TEXT_PLAIN}) | ||
| 63 | + public String createUser(UserDto dto) throws Exception { | ||
| 64 | + userService.createUser(dto); | ||
| 65 | + return "Ok"; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * Aktualisiert das User-Objekt mit der gewünschten Id mit den Eigenschaften, welche mittels {@link UserDto} definiert werden. | ||
| 70 | + * | ||
| 71 | + * Aufruf: | ||
| 72 | + * PUT http://localhost:8080/swxercise/rest/users/42 | ||
| 73 | + * | ||
| 74 | + * @param id die Id des gewünschten User-Objekts | ||
| 75 | + * @return "Ok", wenn die Erstellung des User-Objekts erfolgreich war. | ||
| 76 | + * @throws Exception wirft eine Exception, wenn das Erstellen des UserDto fehlschlug | ||
| 77 | + */ | ||
| 50 | @PUT | 78 | @PUT |
| 51 | - @Path("/user") | 79 | + @Path("{id}") |
| 52 | @Consumes(MediaType.APPLICATION_JSON) | 80 | @Consumes(MediaType.APPLICATION_JSON) |
| 53 | @Produces({MediaType.TEXT_PLAIN}) | 81 | @Produces({MediaType.TEXT_PLAIN}) |
| 54 | - public String postUser(UserDto userDto) throws Exception { | ||
| 55 | - userService.saveUser(userDto); | 82 | + public String updateUser(@PathParam("id") Long id) throws Exception { |
| 83 | + // TODO noch zu implementieren | ||
| 84 | + return "Ok"; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Löscht das User-Objekt mit der gewünschten Id. | ||
| 89 | + * | ||
| 90 | + * @param id die Id des gewünschten User-Objekts | ||
| 91 | + * @return "Ok", wenn die Erstellung des User-Objekts erfolgreich war. | ||
| 92 | + */ | ||
| 93 | + @DELETE | ||
| 94 | + @Path("{id}") | ||
| 95 | + @Produces({MediaType.TEXT_PLAIN}) | ||
| 96 | + public String deleteUser(@PathParam("id") Long id) { | ||
| 97 | + userService.deleteUser(id); | ||
| 56 | return "Ok"; | 98 | return "Ok"; |
| 57 | } | 99 | } |
| 58 | 100 |
src/test/java/net/ziemers/swxercise/ui/RestViewControllerTest.java renamed to src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
| @@ -7,19 +7,14 @@ import org.mockito.InjectMocks; | @@ -7,19 +7,14 @@ 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 RestViewControllerTest { | ||
| 11 | - | 10 | +public class UserViewControllerTest { |
| 11 | + | ||
| 12 | @InjectMocks | 12 | @InjectMocks |
| 13 | private UserViewController underTest; | 13 | private UserViewController underTest; |
| 14 | 14 | ||
| 15 | @Test | 15 | @Test |
| 16 | public void test() { | 16 | public void test() { |
| 17 | - final String name = "Tom"; | ||
| 18 | - final String expected = "Hello " + name; | ||
| 19 | - String actual; | ||
| 20 | - | ||
| 21 | - actual = underTest.helloPath(name); | ||
| 22 | - Assert.assertEquals(expected, actual); | 17 | + // TODO noch zu implementieren |
| 23 | } | 18 | } |
| 24 | 19 | ||
| 25 | } | 20 | } |