|
@@ -2,14 +2,20 @@ package es.uv.garcosda.api.controllers;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+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 org.springframework.web.client.RestTemplate;
|
|
|
|
|
@@ -29,13 +35,43 @@ public class VideosController {
|
|
|
}
|
|
|
|
|
|
@GetMapping()
|
|
|
- public ResponseEntity<?> findVideos() {
|
|
|
- ResponseEntity<Video[]> response = template.getForEntity(videosApi, Video[].class);
|
|
|
+ public ResponseEntity<?> findVideos(@RequestParam(value="title", required = false) Optional<String> title,
|
|
|
+ @RequestParam(value="user", required = false) Optional<String> userId) {
|
|
|
+
|
|
|
+ String url = videosApi;
|
|
|
+ if(title.isPresent()) {
|
|
|
+ url+="?title="+title.get();
|
|
|
+ }
|
|
|
+ else if(userId.isPresent()) {
|
|
|
+ url+="?userId="+userId.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ ResponseEntity<Video[]> response = template.getForEntity(url, Video[].class);
|
|
|
if(response.getStatusCode() == HttpStatus.OK) {
|
|
|
List<Video> videos = Arrays.stream(response.getBody()).collect(Collectors.toList());
|
|
|
return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
|
|
|
}
|
|
|
- return new ResponseEntity<String>("API returns no response", HttpStatus.BAD_REQUEST);
|
|
|
+ return new ResponseEntity<String>("API returned no content", HttpStatus.SERVICE_UNAVAILABLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("{id}")
|
|
|
+ public ResponseEntity<?> findVideoById(@PathVariable("id") String id){
|
|
|
+ Video v = template.getForObject(videosApi+"/"+id, Video.class);
|
|
|
+ if(v.getId() != null) {
|
|
|
+ return new ResponseEntity<Video>(v, HttpStatus.OK);
|
|
|
+ }
|
|
|
+ return new ResponseEntity<String>("Video not found", HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping()
|
|
|
+ public ResponseEntity<?> createVideo(@RequestBody Video video) {
|
|
|
+ ResponseEntity<Video> response = template.postForEntity(videosApi, video, Video.class);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("{id}")
|
|
|
+ public ResponseEntity<String> deleteVideoById(@PathVariable("id") String id) {
|
|
|
+ return template.getForEntity(videosApi, String.class);
|
|
|
}
|
|
|
|
|
|
|