Skip to content

Commit

Permalink
add persistence functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfy committed May 15, 2024
1 parent 8926a90 commit 72440fe
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 36 deletions.
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
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;
private final ConsumoApi api = new ConsumoApi();
private final String URI_API = "http://www.omdbapi.com/?";
private final String API_KEY = "&apikey=35dcfa5c";

private final List<Serie> 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";
Expand All @@ -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");
}
}
}
Expand All @@ -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();


Expand All @@ -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");
}
}

Expand All @@ -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> serie = serieRepository.findByTituloIgnoreCase(titulo.trim());
if (serie.isPresent()){
System.out.println(serie.get());
return true;
} else{
return false;
}
return false;
}

private String getNomeSerie(){
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -132,6 +155,7 @@ public void setSnopse(String snopse) {
this.sinopse = snopse;
}

//***************** End Getters and Setters *********************
@Override
public String toString() {
return "titulo='" + titulo + '\'' +
Expand All @@ -142,4 +166,5 @@ public String toString() {
", totalTemporadas=" + totalTemporadas +
", sinopse='" + sinopse + '\'';
}

}
Original file line number Diff line number Diff line change
@@ -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<Serie, Long> {

// @Query(value = "SELECT * FROM series WHERE lower(titulo)=?",nativeQuery = true)
Optional<Serie> findByTituloIgnoreCase(String titulo);

}
12 changes: 11 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ spring:
jackson:
date-format: io.swagger.RFC3339DateFormat
serialization:
WRITE_DATES_AS_TIMESTAMPS: false
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

0 comments on commit 72440fe

Please sign in to comment.