Facebook
From Sludgy Peccary, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 213
  1. //UserService
  2. package boundary;
  3.  
  4. import entity.User;
  5.  
  6. import java.util.LinkedList;
  7. import java.util.List;
  8.  
  9. public class UserService {
  10.  
  11.     private List<User> users;
  12.  
  13.     public UserService()
  14.     {
  15.         users=new LinkedList<User>();
  16.     }
  17.  
  18.     public List<User> getUsers()
  19.     {
  20.         return users;
  21.     }
  22.     public User getUserByIndex(int userId)
  23.     {
  24.         for(User user : users)
  25.         {
  26.             if(user.getId() == userId)
  27.                 return user;
  28.         }
  29.         return null;
  30.     }
  31.     public boolean deleteUserById(int userId)
  32.     {
  33.  
  34.         int userIndex = -1;
  35.  
  36.         for(User user : users)
  37.         {
  38.             if(user.getId() == userId)
  39.             {
  40.                 userIndex = users.indexOf(user);
  41.                 break;
  42.             }
  43.         }
  44.  
  45.         if(userIndex > -1)
  46.         {
  47.             users.remove(userIndex);
  48.         }
  49.         else
  50.         {
  51.             return false;
  52.         }
  53.         return true;
  54.     }
  55.  
  56.     public void addUser(User user)
  57.     {
  58.         users.add(user);
  59.  
  60.     }
  61. }
  62.  
  63.  
  64. //UserController
  65.  
  66. package control;
  67.  
  68. import boundary.UserService;
  69. import entity.User;
  70.  
  71.  
  72.  
  73. import javax.ws.rs.*;
  74. import javax.ws.rs.core.MediaType;
  75. import java.util.List;
  76.  
  77. @Path("/registration")
  78. @ApplicationPath("rest")
  79. //http://localhost:18080/rest/registration/user
  80. public class UserController {
  81.  
  82.  
  83.     private UserService userService;
  84.  
  85.     @GET
  86.     @Path("/user/{index}")
  87.     @Produces({MediaType.APPLICATION_JSON})
  88.     public User getObject(@PathParam("index") Integer userId) {
  89.         return userService.getUserByIndex(userId);
  90.     }
  91.  
  92.     @GET
  93.     @Path("/user")
  94.     @Produces({MediaType.APPLICATION_JSON})
  95.     public List<User> getAll() {
  96.         return userService.getUsers();
  97.     }
  98.  
  99.     @DELETE
  100.     @Path("/user/{index}")
  101.     @Produces({MediaType.APPLICATION_JSON})
  102.     public boolean deleteObject(@PathParam("index") Integer userId) {
  103.         return userService.deleteUserById(userId);
  104.     }
  105.  
  106.     @POST
  107.     @Path("/user/{index}")
  108.     @Produces({MediaType.APPLICATION_JSON})
  109.     public User createObject(@PathParam("index") Integer userId){
  110.          User user=new User.Builder()
  111.                 .id(userId)
  112.                 .firstName("Jan")
  113.                 .lastName("Kowalski")
  114.                 .login("jkowal")
  115.                 .bornDate("22.06.1996")
  116.                 .password("haslo")
  117.                 .email("[email protected]")
  118.                 .build();
  119.          userService.addUser(user);
  120.          return user;
  121.     }
  122.  
  123.     @PUT
  124.     @Path("/user/{index}")
  125.     @Produces({MediaType.APPLICATION_JSON})
  126.     public User updateObject(@PathParam("index") Integer userId)
  127.     {
  128.         userService.getUserByIndex(userId).setEmail("[email protected]");
  129.         userService.getUserByIndex(userId).setPassword("sssss");
  130.         return userService.getUserByIndex(userId);
  131.     }
  132.  
  133.  
  134. }
  135.  
  136. //User
  137. package entity;
  138.  
  139.  
  140.  
  141. public class User {
  142.     private Integer id;
  143.     private String firstName;
  144.     private String lastName;
  145.     private String login;
  146.     private String email;
  147.     private String bornDate;
  148.     private String password;
  149.  
  150.     public Integer getId() {
  151.         return id;
  152.     }
  153.  
  154.     public void setId(Integer id) {
  155.         this.id = id;
  156.     }
  157.  
  158.     public String getFirstName() {
  159.         return firstName;
  160.     }
  161.  
  162.     public void setFirstName(String firstName) {
  163.         this.firstName = firstName;
  164.     }
  165.  
  166.     public String getLastName() {
  167.         return lastName;
  168.     }
  169.  
  170.     public void setLastName(String lastName) {
  171.         this.lastName = lastName;
  172.     }
  173.  
  174.     public String getLogin() {
  175.         return login;
  176.     }
  177.  
  178.     public void setLogin(String login) {
  179.         this.login = login;
  180.     }
  181.  
  182.     public String getEmail() {
  183.         return email;
  184.     }
  185.  
  186.     public void setEmail(String email) {
  187.         this.email = email;
  188.     }
  189.  
  190.     public String getBornDate() {
  191.         return bornDate;
  192.     }
  193.  
  194.     public void setBornDate(String bornDate) {
  195.         this.bornDate = bornDate;
  196.     }
  197.  
  198.     public String getPassword() {
  199.         return password;
  200.     }
  201.  
  202.     public void setPassword(String password) {
  203.         this.password = password;
  204.     }
  205.  
  206.     public static final class Builder {
  207.         private Integer id;
  208.         private String firstName;
  209.         private String lastName;
  210.         private String login;
  211.         private String email;
  212.         private String bornDate;
  213.         private String password;
  214.  
  215.         public Builder id(Integer id) {
  216.             this.id = id;
  217.             return this;
  218.         }
  219.  
  220.         public Builder firstName(String firstName) {
  221.             this.firstName = firstName;
  222.             return this;
  223.         }
  224.  
  225.         public Builder login(String login) {
  226.             this.login = login;
  227.             return this;
  228.         }
  229.  
  230.         public Builder lastName(String lastName) {
  231.             this.lastName = lastName;
  232.             return this;
  233.         }
  234.  
  235.         public Builder email(String email) {
  236.             this.email = email;
  237.             return this;
  238.         }
  239.  
  240.         public Builder bornDate(String bornDate) {
  241.             this.bornDate = bornDate;
  242.             return this;
  243.         }
  244.  
  245.         public Builder password(String password) {
  246.             this.password = password;
  247.             return this;
  248.         }
  249.  
  250.         public User build() {
  251.             if (email.isEmpty()) {
  252.                 throw new IllegalStateException("E-mail cannot be empty");
  253.             }
  254.             if (password.isEmpty()) {
  255.                 throw new IllegalStateException("Password cannot be empty");
  256.             }
  257.  
  258.             User user = new User();
  259.             user.id = this.id;
  260.             user.firstName = this.firstName;
  261.             user.lastName = this.lastName;
  262.             user.login = this.login;
  263.             user.bornDate = this.bornDate;
  264.             user.password = this.password;
  265.             user.email=this.email;
  266.             return user;
  267.         }
  268.     }
  269. }
  270.