Skip to content

Commit

Permalink
Merge pull request #12 from matheusfy/list-series-byCategory
Browse files Browse the repository at this point in the history
list series by category
  • Loading branch information
matheusfy authored May 17, 2024
2 parents 2f1875f + 6291309 commit 43c569d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import io.github.matheusfy.screanmatch.model.enums.Categoria;
import io.github.matheusfy.screanmatch.model.repository.SerieRepository;

import java.io.IOException;
import java.util.*;

import static io.github.matheusfy.screanmatch.model.enums.Categoria.listarCategoria;

public class Principal {

private final Scanner leitor;
Expand Down Expand Up @@ -48,9 +49,8 @@ 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 \n4 - pega a primeira seria do banco\n0 - Sair");
System.out.println("1 - Buscar série \n2 - Buscar episódio \n3 - Lista séries buscadas \n4 - Buscar série por categoria \n99 - Limpar console\n0 - Sair");
opcao = leitor.nextLine();


switch (opcao){
case "1" ->{
Expand All @@ -66,7 +66,7 @@ private void exibeMenuSerie(){
}

case "3" -> listarSeriesBuscadas();

case "4" -> buscarSeriesCategoria();
case "0" -> System.out.println("Saindo do menu de série");
}
}
Expand All @@ -84,6 +84,26 @@ private void listarSeriesBuscadas(){
.forEach(System.out::println);
}

private void buscarSeriesCategoria(){
System.out.println("Categorias possíveis: " );
listarCategoria();

System.out.println("Digite uma categoria: ");
String categoria = leitor.nextLine();

try {
List<Serie> seriesCategoria = serieRepository.findByCategoria(Categoria.fromString(categoria));
if(!seriesCategoria.isEmpty()){
seriesCategoria.forEach(System.out::println);
} else {
System.out.printf("Série não encontrada para categoria %s%n", categoria);
}
} catch (Exception error){
System.out.println("Não foi possivel obter a lista. Erro: " + error.getMessage());
}

}

private boolean seriePresentOnList(String titulo){

Optional<Serie> serie = serieRepository.findByTituloIgnoreCase(titulo.trim());
Expand Down Expand Up @@ -138,5 +158,5 @@ private String buildUri(String nomePrograma){
String OMDB_URI = "http://www.omdbapi.com/?";
return OMDB_URI + "t=" + nomePrograma.replace(" ", "+") + API_KEY;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public enum Categoria {

public static Categoria fromString(String text) {
for (Categoria categoria : Categoria.values()) {
if (categoria.categoriaOmdb.equalsIgnoreCase(text)) {
if (categoria.categoriaOmdb.equalsIgnoreCase(text) || categoria.toString().equalsIgnoreCase(text)) {
return categoria;
}
}
throw new IllegalArgumentException("Nenhuma categoria encontrada para a string fornecida: " + text);
}

public static void listarCategoria(){
for(Categoria categoria: Categoria.values()){
System.out.println(categoria);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.github.matheusfy.screanmatch.model.repository;

import io.github.matheusfy.screanmatch.model.entity.Serie;
import io.github.matheusfy.screanmatch.model.enums.Categoria;
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);

List<Serie> findByCategoria(Categoria categoria);
}
5 changes: 3 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ spring:
jackson:
date-format: io.swagger.RFC3339DateFormat
serialization:
WRITE_DATES_AS_TIMESTAMPS: false main:
web-application-type: none
WRITE_DATES_AS_TIMESTAMPS: false
main:
web-application-type: none
datasource:
username: ${DB_USER}
password: ${DB_PASSWORD}
Expand Down

0 comments on commit 43c569d

Please sign in to comment.