Commit f18d339fb494206fabf4fd0becfa8f17d46a3cd2

Authored by Thomas Ziemer
1 parent 12fe59cc

feature: ordentlichen RestResponse eingeführt (WIP)

src/main/java/net/ziemers/swxercise/ui/RestResponse.java 0 → 100644
  1 +package net.ziemers.swxercise.ui;
  2 +
  3 +import net.ziemers.swxercise.ui.enums.ResponseState;
  4 +import net.ziemers.swxercise.ui.enums.ResponseState.SUCCESS;
  5 +
  6 +public class RestResponse {
  7 +
  8 + private ResponseState responseState;
  9 +
  10 + public RestResponse() {
  11 + this(ResponseState.SUCCESS);
  12 + }
  13 +
  14 + public RestResponse(final ResponseState responseState) {
  15 + this.responseState = responseState;
  16 + }
  17 +
  18 + public int getResponseCode() {
  19 + return responseState.getResponseCode();
  20 + }
  21 +
  22 + public String getResponseText() {
  23 + return responseState.getResponseText();
  24 + }
  25 +
  26 +}
  27 +
... ...
src/main/java/net/ziemers/swxercise/ui/UserViewController.java
... ... @@ -11,6 +11,9 @@ import net.ziemers.swxercise.lg.model.user.User;
11 11 import net.ziemers.swxercise.lg.user.dto.UserDto;
12 12 import net.ziemers.swxercise.lg.user.service.UserService;
13 13  
  14 +import net.ziemers.swxercise.ui.RestResponse;
  15 +import net.ziemers.swxercise.ui.enums.ResponseState.FAILED;
  16 +
14 17 /**
15 18 * REST-Methoden für die Benutzerverwaltung.
16 19 */
... ... @@ -26,6 +29,9 @@ public class UserViewController {
26 29 /**
27 30 * Liefert alle User-Objekte zurück.
28 31 *
  32 + * Aufruf:
  33 + * GET http://localhost:8080/swxercise/rest/users
  34 + *
29 35 * @return die User-Objekte.
30 36 */
31 37 @GET
... ... @@ -62,10 +68,10 @@ public class UserViewController {
62 68 */
63 69 @POST
64 70 @Consumes(MediaType.APPLICATION_JSON)
65   - @Produces(MediaType.TEXT_PLAIN)
66   - public String createUser(UserDto dto) throws Exception {
  71 + @Produces(MediaType.APPLICATION_JSON)
  72 + public RestResponse createUser(UserDto dto) throws Exception {
67 73 final Long id = userService.createUser(dto);
68   - return String.format("Ok (#%d)", id);
  74 + return new RestResponse();
69 75 }
70 76  
71 77 /**
... ... @@ -81,10 +87,10 @@ public class UserViewController {
81 87 @PUT
82 88 @Path("{id}")
83 89 @Consumes(MediaType.APPLICATION_JSON)
84   - @Produces(MediaType.TEXT_PLAIN)
85   - public String updateUser(@PathParam("id") Long id) throws Exception {
  90 + @Produces(MediaType.APPLICATION_JSON)
  91 + public RestResponse updateUser(@PathParam("id") Long id) throws Exception {
86 92 // TODO noch zu implementieren
87   - return "Ok";
  93 + return new RestResponse(ResponseState.FAILED);
88 94 }
89 95  
90 96 /**
... ... @@ -98,10 +104,10 @@ public class UserViewController {
98 104 */
99 105 @DELETE
100 106 @Path("{id}")
101   - @Produces(MediaType.TEXT_PLAIN)
102   - public String deleteUser(@PathParam("id") Long id) {
  107 + @Produces(MediaType.APPLICATION_JSON)
  108 + public RestResponse deleteUser(@PathParam("id") Long id) {
103 109 userService.deleteUser(id);
104   - return "Ok";
  110 + return new RestResponse();
105 111 }
106 112  
107 113 /**
... ... @@ -113,12 +119,12 @@ public class UserViewController {
113 119 * @return "Ok", wenn das Löschen des User-Objekts erfolgreich war.
114 120 */
115 121 @DELETE
116   - @Produces(MediaType.TEXT_PLAIN)
117   - public String deleteUser() {
  122 + @Produces(MediaType.APPLICATION_JSON)
  123 + public RestResponse deleteUser() {
118 124 if (userService.deleteUser()) {
119   - return "Ok";
  125 + return new RestResponse();
120 126 }
121   - return "Failed";
  127 + return new RestResponse(ResponseState.FAILED);
122 128 }
123 129  
124 130 /**
... ... @@ -133,13 +139,12 @@ public class UserViewController {
133 139 @POST
134 140 @Path("/login")
135 141 @Consumes(MediaType.APPLICATION_JSON)
136   - @Produces(MediaType.TEXT_PLAIN)
137   - public String loginUser(UserDto dto) {
  142 + @Produces(MediaType.APPLICATION_JSON)
  143 + public RestResponse loginUser(UserDto dto) {
138 144 if (userService.loginUser(dto)) {
139   - final User user = userService.getSessionUser();
140   - return String.format("Ok (%s %s)", user.getFirstname(), user.getLastname());
  145 + return new RestResponse();
141 146 }
142   - return "Failed";
  147 + return new RestResponse(ResponseState.FAILED);
143 148 }
144 149  
145 150 /**
... ... @@ -153,12 +158,12 @@ public class UserViewController {
153 158 @POST
154 159 @Path("/logout")
155 160 @Consumes(MediaType.APPLICATION_JSON)
156   - @Produces(MediaType.TEXT_PLAIN)
157   - public String logoutUser() {
  161 + @Produces(MediaType.APPLICATION_JSON)
  162 + public RestResponse logoutUser() {
158 163 if (userService.logoutUser()) {
159   - return "Ok";
  164 + return new RestResponse();
160 165 }
161   - return "Failed";
  166 + return new RestResponse(ResponseState.FAILED);
162 167 }
163 168  
164 169 }
... ...
src/main/java/net/ziemers/swxercise/ui/enums/ResponseState.java 0 → 100644
  1 +package net.ziemers.swxercise.ui.enums;
  2 +
  3 +public enum ResponseState {
  4 +
  5 + SUCCESS(0, "Success"),
  6 + FAILED(1, "Failed"),
  7 + ;
  8 +
  9 + private final int responseCode;
  10 +
  11 + private final String responseText;
  12 +
  13 + ResponseState(final int responseCode, final String responseText) {
  14 + this.responseCode = responseCode;
  15 + this.responseText = responseText;
  16 + }
  17 +
  18 + public int getResponseCode() {
  19 + return responseCode;
  20 + }
  21 +
  22 + public String getResponseText() {
  23 + return responseText;
  24 + }
  25 +
  26 +}
  27 +
... ...