forked from rodrigojv/FP-PSP-SERVER
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mcespedeso/FPPSP-4
Add crud endpoint user operations
- Loading branch information
Showing
8 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/py/org/fundacionparaguaya/pspserver/security/user/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package py.org.fundacionparaguaya.pspserver.security.user.domain; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.SequenceGenerator; | ||
import javax.persistence.Table; | ||
|
||
/** | ||
* User DAO Layer | ||
* | ||
* <p> | ||
* This class represents the user mapped with the database table | ||
* <p> | ||
* | ||
* @author Marcos Cespedes | ||
* @since 2017-08-14 | ||
* @version 1.0 | ||
* | ||
*/ | ||
@Entity | ||
@Table(name = "user", schema = "security") | ||
public class User implements java.io.Serializable { | ||
|
||
private static final long serialVersionUID = -7693678673241220633L; | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="security.user_user_id_seq") | ||
@SequenceGenerator(name="security.user_user_id_seq", sequenceName="security.user_user_id_seq", allocationSize=1) | ||
@Column(name = "user_id") | ||
private Long userId; | ||
|
||
private String username; | ||
|
||
// @JsonIgnore | ||
private String pass; | ||
|
||
private boolean active; | ||
|
||
public User() { | ||
// do nothing | ||
} | ||
|
||
public User(Long userId, String username, String pass, boolean active) { | ||
this.userId = userId; | ||
this.username = username; | ||
this.pass = pass; | ||
this.active = active; | ||
} | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(Long userId) { | ||
this.userId = userId; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPass() { | ||
return pass; | ||
} | ||
|
||
public void setPass(String pass) { | ||
this.pass = pass; | ||
} | ||
|
||
public boolean isActive() { | ||
return active; | ||
} | ||
|
||
public void setActive(boolean active) { | ||
this.active = active; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User [userId=" + userId + ", username=" + username + ", pass=" + pass + ", active=" + active + "]"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (userId == null || obj == null || getClass() != obj.getClass()) | ||
return false; | ||
User toCompare = (User) obj; | ||
return userId.equals(toCompare.userId); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return userId == null ? 0 : userId.hashCode(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ain/java/py/org/fundacionparaguaya/pspserver/security/user/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package py.org.fundacionparaguaya.pspserver.security.user.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import py.org.fundacionparaguaya.pspserver.security.user.domain.User; | ||
|
||
import java.lang.Long; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/py/org/fundacionparaguaya/pspserver/security/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package py.org.fundacionparaguaya.pspserver.security.user.service; | ||
|
||
import py.org.fundacionparaguaya.pspserver.security.user.domain.User; | ||
import py.org.fundacionparaguaya.pspserver.util.service.CRUDService; | ||
|
||
public interface UserService extends CRUDService<User> { | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/py/org/fundacionparaguaya/pspserver/security/user/service/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package py.org.fundacionparaguaya.pspserver.security.user.service; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import javax.persistence.NoResultException; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import py.org.fundacionparaguaya.pspserver.security.user.domain.User; | ||
import py.org.fundacionparaguaya.pspserver.security.user.repository.UserRepository; | ||
|
||
/** | ||
* | ||
* <p> | ||
* Implementation of user services | ||
* <p> | ||
* | ||
* @author Marcos Cespedes | ||
* @since 2017-08-14 | ||
* @version 1.0 | ||
*/ | ||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
protected static Logger logger = Logger.getLogger(UserServiceImpl.class); | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Override | ||
public User save(User entity) { | ||
return userRepository.save(entity); | ||
} | ||
|
||
@Override | ||
public User getById(Serializable id) { | ||
return userRepository.findOne((Long) id); | ||
} | ||
|
||
@Override | ||
public List<User> getAll() { | ||
return userRepository.findAll(); | ||
} | ||
|
||
@Override | ||
public void delete(Serializable id) { | ||
userRepository.delete((Long) id); | ||
} | ||
|
||
} |
128 changes: 128 additions & 0 deletions
128
...java/py/org/fundacionparaguaya/pspserver/security/user/web/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package py.org.fundacionparaguaya.pspserver.security.user.web.controller; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import py.org.fundacionparaguaya.pspserver.security.user.domain.User; | ||
import py.org.fundacionparaguaya.pspserver.security.user.service.UserService; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* <h1>User RestController</h1> | ||
* <p> | ||
* The UserController program implements an application that simply CRUD | ||
* operation on RESTApi | ||
* | ||
* <b>Note:</b> Endpoints are defined for POST, PUT, GET, DELETE | ||
* </p> | ||
* | ||
* @author Marcos Cespedes | ||
* @version 1.0 | ||
* @since 2017-08-14 | ||
*/ | ||
@ControllerAdvice | ||
@RestController | ||
@RequestMapping(value = "/api/v1/users") | ||
@Api(value = "api/v1/users", description = "User controller class", consumes = "application/json") | ||
public class UserController { | ||
|
||
|
||
protected static Logger logger = Logger.getLogger(UserController.class); | ||
|
||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
|
||
|
||
@RequestMapping(value="/", method = RequestMethod.POST) | ||
@ApiOperation(value = "Create User", | ||
notes = "This operation allows to receive the data of the user and create a new resource in the server") | ||
public ResponseEntity<User> addUser(@RequestBody User user) { | ||
userService.save(user); | ||
logger.debug("Added:: " + user); | ||
return new ResponseEntity<User>(user, HttpStatus.CREATED); | ||
} | ||
|
||
|
||
|
||
@RequestMapping(method = RequestMethod.PUT) | ||
@ApiOperation(value = "Update user resource", | ||
notes = "This operation update a user") | ||
public ResponseEntity<Void> updateUser(@RequestBody User user) { | ||
User existingUser = userService.getById(user.getUserId()); | ||
if (existingUser == null) { | ||
logger.debug("User with id " + user.getUserId() + " does not exists"); | ||
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); | ||
} else { | ||
userService.save(user); | ||
logger.debug("Updated:: " + user); | ||
return new ResponseEntity<Void>(HttpStatus.OK); | ||
} | ||
} | ||
|
||
|
||
|
||
@RequestMapping(value = "/{userid}", method = RequestMethod.GET) | ||
@ApiOperation(value = "Find user by ID", | ||
notes = "This operation response user by identification", | ||
response = User.class, responseContainer = "User") | ||
public ResponseEntity<User> getUser(@PathVariable("userid") Long userid) { | ||
User user = userService.getById(userid); | ||
if (user == null) { | ||
logger.debug("User with id " + userid + " does not exists"); | ||
return new ResponseEntity<User>(HttpStatus.NOT_FOUND); | ||
} | ||
logger.debug("Found User:: " + user); | ||
return new ResponseEntity<User>(user, HttpStatus.OK); | ||
} | ||
|
||
|
||
|
||
@RequestMapping(method = RequestMethod.GET) | ||
@ApiOperation(value = "Find all users", | ||
notes = "This operation response all users", | ||
response = List.class, | ||
responseContainer = "List") | ||
public ResponseEntity<List<User>> getAllUsers() { | ||
List<User> users = userService.getAll(); | ||
if (users.isEmpty()) { | ||
logger.debug("Users does not exists"); | ||
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT); | ||
} | ||
logger.debug("Found " + users.size() + " Users"); | ||
logger.debug(users); | ||
logger.debug(Arrays.toString(users.toArray())); | ||
return new ResponseEntity<List<User>>(users, HttpStatus.OK); | ||
} | ||
|
||
|
||
|
||
@RequestMapping(value = "/{userid}", method = RequestMethod.DELETE) | ||
@ApiOperation(value = "Delete User resource", | ||
notes = "This operation delete the user") | ||
public ResponseEntity<Void> deleteUser(@PathVariable("userid") Long userid) { | ||
User user = userService.getById(userid); | ||
if (user == null) { | ||
logger.debug("User with id " + userid + " does not exists"); | ||
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); | ||
} else { | ||
userService.delete(userid); | ||
logger.debug("User with id " + userid + " deleted"); | ||
return new ResponseEntity<Void>(HttpStatus.GONE); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/py/org/fundacionparaguaya/pspserver/util/service/CRUDService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package py.org.fundacionparaguaya.pspserver.util.service; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
public interface CRUDService<E> { | ||
|
||
E save(E entity); | ||
|
||
E getById(Serializable id); | ||
|
||
List<E> getAll(); | ||
|
||
void delete(Serializable id); | ||
|
||
} |
Oops, something went wrong.