Przeglądaj źródła

Users model, repo, service and endpoint

dagarcos 2 lat temu
rodzic
commit
a642797c5d

+ 95 - 0
src/main/java/es/uv/garcosda/controllers/UserController.java

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

+ 77 - 0
src/main/java/es/uv/garcosda/domain/User.java

@@ -0,0 +1,77 @@
+package es.uv.garcosda.domain;
+
+import java.io.Serializable;
+import java.util.UUID;
+
+import org.hibernate.annotations.GenericGenerator;
+import org.springframework.hateoas.RepresentationModel;
+
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+import jakarta.persistence.Table;
+
+@Entity
+@Table(name = "users")
+public class User extends RepresentationModel<Video> implements Serializable {
+
+	
+private static final long serialVersionUID = 1L;
+	
+	@Id
+	@GeneratedValue(generator = "UUID", strategy = GenerationType.AUTO)
+	@GenericGenerator(
+            name = "UUID",
+            strategy = "org.hibernate.id.UUIDGenerator"
+    )
+	@Column(name="id", updatable = false, nullable = false)
+	private UUID id;
+	
+	@Column(name = "firstname")
+	private String firstname;
+	
+	@Column(name = "lastname")
+	private String lastname;
+
+	public User() {}
+	
+	public User(UUID id, String firstname, String lastname) {
+		super();
+		this.id = id;
+		this.firstname = firstname;
+		this.lastname = lastname;
+	}
+
+	public UUID getId() {
+		return id;
+	}
+
+	public void setId(UUID id) {
+		this.id = id;
+	}
+
+	public String getFirstname() {
+		return firstname;
+	}
+
+	public void setFirstname(String firstname) {
+		this.firstname = firstname;
+	}
+
+	public String getLastname() {
+		return lastname;
+	}
+
+	public void setLastname(String lastname) {
+		this.lastname = lastname;
+	}
+
+	public static long getSerialversionuid() {
+		return serialVersionUID;
+	}
+	
+	
+	
+}

+ 15 - 0
src/main/java/es/uv/garcosda/repositories/UserRepository.java

@@ -0,0 +1,15 @@
+package es.uv.garcosda.repositories;
+
+import java.util.List;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import es.uv.garcosda.domain.User;
+
+public interface UserRepository extends JpaRepository<User, String> {
+
+	public List<User> findByFirstname(String firstname);
+	public List<User> findByLastname(String lastname);
+
+
+}

+ 47 - 0
src/main/java/es/uv/garcosda/services/UserService.java

@@ -0,0 +1,47 @@
+package es.uv.garcosda.services;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import es.uv.garcosda.domain.User;
+import es.uv.garcosda.repositories.UserRepository;
+
+@Service
+@Transactional
+public class UserService {
+
+	@Autowired UserRepository ur;
+
+	public List<User> findAll(){
+		return ur.findAll();
+	}
+
+	public Optional<User> findById(String id) {
+		return ur.findById(id);
+	}
+	
+	public List<User> findByFirstname(String firstname){
+		return ur.findByFirstname(firstname);
+	}
+	
+	public List<User> findByLastname(String lastname){
+		return ur.findByLastname(lastname);
+	}
+	
+	public User update(User user) {
+		return ur.save(user);
+	}
+	
+	public User create(User user) {
+		return ur.save(user);
+	}
+
+	public void deleteById(String id) {
+		ur.deleteById(id);
+	}
+	
+}