Skip to content

Commit

Permalink
Merge pull request #1 from mcespedeso/FPPSP-4
Browse files Browse the repository at this point in the history
Add crud endpoint user operations
  • Loading branch information
mcespedeso authored Aug 16, 2017
2 parents 76a95f2 + d977f3c commit 441d3de
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
<!-- OpenAPI Specification -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.16</version>
</dependency>
</dependencies>

<build>
Expand Down
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();
}

}
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> {

}
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> {

}
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);
}

}
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);
}
}

}
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);

}
Loading

0 comments on commit 441d3de

Please sign in to comment.