From 72440fedb04e5d95bac7ab280c7ea08a97c9012c Mon Sep 17 00:00:00 2001 From: matheusfy Date: Wed, 15 May 2024 20:00:14 -0300 Subject: [PATCH] add persistence functionality --- build.gradle.kts | 7 +++ .../screanmatch/ScreanMatchApplication.java | 11 +++- .../screanmatch/application/Principal.java | 56 ++++++++----------- .../screanmatch/model/entity/Serie.java | 25 +++++++++ .../model/repository/SerieRepository.java | 18 ++++++ src/main/resources/application.yml | 12 +++- 6 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/github/matheusfy/screanmatch/model/repository/SerieRepository.java diff --git a/build.gradle.kts b/build.gradle.kts index 28f24cf..d2c911e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,6 +22,13 @@ dependencies { implementation("com.fasterxml.jackson.core:jackson-annotations:2.12.5") implementation("com.fasterxml.jackson.core:jackson-databind:2.12.5") + // OpenAi + implementation("com.theokanning.openai-gpt3-java:service:0.14.0") + + // Database + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + runtimeOnly("org.postgresql:postgresql") + testImplementation("org.springframework.boot:spring-boot-starter-test") } diff --git a/src/main/java/io/github/matheusfy/screanmatch/ScreanMatchApplication.java b/src/main/java/io/github/matheusfy/screanmatch/ScreanMatchApplication.java index 9ee2725..a0aa3de 100644 --- a/src/main/java/io/github/matheusfy/screanmatch/ScreanMatchApplication.java +++ b/src/main/java/io/github/matheusfy/screanmatch/ScreanMatchApplication.java @@ -1,21 +1,26 @@ package io.github.matheusfy.screanmatch; import io.github.matheusfy.screanmatch.application.Principal; +import io.github.matheusfy.screanmatch.model.repository.SerieRepository; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; - @SpringBootApplication + public class ScreanMatchApplication implements CommandLineRunner { + @Autowired + private SerieRepository serieRepository; public static void main(String[] args) { SpringApplication.run(ScreanMatchApplication.class, args); } @Override - public void run(String... args) throws Exception { - Principal menu = new Principal(); + public void run(String... args) { + Principal menu = new Principal(serieRepository); menu.exibeMenu(); } } diff --git a/src/main/java/io/github/matheusfy/screanmatch/application/Principal.java b/src/main/java/io/github/matheusfy/screanmatch/application/Principal.java index 8862638..00ef559 100644 --- a/src/main/java/io/github/matheusfy/screanmatch/application/Principal.java +++ b/src/main/java/io/github/matheusfy/screanmatch/application/Principal.java @@ -3,10 +3,10 @@ import io.github.matheusfy.screanmatch.model.dtos.SerieDTO; import io.github.matheusfy.screanmatch.model.entity.Serie; import io.github.matheusfy.screanmatch.model.api.ConsumoApi; +import io.github.matheusfy.screanmatch.model.repository.SerieRepository; import java.util.*; - public class Principal { private final Scanner leitor; @@ -14,13 +14,15 @@ public class Principal { private final String URI_API = "http://www.omdbapi.com/?"; private final String API_KEY = "&apikey=35dcfa5c"; - private final List lstSeriesBuscadas = new ArrayList<>(); + private final SerieRepository serieRepository; - public Principal(){ + public Principal(SerieRepository serieRepository){ + this.serieRepository = serieRepository; this.leitor = new Scanner(System.in); } + public void exibeMenu(){ String opcao = "-1"; @@ -30,18 +32,14 @@ public void exibeMenu(){ opcao = leitor.nextLine(); switch (opcao){ - case "1" ->{ - exibeMenuSerie(); - } + case "1" -> exibeMenuSerie(); case "2" ->{ System.out.println("Digite o nome do filme: "); buscarFilme(buildUri(leitor.nextLine())); } - case "0" -> { - System.out.println("Saindo do menu principal"); - } + case "0" -> System.out.println("Saindo do menu principal"); } } } @@ -51,7 +49,7 @@ private void exibeMenuSerie(){ String opcao = "-1"; while(!opcao.equals("0")){ - System.out.println("1 - Buscar série \n2 - Buscar episódio \n3 - Lista Séries buscadas\n0 - Sair"); + System.out.println("1 - Buscar série \n2 - Buscar episódio \n3 - Lista Séries buscadas \n4 - pega a primeira seria do banco\n0 - Sair"); opcao = leitor.nextLine(); @@ -68,13 +66,9 @@ private void exibeMenuSerie(){ buscaEpisodioSerie(getUriSerie(nomeSerie)); } - case "3" -> { - listarSeriesBuscadas(); - } + case "3" -> listarSeriesBuscadas(); - case "0" -> { - System.out.println("Saindo do menu de série"); - } + case "0" -> System.out.println("Saindo do menu de série"); } } @@ -86,26 +80,20 @@ private void listarSeriesBuscadas(){ // TODO: criar outro tipo de listagem. Por exemplo: ordem alfabética, mais bem avaliados, com menor temporada etc... System.out.println("Listando por categoria: "); - lstSeriesBuscadas.stream() + serieRepository.findAll().stream() .sorted(Comparator.comparing(Serie::getCategoria)) .forEach(System.out::println); } - private boolean seriePresentOnList(String nomeSerie){ + private boolean seriePresentOnList(String titulo){ - String nomeSemEspaco = nomeSerie.replace(" ", ""); - - try{ - for(Serie serie: lstSeriesBuscadas){ - if(serie.getTitulo().toLowerCase().replace(" ", "").equals(nomeSemEspaco)){ - System.out.println("Série existente na lista: " + serie.toString()); - return true; - } - } - } catch (Exception error){ - System.out.println("ocorreu um erro inesperado: " + error.getMessage()); + Optional serie = serieRepository.findByTituloIgnoreCase(titulo.trim()); + if (serie.isPresent()){ + System.out.println(serie.get()); + return true; + } else{ + return false; } - return false; } private String getNomeSerie(){ @@ -127,8 +115,12 @@ private void buscarSerie(String uri){ } if(serieDTO.isPresent()){ - System.out.println("Informação serie: " + serieDTO.get().toString()); - lstSeriesBuscadas.add(new Serie(serieDTO.get())); + System.out.println("Informação serie: " + serieDTO.get()); + + Serie serie = new Serie(serieDTO.get()); + + //Adiciona no banco + serieRepository.save(serie); } } diff --git a/src/main/java/io/github/matheusfy/screanmatch/model/entity/Serie.java b/src/main/java/io/github/matheusfy/screanmatch/model/entity/Serie.java index e10fd19..3bb4bc4 100644 --- a/src/main/java/io/github/matheusfy/screanmatch/model/entity/Serie.java +++ b/src/main/java/io/github/matheusfy/screanmatch/model/entity/Serie.java @@ -3,14 +3,23 @@ import io.github.matheusfy.screanmatch.model.api.OpenAiApi; import io.github.matheusfy.screanmatch.model.dtos.SerieDTO; import io.github.matheusfy.screanmatch.model.enums.Categoria; +import jakarta.persistence.*; import java.util.OptionalDouble; + +@Entity +@Table(name = "series") public class Serie { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(unique = true, nullable = false) private String titulo; private String ano; private String duracao; + @Enumerated(EnumType.STRING) private Categoria categoria; private Double avaliacao; private String votos; @@ -20,6 +29,7 @@ public class Serie { private String atores; private String sinopse; + public Serie(SerieDTO serie){ this.titulo = serie.titulo(); this.ano = serie.ano(); @@ -44,6 +54,19 @@ public Serie(SerieDTO serie){ this.totalTemporadas = serie.totalTemporadas(); } + public Serie() { + + } + + //*************** Init Getter and setters **********************// + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + public String getTitulo() { return titulo; } @@ -132,6 +155,7 @@ public void setSnopse(String snopse) { this.sinopse = snopse; } + //***************** End Getters and Setters ********************* @Override public String toString() { return "titulo='" + titulo + '\'' + @@ -142,4 +166,5 @@ public String toString() { ", totalTemporadas=" + totalTemporadas + ", sinopse='" + sinopse + '\''; } + } diff --git a/src/main/java/io/github/matheusfy/screanmatch/model/repository/SerieRepository.java b/src/main/java/io/github/matheusfy/screanmatch/model/repository/SerieRepository.java new file mode 100644 index 0000000..e304c40 --- /dev/null +++ b/src/main/java/io/github/matheusfy/screanmatch/model/repository/SerieRepository.java @@ -0,0 +1,18 @@ +package io.github.matheusfy.screanmatch.model.repository; + +import io.github.matheusfy.screanmatch.model.entity.Serie; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import javax.swing.text.html.Option; +import java.util.List; +import java.util.Optional; + +@Repository +public interface SerieRepository extends JpaRepository { + +// @Query(value = "SELECT * FROM series WHERE lower(titulo)=?",nativeQuery = true) + Optional findByTituloIgnoreCase(String titulo); + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c1d23b4..38cecdb 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,4 +4,14 @@ spring: jackson: date-format: io.swagger.RFC3339DateFormat serialization: - WRITE_DATES_AS_TIMESTAMPS: false \ No newline at end of file + WRITE_DATES_AS_TIMESTAMPS: false main: + web-application-type: none + datasource: + username: postgres + password: 1234 + url: jdbc:postgresql://localhost:5432/screen_match + driver-class-name: org.postgresql.Driver + jpa: + hibernate: + ddl-auto: update + show-sql: true \ No newline at end of file