Skip to content

Commit

Permalink
fix: (all) type filtering with id
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobPotter committed Nov 2, 2024
1 parent 3f28828 commit 1c48561
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
26 changes: 17 additions & 9 deletions api/controllers/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion api/requests/pokemon.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 2 additions & 2 deletions frontend/src/components/pokemon/FilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/models/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ export interface MoveType {

export interface ListPokemonParams {
pokemonName: string | null
pokemonType: string | null
pokemonTypeId: number | null
}

0 comments on commit 1c48561

Please sign in to comment.