1111public class SparkRestExample {
1212 public static void main (String [] args ) {
1313 final UserService userService = new UserServiceMapImpl ();
14-
14+
1515 post ("/users" , (request , response ) -> {
1616 response .type ("application/json" );
17-
17+
1818 User user = new Gson ().fromJson (request .body (), User .class );
1919 userService .addUser (user );
2020
@@ -23,52 +23,40 @@ public static void main(String[] args) {
2323
2424 get ("/users" , (request , response ) -> {
2525 response .type ("application/json" );
26-
27- return new Gson ().toJson (
28- new StandardResponse (StatusResponse .SUCCESS ,new Gson ()
29- .toJsonTree (userService .getUsers ())));
26+
27+ return new Gson ().toJson (new StandardResponse (StatusResponse .SUCCESS , new Gson ().toJsonTree (userService .getUsers ())));
3028 });
3129
3230 get ("/users/:id" , (request , response ) -> {
3331 response .type ("application/json" );
34-
35- return new Gson ().toJson (
36- new StandardResponse (StatusResponse .SUCCESS ,new Gson ()
37- .toJsonTree (userService .getUser (request .params (":id" )))));
32+
33+ return new Gson ().toJson (new StandardResponse (StatusResponse .SUCCESS , new Gson ().toJsonTree (userService .getUser (request .params (":id" )))));
3834 });
3935
4036 put ("/users/:id" , (request , response ) -> {
4137 response .type ("application/json" );
42-
38+
4339 User toEdit = new Gson ().fromJson (request .body (), User .class );
4440 User editedUser = userService .editUser (toEdit );
45-
41+
4642 if (editedUser != null ) {
47- return new Gson ().toJson (
48- new StandardResponse (StatusResponse .SUCCESS ,new Gson ()
49- .toJsonTree (editedUser )));
50- }else {
51- return new Gson ().toJson (
52- new StandardResponse (StatusResponse .ERROR ,new Gson ()
53- .toJson ("User not found or error in edit" )));
43+ return new Gson ().toJson (new StandardResponse (StatusResponse .SUCCESS , new Gson ().toJsonTree (editedUser )));
44+ } else {
45+ return new Gson ().toJson (new StandardResponse (StatusResponse .ERROR , new Gson ().toJson ("User not found or error in edit" )));
5446 }
5547 });
5648
5749 delete ("/users/:id" , (request , response ) -> {
5850 response .type ("application/json" );
59-
51+
6052 userService .deleteUser (request .params (":id" ));
61- return new Gson ().toJson (
62- new StandardResponse (StatusResponse .SUCCESS , "user deleted" ));
53+ return new Gson ().toJson (new StandardResponse (StatusResponse .SUCCESS , "user deleted" ));
6354 });
6455
6556 options ("/users/:id" , (request , response ) -> {
6657 response .type ("application/json" );
67-
68- return new Gson ().toJson (
69- new StandardResponse (StatusResponse .SUCCESS ,
70- (userService .userExist (
71- request .params (":id" ))) ? "User exists" : "User does not exists" ));
58+
59+ return new Gson ().toJson (new StandardResponse (StatusResponse .SUCCESS , (userService .userExist (request .params (":id" ))) ? "User exists" : "User does not exists" ));
7260 });
7361
7462 }
0 commit comments