VideoController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package es.uv.garcosda.controllers;
  2. import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
  3. import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Optional;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.bind.annotation.DeleteMapping;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import es.uv.garcosda.domain.Video;
  21. import es.uv.garcosda.services.VideoService;
  22. @RestController
  23. @RequestMapping("/api/v1/videos")
  24. public class VideoController {
  25. private final static Logger LOGGER = LoggerFactory.getLogger(VideoController.class);
  26. @Autowired private VideoService vs;
  27. @GetMapping("")
  28. public ResponseEntity<List<Video>> findVideos(@RequestParam(value="title", required = false) Optional<String> title,
  29. @RequestParam(value="user", required = false) Optional<String> userId) {
  30. List<Video> videos = new ArrayList<Video>();
  31. if(!title.isPresent() && !userId.isPresent()) {
  32. LOGGER.debug("Get all videos");
  33. videos = vs.findAll();
  34. }
  35. else if(title.isPresent()) {
  36. LOGGER.debug("Get videos by title");
  37. videos = vs.findByTitle(title.get());
  38. }
  39. else if(userId.isPresent()) {
  40. LOGGER.debug("Get videos by user");
  41. videos = vs.findByUserId(userId.get());
  42. }
  43. for (Video v : videos) {
  44. v.add(linkTo(methodOn(VideoController.class).findVideos(null, null)).withRel("collection"));
  45. v.add(linkTo(methodOn(VideoController.class).findVideoById(v.getId().toString())).withSelfRel());
  46. v.add(linkTo(methodOn(UserController.class).findUserById(v.getUserId().toString())).withRel("user"));
  47. }
  48. return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
  49. }
  50. @GetMapping("{id}")
  51. public ResponseEntity<Video> findVideoById(@PathVariable("id") String id) {
  52. LOGGER.debug("Get video id: "+id);
  53. Optional<Video> video = vs.findById(id);
  54. if(!video.isEmpty()) {
  55. Video v = video.get();
  56. v.add(linkTo(methodOn(VideoController.class).findVideoById(id)).withSelfRel());
  57. v.add(linkTo(methodOn(UserController.class).findUserById(v.getUserId().toString())).withRel("user"));
  58. return new ResponseEntity<Video>(video.get(), HttpStatus.OK);
  59. }
  60. else {
  61. return new ResponseEntity<Video>(new Video(), HttpStatus.NOT_FOUND);
  62. }
  63. }
  64. @PostMapping("")
  65. public ResponseEntity<Video> createVideo(@RequestBody Video video) {
  66. LOGGER.debug("Create video");
  67. Video createdVideo = vs.create(video);
  68. if(createdVideo.getId() != null) {
  69. createdVideo.add(linkTo(methodOn(VideoController.class).findVideoById(createdVideo.getId().toString())).withSelfRel());
  70. createdVideo.add(linkTo(methodOn(UserController.class).findUserById(createdVideo.getUserId().toString())).withRel("user"));
  71. return new ResponseEntity<Video>(createdVideo, HttpStatus.OK);
  72. }
  73. else {
  74. return new ResponseEntity<Video>(new Video(), HttpStatus.BAD_REQUEST);
  75. }
  76. }
  77. @DeleteMapping("{id}")
  78. public ResponseEntity<String> deleteVideoById(@PathVariable("id") String id) {
  79. LOGGER.debug("Delete video id: "+id);
  80. vs.deleteById(id);
  81. return new ResponseEntity<String>("", HttpStatus.OK);
  82. }
  83. }