Record EpisodeDatas
package br.com.alura.screenmatch.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record EpisodeDatas(@JsonAlias("Title") String title,
@JsonAlias("Episode") Integer episode,
@JsonAlias("imdbRating") String rating,
@JsonAlias("Released") String released) {
}
SeasonDatas
package br.com.alura.screenmatch.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public record SeasonDatas(@JsonAlias("Season") Integer season,
@JsonAlias("Episodes") List<EpisodeDatas> episodes) {
}
Main
public class Main {
private Scanner reader = new Scanner(System.in);
private ConsumeApi consume = new ConsumeApi();
private ConvertDatas converter = new ConvertDatas();
private final String ADDRESS = "https://www.omdbapi.com/?t=";
private final String API_KEY = "&apikey=96433339";
public void showMenu(){
System.out.println("Type the series name: ");
var seriesName = reader.nextLine();
var json = consume.getData(ADDRESS + seriesName.replace(" ", "+") + API_KEY);
SerieDatas datas = converter.getDatas(json, SerieDatas.class);
System.out.println(datas);
List<SeasonDatas> seasons = new ArrayList<>();
for (int i = 1; i <= datas.totalSeasons(); i++) {
json = consume.getData(ADDRESS + seriesName.replace(" ", "+") + "&season=" + i + API_KEY);
SeasonDatas seasonDatas = converter.getDatas(json, SeasonDatas.class);
seasons.add(seasonDatas);
}
seasons.forEach(System.out::println);
/*for (int i = 0; i < datas.totalSeasons(); i++) {
List<EpisodeDatas> seasonEpisodes = seasons.get(i).episodes();
for (int j = 0; j < seasonEpisodes.size(); j++){
System.out.println(seasonEpisodes.get(j).title());
}
}*/
seasons.forEach(s -> s.episodes().forEach(e -> System.out.println(e.title())));
}
}
ScreenmatchApplication
@SpringBootApplication
public class ScreenmatchApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ScreenmatchApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Main main = new Main();
main.showMenu();
}
}