Skip to content

Commit

Permalink
implemente get episode by word and remove tests [feature]
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfy committed May 5, 2024
1 parent 0350d50 commit 534fc92
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

- name: Make gradlew executable
run: chmod +x ./gradlew

- name: Build with Gradle Wrapper
run: ./gradlew build



# NOTE: The Gradle Wrapper is the default and recommended way to run Gradle (https://docs.gradle.org/current/userguide/gradle_wrapper.html).
# If your project does not have the Gradle Wrapper configured, you can use the following configuration to run Gradle with a specified version.
#
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.matheusfy.screanmatch;

import io.github.matheusfy.screanmatch.application.Principal;
import io.github.matheusfy.screanmatch.service.ConsumoApi;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ScreanMatchApplication implements CommandLineRunner {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package io.github.matheusfy.screanmatch.application;

import io.github.matheusfy.screanmatch.model.Episodio;
import io.github.matheusfy.screanmatch.model.dtos.EpisodioDTO;
import io.github.matheusfy.screanmatch.service.ConsumoApi;

import java.util.List;
import java.util.Scanner;
import java.util.logging.Logger;

public class Principal {

private Scanner leitor = new Scanner(System.in);
private final String URI_API = "http://www.omdbapi.com/?";
private final String API_KEY = "&apikey=35dcfa5c";
private final Scanner leitor;
private ConsumoApi api;

public Principal(){
this.api = new ConsumoApi();
this.api = new ConsumoApi();
this.leitor = new Scanner(System.in);
}

public void exibeMenu(){
System.out.println("Digite o nome da série/filme: ");
String serie = leitor.nextLine();

String URI_API = "http://www.omdbapi.com/?";
String API_KEY = "&apikey=35dcfa5c";
String uri = URI_API + "t=" + serie.replace(" ", "+") + API_KEY;

api.obterDado(uri);

}

}
44 changes: 29 additions & 15 deletions src/main/java/io/github/matheusfy/screanmatch/model/Episodio.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@

public class Episodio {
private Integer temporada;

private String titulo;
private Integer episodio;
private Double avaliacao;
private LocalDate dataLancamento;

public Episodio(Integer temporada, EpisodioDTO episodio){
this.temporada = temporada;
this.titulo = episodio.titulo();
this.episodio = episodio.numero();
public Episodio(Integer temporada, EpisodioDTO episodio){
this.temporada = temporada;
this.titulo = episodio.titulo();
this.episodio = episodio.numero();

try {
this.avaliacao = Double.valueOf(episodio.avaliacao());
} catch (NumberFormatException error){
this.avaliacao = 0.0;
}
try {
this.avaliacao = Double.valueOf(episodio.avaliacao());
} catch (NumberFormatException error){
this.avaliacao = 0.0;
}

try{
this.dataLancamento = LocalDate.parse(episodio.dataLancamento());
} catch (DateTimeParseException error) {
this.dataLancamento = null;
}
try{
this.dataLancamento = LocalDate.parse(episodio.dataLancamento());
} catch (DateTimeParseException error) {
this.dataLancamento = null;
}

}
}

public String getTitulo() {
return this.titulo;
}

public Integer getTemporada(){
return this.temporada;
}

public Double getAvaliacao(){
return this.avaliacao;
}

@Override
public String toString() {
Expand All @@ -42,4 +54,6 @@ public String toString() {
", avaliacao=" + avaliacao +
", dataLancamento=" + dataLancamento;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.matheusfy.screanmatch.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.matheusfy.screanmatch.model.Episodio;
import io.github.matheusfy.screanmatch.model.dtos.EpisodioDTO;
Expand All @@ -12,15 +13,13 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class ConsumoApi {
private HttpClient client;
private final HttpClient client;
private final ConverterDados conversor;
private ObjectMapper mapper;
private ConverterDados conversor;

public ConsumoApi(){
this.client = HttpClient.newHttpClient();
Expand All @@ -29,8 +28,7 @@ public ConsumoApi(){
}

private HttpRequest buildRequest(String uri){
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri)).GET().build();
return request;
return HttpRequest.newBuilder().uri(URI.create(uri)).GET().build();
}

private HttpResponse<String> sendRequest(HttpRequest request){
Expand All @@ -46,31 +44,32 @@ private HttpResponse<String> sendRequest(HttpRequest request){
private String buildAndSendRequest(String uri){
HttpResponse<String> response = sendRequest(buildRequest(uri));
if (response!=null) {
return response.body().toString();
return response.body();
}
return "";
}

public void obterDado(String apiUri){
public void obterDado(String apiUri){

String json = buildAndSendRequest(apiUri);
String tipo = conversor.getType(json);

switch (tipo){
case "series":
switch (tipo) {
case "series" -> {
SerieDTO serie = conversor.obterDados(json, SerieDTO.class);
System.out.println("Informação serie: " + serie.toString());
if (serie.totalTemporadas() != "N/A"){
List<TemporadaDTO> lstTemporadas= getTemporadas(apiUri, conversor.strToInt(serie.totalTemporadas()));
if (serie.totalTemporadas() != "N/A") {
List<TemporadaDTO> lstTemporadas = getTemporadas(apiUri, conversor.strToInt(serie.totalTemporadas()));
// getMelhoresEpisodios(lstTemporadas);
getEpisodios(lstTemporadas);
}

break;
case "movie":


}
}
case "movie" -> {
DadosFilmeDTO filme = conversor.obterDados(json, DadosFilmeDTO.class);
System.out.println(filme.toString());
break;
}
}

}
Expand All @@ -93,22 +92,29 @@ public List<Episodio> getEpisodios(List<TemporadaDTO> lstTemporadas){
return lstTemporadas.stream()
.flatMap( t -> t.lstEpisodios().stream()
.map(e -> new Episodio(t.numero(), e)))
.collect(Collectors.toList());
.toList();
}

public List<EpisodioDTO> getMelhoresEpisodios(List<TemporadaDTO> lstTemporadas) {
public List<EpisodioDTO> getMelhores5Episodios(List<TemporadaDTO> lstTemporadas) {

List<EpisodioDTO> allEpisodes = lstTemporadas.stream()
.flatMap(temp -> temp.lstEpisodios().stream())
.collect(Collectors.toList());
.toList();

List<EpisodioDTO> betEpisodes = allEpisodes.stream()
return allEpisodes.stream()
.filter(e->!e.avaliacao().equalsIgnoreCase("N/A"))
.sorted(Comparator.comparing(EpisodioDTO::avaliacao).reversed())
.limit(5)
.collect(Collectors.toList());
}

public Optional<Episodio> buscaEpisodio(String nomeEpisodio, List<Episodio> episodios){

Optional<Episodio> episodio = episodios.stream()
.filter(e -> e.getTitulo().contains(nomeEpisodio))
.findFirst();

return betEpisodes;
return episodio;
}

}

This file was deleted.

0 comments on commit 534fc92

Please sign in to comment.