Skip to content

Commit

Permalink
map pokemons results
Browse files Browse the repository at this point in the history
  • Loading branch information
almogko1 committed Sep 2, 2024
1 parent 4315893 commit 2233cf1
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/pokedexServer/pokemons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def getPokemonsPage(request):
offset = int(request.GET.get('offset', 0))
limit = int(request.GET.get('limit', 20))

if (limit > 20):
limit = 20

pokeAPIPokemons = pb.APIResourceList('pokemon')
count = pokeAPIPokemons.count
full_url = request.build_absolute_uri()[:-1]
Expand Down Expand Up @@ -58,14 +61,16 @@ def getPokemonsPage(request):
else:
next_limit = count - next_offset
next = f'{full_url}?offset={next_offset}&limit={next_limit}'

results = paginate(list(pokeAPIPokemons.__iter__()), offset, limit)
pokeAPIPokemonsNamesPage = paginate(list(pokeAPIPokemons.names), offset, limit)
results = list(map(lambda name: fetchAndMapPokemon(name).__dict__, pokeAPIPokemonsNamesPage))
responseData = APIPageResponse(pokeAPIPokemons.count, next, previous, results)
return JsonResponse(responseData.__dict__)



def getPokemon(request, name):
def fetchAndMapPokemon(name):
pokeAPIPokemon = pb.pokemon(name)
pokemonModel = mapPokeAPIPokemonToPokemonModel(pokeAPIPokemon)
return pokemonModel
def getPokemon(request, name):
pokemonModel = fetchAndMapPokemon(name)
return JsonResponse(pokemonModel.__dict__)

0 comments on commit 2233cf1

Please sign in to comment.