瀏覽代碼

Comments and Hateoas cross reference to Users and Videos

Daniel Garcia Costa 2 年之前
父節點
當前提交
0b5cc895cf

+ 103 - 0
src/main/java/es/uv/garcosda/controllers/CommentController.java

@@ -0,0 +1,103 @@
+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.Comment;
+import es.uv.garcosda.services.CommentService;
+
+@RestController
+@RequestMapping("/api/v1/comments")
+public class CommentController {
+
+	private final static Logger LOGGER = LoggerFactory.getLogger(CommentController.class);
+	
+	@Autowired private CommentService cs;
+	
+	@GetMapping("")
+	public ResponseEntity<List<Comment>> findComments(@RequestParam(value="videoId", required = false) Optional<String> videoId, 
+			@RequestParam(value="userId", required = false) Optional<String> userId) {
+		
+		List<Comment> comments = new ArrayList<Comment>();
+		
+		if(!videoId.isPresent() && !userId.isPresent()) {
+			LOGGER.debug("Get all comments");
+			comments = cs.findAll();
+		}
+		else if(videoId.isPresent()) {
+			LOGGER.debug("Get comments by video id");
+			comments = cs.findByVideoId(videoId.get());
+		}
+		else if(userId.isPresent()) {
+			LOGGER.debug("Get comments by user id");
+			comments = cs.findByUserId(userId.get());
+		}
+		
+		for (Comment c : comments) {
+			c.add(linkTo(methodOn(CommentController.class).findCommentById(c.getId())).withSelfRel());
+			c.add(linkTo(methodOn(VideoController.class).findVideoById(c.getVideoId())).withRel("video"));
+			c.add(linkTo(methodOn(UserController.class).findUserById(c.getUserId())).withRel("user"));
+		}
+		
+		return new ResponseEntity<List<Comment>>(comments, HttpStatus.OK);
+	}
+	
+	@GetMapping("{id}")
+	public ResponseEntity<Comment> findCommentById(@PathVariable("id") String id) {
+		LOGGER.debug("Get comment id: "+id);
+		Optional<Comment> comment = cs.findById(id);
+		if(!comment.isEmpty()) {
+			Comment c = comment.get();
+			c.add(linkTo(methodOn(UserController.class).findUserById(id)).withSelfRel());
+			c.add(linkTo(methodOn(VideoController.class).findVideoById(c.getVideoId())).withRel("video"));
+			c.add(linkTo(methodOn(UserController.class).findUserById(c.getUserId())).withRel("user"));
+			return new ResponseEntity<Comment>(c, HttpStatus.OK);
+			
+		}
+		else {
+			return new ResponseEntity<Comment>(new Comment(), HttpStatus.NOT_FOUND);
+		}
+	}
+	
+	@PostMapping("")
+	public ResponseEntity<Comment> createComment(@RequestBody Comment user) {
+		LOGGER.debug("Create comment");
+		Comment createdComment = cs.create(user);
+		
+		if(createdComment.getId() != null) {
+			createdComment.add(linkTo(methodOn(UserController.class).findUserById(createdComment.getId())).withSelfRel());
+			createdComment.add(linkTo(methodOn(VideoController.class).findVideoById(createdComment.getVideoId())).withRel("video"));
+			createdComment.add(linkTo(methodOn(UserController.class).findUserById(createdComment.getUserId())).withRel("user"));
+			return new ResponseEntity<Comment>(createdComment, HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<Comment>(new Comment(), HttpStatus.BAD_REQUEST);
+		}
+	}
+			
+	@DeleteMapping("{id}")
+	public ResponseEntity<String> deleteCommentById(@PathVariable("id") String id) {
+		LOGGER.debug("Delete comment id: "+id);
+		cs.deleteById(id);
+		return new ResponseEntity<String>("", HttpStatus.OK);
+	}
+	
+}

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

@@ -64,7 +64,7 @@ public class UserController {
 		if(!user.isEmpty()) {
 			User v = user.get();
 			v.add(linkTo(methodOn(UserController.class).findUserById(id)).withSelfRel());
-			return new ResponseEntity<User>(user.get(), HttpStatus.OK);
+			return new ResponseEntity<User>(v, HttpStatus.OK);
 			
 		}
 		else {

+ 13 - 0
src/main/java/es/uv/garcosda/repositories/CommentRepository.java

@@ -0,0 +1,13 @@
+package es.uv.garcosda.repositories;
+
+import java.util.List;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import es.uv.garcosda.domain.Comment;
+
+public interface CommentRepository extends JpaRepository<Comment, String> {
+
+	public List<Comment> findByVideoId(String videoId);
+	public List<Comment> findByUserId(String UserId);
+}

+ 46 - 0
src/main/java/es/uv/garcosda/services/CommentService.java

@@ -0,0 +1,46 @@
+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.Comment;
+import es.uv.garcosda.repositories.CommentRepository;
+
+@Service
+@Transactional
+public class CommentService {
+
+	@Autowired CommentRepository cr;
+
+	public List<Comment> findAll(){
+		return cr.findAll();
+	}
+
+	public Optional<Comment> findById(String id) {
+		return cr.findById(id);
+	}
+	
+	public List<Comment> findByVideoId(String firstname){
+		return cr.findByVideoId(firstname);
+	}
+	
+	public List<Comment> findByUserId(String lastname){
+		return cr.findByUserId(lastname);
+	}
+	
+	public Comment update(Comment comment) {
+		return cr.save(comment);
+	}
+	
+	public Comment create(Comment comment) {
+		return cr.save(comment);
+	}
+
+	public void deleteById(String id) {
+		cr.deleteById(id);
+	}
+}