diff --git a/api/controllers/pokemon.go b/api/controllers/pokemon.go index fad39ce..2c05bcb 100644 --- a/api/controllers/pokemon.go +++ b/api/controllers/pokemon.go @@ -97,8 +97,8 @@ func (h *PokemonHandler) GetPokemon(c *gin.Context) { } type ListPokemonParams struct { - PokemonName string `form:"pokemonName" json:"pokemonName,omitempty"` - PokemonId int64 `form:"pokemonType" json:"pokemonId,omitempty"` + PokemonName string `form:"pokemonName" json:"pokemonName,omitempty"` + PokemonTypeId int64 `form:"pokemonTypeId" json:"pokemonTypeId,omitempty"` } // ListPokemon fetches all Pokemon from the database and returns them as JSON data in the HTTP response. @@ -134,20 +134,28 @@ func (h *PokemonHandler) ListPokemon(c *gin.Context) { var count int64 tx := h.db.Model(&models.PokemonSpecies{}). - Order(clause.OrderByColumn{ - Column: clause.Column{Name: "id"}, - Desc: false, - }). Session(&gorm.Session{}) if queryParams.PokemonName != "" { tx = tx.Where("name LIKE ?", fmt.Sprintf("%%%s%%", queryParams.PokemonName)) } - if queryParams.PokemonId != 0 { - tx = tx.Preload("Varieties", "primary_type_id = ? or secondary_type_id = ?", queryParams.PokemonId) + if queryParams.PokemonTypeId != 0 { + tx = tx.InnerJoins("JOIN pokemons p ON p.pokemon_species_id = pokemon_species.id"). + Where("p.primary_type_id = ?", queryParams.PokemonTypeId). + Or("p.secondary_type_id = ?", queryParams.PokemonTypeId). + Group("pokemon_species.id"). + Order(clause.OrderByColumn{ + Column: clause.Column{Name: "pokemon_species.id"}, + Desc: false, + }) } else { - tx = tx.Preload("Varieties") + tx = tx.Preload("Varieties", func(db *gorm.DB) *gorm.DB { + return db.Order("id ASC") + }).Order(clause.OrderByColumn{ + Column: clause.Column{Name: "id"}, + Desc: false, + }) } tx = tx.Preload("Varieties.PrimaryType").Preload("Varieties.SecondaryType") diff --git a/api/requests/pokemon.http b/api/requests/pokemon.http index a4e6c88..621bb5a 100644 --- a/api/requests/pokemon.http +++ b/api/requests/pokemon.http @@ -17,5 +17,5 @@ GET {{host}}/pokemon GET {{host}}/pokemon?pokemonName=bulba ### -GET {{host}}/pokemon?pokemonId=1&pageSize=20 +GET {{host}}/pokemon?pokemonTypeId=10&pageSize=20 diff --git a/frontend/src/components/pokemon/FilterForm.tsx b/frontend/src/components/pokemon/FilterForm.tsx index 34ee47e..5224acb 100644 --- a/frontend/src/components/pokemon/FilterForm.tsx +++ b/frontend/src/components/pokemon/FilterForm.tsx @@ -19,8 +19,8 @@ export const FilterForm = ({onSubmit}: { onSubmit: (params: ListPokemonParams) = } onSubmit({ - pokemonName: pokemonName && (pokemonName as string).toLowerCase(), - pokemonType: pokemonType && pokemonType as string, + pokemonName: pokemonName !== "" ? (pokemonName as string).toLowerCase() : null, + pokemonTypeId: moveTypes.find(moveType => moveType.name.toLowerCase() === pokemonType?.toString().toLowerCase())?.id ?? null, }) }; diff --git a/frontend/src/models/pokemon.ts b/frontend/src/models/pokemon.ts index 5ecaf39..5ae3f13 100644 --- a/frontend/src/models/pokemon.ts +++ b/frontend/src/models/pokemon.ts @@ -59,5 +59,5 @@ export interface MoveType { export interface ListPokemonParams { pokemonName: string | null - pokemonType: string | null + pokemonTypeId: number | null } \ No newline at end of file