VideoController.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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).findVideoById(v.getId().toString())).withSelfRel());
  45. }
  46. return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
  47. }
  48. @GetMapping("{id}")
  49. public ResponseEntity<Video> findVideoById(@PathVariable("id") String id) {
  50. LOGGER.debug("Get video id: "+id);
  51. Optional<Video> video = vs.findById(id);
  52. if(!video.isEmpty()) {
  53. return new ResponseEntity<Video>(new Video(), HttpStatus.OK);
  54. }
  55. else {
  56. Video v = video.get();
  57. v.add(linkTo(methodOn(VideoController.class).findVideoById(id)).withSelfRel());
  58. return new ResponseEntity<Video>(video.get(), HttpStatus.NOT_FOUND);
  59. }
  60. }
  61. @PostMapping("")
  62. public ResponseEntity<Video> createVideo(@RequestBody Video video) {
  63. LOGGER.debug("Create video");
  64. Video createdVideo = vs.create(video);
  65. if(createdVideo.getId() != null) {
  66. createdVideo.add(linkTo(methodOn(VideoController.class).findVideoById(createdVideo.getId().toString())).withSelfRel());
  67. return new ResponseEntity<Video>(createdVideo, HttpStatus.OK);
  68. }
  69. else {
  70. return new ResponseEntity<Video>(new Video(), HttpStatus.BAD_REQUEST);
  71. }
  72. }
  73. @DeleteMapping("{id}")
  74. public ResponseEntity<String> deleteVideoById(@PathVariable("id") String id) {
  75. LOGGER.debug("Delete video id: "+id);
  76. vs.deleteById(id);
  77. return new ResponseEntity<String>("", HttpStatus.OK);
  78. }
  79. }