123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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.Video;
- import es.uv.garcosda.services.VideoService;
- @RestController
- @RequestMapping("/api/v1/videos")
- public class VideoController {
- private final static Logger LOGGER = LoggerFactory.getLogger(VideoController.class);
-
- @Autowired private VideoService vs;
-
- @GetMapping("")
- public ResponseEntity<List<Video>> findVideos(@RequestParam(value="title", required = false) Optional<String> title,
- @RequestParam(value="user", required = false) Optional<String> userId) {
-
- List<Video> videos = new ArrayList<Video>();
-
- if(!title.isPresent() && !userId.isPresent()) {
- LOGGER.debug("Get all videos");
- videos = vs.findAll();
- }
- else if(title.isPresent()) {
- LOGGER.debug("Get videos by title");
- videos = vs.findByTitle(title.get());
- }
- else if(userId.isPresent()) {
- LOGGER.debug("Get videos by user");
- videos = vs.findByUserId(userId.get());
- }
-
- for (Video v : videos) {
- v.add(linkTo(methodOn(VideoController.class).findVideos(null, null)).withRel("collection"));
- v.add(linkTo(methodOn(VideoController.class).findVideoById(v.getId().toString())).withSelfRel());
- v.add(linkTo(methodOn(UserController.class).findUserById(v.getUserId().toString())).withRel("user"));
- }
-
- return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
- }
-
- @GetMapping("{id}")
- public ResponseEntity<Video> findVideoById(@PathVariable("id") String id) {
- LOGGER.debug("Get video id: "+id);
- Optional<Video> video = vs.findById(id);
-
- if(!video.isEmpty()) {
- Video v = video.get();
- v.add(linkTo(methodOn(VideoController.class).findVideoById(id)).withSelfRel());
- v.add(linkTo(methodOn(UserController.class).findUserById(v.getUserId().toString())).withRel("user"));
- return new ResponseEntity<Video>(video.get(), HttpStatus.OK);
- }
- else {
- return new ResponseEntity<Video>(new Video(), HttpStatus.NOT_FOUND);
- }
- }
-
- @PostMapping("")
- public ResponseEntity<Video> createVideo(@RequestBody Video video) {
- LOGGER.debug("Create video");
- Video createdVideo = vs.create(video);
-
- if(createdVideo.getId() != null) {
- createdVideo.add(linkTo(methodOn(VideoController.class).findVideoById(createdVideo.getId().toString())).withSelfRel());
- createdVideo.add(linkTo(methodOn(UserController.class).findUserById(createdVideo.getUserId().toString())).withRel("user"));
- return new ResponseEntity<Video>(createdVideo, HttpStatus.OK);
- }
- else {
- return new ResponseEntity<Video>(new Video(), HttpStatus.BAD_REQUEST);
- }
- }
-
- @DeleteMapping("{id}")
- public ResponseEntity<String> deleteVideoById(@PathVariable("id") String id) {
- LOGGER.debug("Delete video id: "+id);
- vs.deleteById(id);
- return new ResponseEntity<String>("", HttpStatus.OK);
- }
-
- }
|