Implement rest template api request to imdb database

master
Joshua Sigona 4 years ago
parent 00e9e07ee1
commit 4458545d9e
  1. 17
      src/main/java/com/example/demo/Controller.java
  2. 2
      src/main/java/com/example/demo/DataRepository.java
  3. 63
      src/main/java/com/example/demo/Movie.java
  4. 23
      src/main/java/com/example/demo/MovieRequest.java

@ -1,6 +1,7 @@
package com.example.demo; package com.example.demo;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PatchMapping;
@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
@ -31,15 +33,24 @@ import java.util.Optional;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.websocket.server.PathParam; import javax.websocket.server.PathParam;
@Service
@RestController @RestController
public class Controller { public class Controller {
DataRepository database; DataRepository database;
RestTemplate connection = new RestTemplate();
public Controller(DataRepository database) { public Controller(DataRepository database) {
this.database=database; this.database=database;
} }
@GetMapping("/movies")
public List<Movie> _8(@RequestParam("q") String q) {
System.out.println(connection.getForObject("http://www.omdbapi.com/?apikey={key}&s={title}", String.class, "6ba5969a",q));
MovieRequest mr = connection.getForObject("http://www.omdbapi.com/?apikey={key}&s={title}", MovieRequest.class, "6ba5969a",q);
return mr.movies;
}
@GetMapping("") @GetMapping("")
public Iterable<Data> _1(){ public Iterable<Data> _1(){
return database.findAll(); return database.findAll();
@ -49,8 +60,8 @@ public class Controller {
return database.save(data); return database.save(data);
} }
@GetMapping("/data/{id}") @GetMapping("/data/{id}")
public Optional<Data> _3(@PathVariable Long id) { public Data _3(@PathVariable Long id) {
return database.findById(id); return database.findById(id).orElse(new Data());
} }
@PutMapping("/data/{id}") @PutMapping("/data/{id}")
public Object _5(@PathVariable Long id,@RequestBody Data data) { public Object _5(@PathVariable Long id,@RequestBody Data data) {
@ -75,7 +86,7 @@ public class Controller {
@GetMapping("/lessons/between") @GetMapping("/lessons/between")
public List<Data> _8(@DateTimeFormat(pattern = "MM-dd-yyyy")@RequestParam("date1") Date date1, public List<Data> _8(@DateTimeFormat(pattern = "MM-dd-yyyy")@RequestParam("date1") Date date1,
@DateTimeFormat(pattern = "MM-dd-yyyy")@RequestParam("date2") Date date2) { @DateTimeFormat(pattern = "MM-dd-yyyy")@RequestParam("date2") Date date2) {
return database.findBysubmittedOnBetween(date1, date2); return database.submittedOnBetween(date1, date2);
} }
@DeleteMapping("/data/{id}") @DeleteMapping("/data/{id}")
public String _4(@PathVariable Long id) { public String _4(@PathVariable Long id) {

@ -7,5 +7,5 @@ import org.springframework.data.repository.CrudRepository;
public interface DataRepository extends CrudRepository<Data,Long>{ public interface DataRepository extends CrudRepository<Data,Long>{
Data findByName(String name); Data findByName(String name);
List<Data> findBysubmittedOnBetween(Date date1,Date date2); List<Data> submittedOnBetween(Date date1,Date date2);
} }

@ -0,0 +1,63 @@
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@RequestMapping
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Movie {
String title;
String imdbId;
String poster;
int year;
@JsonCreator
Movie(
@JsonProperty("Title") String title,
@JsonProperty("imdbID") String imdbId,
@JsonProperty("Poster") String poster,
@JsonProperty("Year") int year) {
this.title=title;
this.imdbId=imdbId;
this.poster=poster;
this.year=year;
}
@JsonProperty("title")
public String getTitle() {
return title;
}
@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}
@JsonProperty("imdbId")
public String getImdbId() {
return imdbId;
}
@JsonProperty("imdbId")
public void setImdbId(String imdbId) {
this.imdbId = imdbId;
}
@JsonProperty("poster")
public String getPoster() {
return poster;
}
@JsonProperty("poster")
public void setPoster(String poster) {
this.poster = poster;
}
@JsonProperty("year")
public int getYear() {
return year;
}
@JsonProperty("year")
public void setYear(int year) {
this.year = year;
}
}

@ -0,0 +1,23 @@
package com.example.demo;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.annotation.JsonProperty;
@RequestMapping
public class MovieRequest {
List<Movie> movies;
@JsonProperty("Search")
public List<Movie> getMovies() {
return movies;
}
@JsonProperty("Search")
public void setMovies(List<Movie> movies) {
this.movies = movies;
}
}
Loading…
Cancel
Save