Created
October 15, 2022 01:05
-
-
Save erickdsama/2c80f6f1fc848bfd378881bd6d7b3287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pokemon.application.model import TourData | |
from pokemon.domain.exceptions import WrongKantoLocation | |
from pokemon.domain.model import AreaData, LocationData, TourResponse | |
from pokemon.domain.repository import AbstractPokemonAdapter | |
class PokemonTourService: | |
def __init__(self, adapter: AbstractPokemonAdapter): | |
self.adapter = adapter | |
@staticmethod | |
def _get_total_pokemon(area: dict) -> int: | |
aggregate_data = area.get('aggregate_data', {}) | |
info = aggregate_data.get('info', {}) | |
return info.get('count', 0) | |
def _there_are_interesting_pokemon(self, area: dict) -> bool: | |
""" | |
Search if there are a Pokémon legendary o mythical in the selected area | |
:param area: dict | |
:return: if exists | |
""" | |
def _its_interesting_pokemon(pok: dict) -> bool: | |
pokemon_obj = pok.get('pokemon_v2_pokemon', {}) | |
specy = pokemon_obj.get('pokemon_v2_pokemonspecy', {}) | |
if specy.get('is_legendary') or specy.get('is_mythical'): | |
return True | |
pokemon = area.get('pokemon', []) | |
interesting = next((poke for poke in pokemon if _its_interesting_pokemon(poke)), None) | |
return True if interesting else False | |
def _parse_area_data(self, area: dict) -> AreaData: | |
total_pokemon = self._get_total_pokemon(area) | |
return AreaData( | |
name=area.get('name'), | |
total_species_pokemon=total_pokemon, | |
interesting_pokemon=self._there_are_interesting_pokemon(area) | |
) | |
def _parse_location_data(self, location_data: dict) -> LocationData: | |
areas = location_data.get('areas') | |
location_data = LocationData(name=location_data.get('name')) | |
for area in areas: | |
location_data.areas.append(self._parse_area_data(area=area)) | |
return location_data | |
def _get_info_by_location(self, location: str) -> LocationData: | |
data = self.adapter.get_location_data(location=location) | |
locations = data.get("locations", []) | |
if not locations: | |
raise WrongKantoLocation(f'{location} is not a Kanto location') | |
location_data = locations[0] | |
location = self._parse_location_data(location_data=location_data) | |
return location | |
def get_data_from_tour(self, tour_data: TourData) -> TourResponse: | |
response = TourResponse() | |
for location in tour_data.planing_route: | |
response.locations.append(self._get_info_by_location(location=location)) | |
return response | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment