diff --git a/practice-app/backend/disasterresponse/pom.xml b/practice-app/backend/disasterresponse/pom.xml index 89aff297..e0f9aa9c 100644 --- a/practice-app/backend/disasterresponse/pom.xml +++ b/practice-app/backend/disasterresponse/pom.xml @@ -60,7 +60,17 @@ okhttp 4.9.3 + + com.fasterxml.jackson.core + jackson-databind + 2.13.3 + + + org.projectlombok + lombok + + diff --git a/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Image.java b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Image.java new file mode 100644 index 00000000..a25cfc0a --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Image.java @@ -0,0 +1,20 @@ +package Response.ImdbResponse; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Image { + private int height; + private String id; + private String url; + private int width; + +} + + diff --git a/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/ImdbResponse.java b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/ImdbResponse.java new file mode 100644 index 00000000..bff9956f --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/ImdbResponse.java @@ -0,0 +1,33 @@ +package Response.ImdbResponse; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.NoArgsConstructor; + +import java.util.List; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class ImdbResponse { + private String type; + private String query; + @JsonProperty("results") + private List movies; +} + + + + + + + + + + + + diff --git a/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Movie.java b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Movie.java new file mode 100644 index 00000000..9942a4c0 --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Movie.java @@ -0,0 +1,28 @@ +package Response.ImdbResponse; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonRootName; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonRootName(value = "results") +public class Movie { + private String id; + private Image image; + private int runningTimeInMinutes; + private String nextEpisode; + private int numberOfEpisodes; + private int seriesEndYear; + private int seriesStartYear; + private String title; + private String titleType; + private int year; + private List principals; +} \ No newline at end of file diff --git a/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Principal.java b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Principal.java new file mode 100644 index 00000000..1528a6ed --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Principal.java @@ -0,0 +1,33 @@ +package Response.ImdbResponse; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +class Principal { + + private String id; + + private String legacyNameText; + + private String name; + + private String category; + + private List characters; + + private int endYear; + + private int episodeCount; + + private List roles; + + private int startYear; +} \ No newline at end of file diff --git a/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Role.java b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Role.java new file mode 100644 index 00000000..ea160a75 --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Role.java @@ -0,0 +1,16 @@ +package Response.ImdbResponse; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Role { + private String character; + private String characterId; + +} diff --git a/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/controller/FilmController.java b/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/controller/FilmController.java new file mode 100644 index 00000000..53638c02 --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/controller/FilmController.java @@ -0,0 +1,35 @@ +package com.a1.disasterresponse.controller; + +import Response.ImdbResponse.Movie; +import com.a1.disasterresponse.service.FilmService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.io.IOException; +import java.util.List; + +@Controller +@RequestMapping("/film") +public class FilmController { + + private final FilmService filmService; + + public FilmController(FilmService filmService) { + this.filmService = filmService; + } + + @GetMapping("/id") + public ResponseEntity> getFilmId(@RequestParam String query, @RequestParam int limit) { + try { + List movies = filmService.getFilmId(query, limit); + return new ResponseEntity<>(movies, HttpStatus.OK); + } catch (IOException e) { + e.printStackTrace(); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/service/FilmService.java b/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/service/FilmService.java new file mode 100644 index 00000000..de4bb3fd --- /dev/null +++ b/practice-app/backend/disasterresponse/src/main/java/com/a1/disasterresponse/service/FilmService.java @@ -0,0 +1,53 @@ +package com.a1.disasterresponse.service; + +import Response.ImdbResponse.ImdbResponse; +import Response.ImdbResponse.Movie; +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + + +@Service +public class FilmService { + String BASE_URL = "https://imdb8.p.rapidapi.com/title/v2/find?title="; + + String API_KEY = "8720851bd4msh5b9408dcf528f49p16c2cfjsna4066bc59d74"; + + private static final OkHttpClient client = new OkHttpClient(); + + private static final ObjectMapper mapper = new ObjectMapper(); + + + public List getFilmId(String query, int limit) throws IOException { + + String encodedQueryParam = URLEncoder.encode(query, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); + + Request request = new Request.Builder() + .url(BASE_URL + encodedQueryParam + "&limit="+ Integer.toString(limit)+"&sortArg=moviemeter%2Casc") + .get() + .addHeader("X-RapidAPI-Key", API_KEY) + .addHeader("X-RapidAPI-Host", "imdb8.p.rapidapi.com") + .build(); + try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + throw new IOException("Unexpected code " + response); + } + String body = response.body().string(); + + ImdbResponse imdbResponse = mapper.readValue(body, ImdbResponse.class); + return imdbResponse.getMovies(); + } + } + + + + +} +