Browse Source

Access from API to repository

dagarcos 2 years ago
parent
commit
2d7d642f93

+ 39 - 3
DBCDS_S10_1_Api/src/main/java/es/uv/garcosda/api/controllers/VideosController.java

@@ -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);
 	}
 
 	

+ 6 - 6
DBCDS_S10_1_Repository/src/main/java/es/uv/garcosda/repository/VideoController.java

@@ -53,25 +53,25 @@ public class VideoController {
 	}
 	
 	@GetMapping("{id}")
-	public ResponseEntity<Video> findVideoById(@PathVariable("id") String id) {
+	public Video findVideoById(@PathVariable("id") String id) {
 		LOGGER.debug("Get video id: "+id);
 		Optional<Video> video = vr.findById(id);
 		
-		if(!video.isEmpty()) {
-			return new ResponseEntity<Video>(new Video(), HttpStatus.OK);
+		if(video.isEmpty()) {
+			return new Video();
 		}
 		else {
-			return new ResponseEntity<Video>(video.get(), HttpStatus.NOT_FOUND);
+			return video.get();
 		}
 	}
 	
-	@PostMapping("")
+	@PostMapping()
 	public ResponseEntity<Video> createVideo(@RequestBody Video video) {
 		LOGGER.debug("Create video");
 		Video createdVideo = vr.save(video);
 		
 		if(createdVideo.getId() != null) {
-			return new ResponseEntity<Video>(createdVideo, HttpStatus.OK);
+			return new ResponseEntity<Video>(createdVideo, HttpStatus.CREATED);
 		}
 		else {
 			return new ResponseEntity<Video>(new Video(), HttpStatus.BAD_REQUEST);