|
@@ -0,0 +1,95 @@
|
|
|
+package es.uv.garcosda.controllers;
|
|
|
+
|
|
|
+import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
|
|
+import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import es.uv.garcosda.domain.User;
|
|
|
+import es.uv.garcosda.services.UserService;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/v1/users")
|
|
|
+public class UserController {
|
|
|
+ private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
|
|
|
+
|
|
|
+ @Autowired private UserService us;
|
|
|
+
|
|
|
+ @GetMapping("")
|
|
|
+ public ResponseEntity<List<User>> findUsers(@RequestParam(value="firstname", required = false) Optional<String> firstname,
|
|
|
+ @RequestParam(value="lastname", required = false) Optional<String> lastname) {
|
|
|
+
|
|
|
+ List<User> users = new ArrayList<User>();
|
|
|
+
|
|
|
+ if(!firstname.isPresent() && !lastname.isPresent()) {
|
|
|
+ LOGGER.debug("Get all users");
|
|
|
+ users = us.findAll();
|
|
|
+ }
|
|
|
+ else if(firstname.isPresent()) {
|
|
|
+ LOGGER.debug("Get users by firstname");
|
|
|
+ users = us.findByFirstname(firstname.get());
|
|
|
+ }
|
|
|
+ else if(lastname.isPresent()) {
|
|
|
+ LOGGER.debug("Get users by lastname");
|
|
|
+ users = us.findByLastname(lastname.get());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (User u : users) {
|
|
|
+ u.add(linkTo(methodOn(UserController.class).findUserById(u.getId().toString())).withSelfRel());
|
|
|
+ }
|
|
|
+
|
|
|
+ return new ResponseEntity<List<User>>(users, HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("{id}")
|
|
|
+ public ResponseEntity<User> findUserById(@PathVariable("id") String id) {
|
|
|
+ LOGGER.debug("Get user id: "+id);
|
|
|
+ Optional<User> user = us.findById(id);
|
|
|
+
|
|
|
+ if(!user.isEmpty()) {
|
|
|
+ return new ResponseEntity<User>(new User(), HttpStatus.OK);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ User v = user.get();
|
|
|
+ v.add(linkTo(methodOn(UserController.class).findUserById(id)).withSelfRel());
|
|
|
+ return new ResponseEntity<User>(user.get(), HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("")
|
|
|
+ public ResponseEntity<User> createUser(@RequestBody User user) {
|
|
|
+ LOGGER.debug("Create user");
|
|
|
+ User createdUser = us.create(user);
|
|
|
+
|
|
|
+ if(createdUser.getId() != null) {
|
|
|
+ createdUser.add(linkTo(methodOn(UserController.class).findUserById(createdUser.getId().toString())).withSelfRel());
|
|
|
+ return new ResponseEntity<User>(createdUser, HttpStatus.OK);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return new ResponseEntity<User>(new User(), HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("{id}")
|
|
|
+ public ResponseEntity<String> deleteUserById(@PathVariable("id") String id) {
|
|
|
+ LOGGER.debug("Delete user id: "+id);
|
|
|
+ us.deleteById(id);
|
|
|
+ return new ResponseEntity<String>("", HttpStatus.OK);
|
|
|
+ }
|
|
|
+}
|