diff --git a/lib/data/dtos/pokemon_dto.dart b/lib/data/dtos/pokemon_dto.dart index 1be5d9d..b4faac2 100644 --- a/lib/data/dtos/pokemon_dto.dart +++ b/lib/data/dtos/pokemon_dto.dart @@ -24,31 +24,20 @@ class PokemonDataDto { final String? name; final String? url; final String? imageUrl; - final PokemonDetailsDto? details; + final int? height; // Добавлено поле для роста + final int? weight; // Добавлено поле для веса + final List? abilities; // Добавлено поле для способностей const PokemonDataDto({ this.name, this.url, this.imageUrl, - this.details, - }); - - factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class PokemonDetailsDto { - final int? height; - final int? weight; - final List? abilities; - - const PokemonDetailsDto({ this.height, this.weight, this.abilities, }); - factory PokemonDetailsDto.fromJson(Map json) => _$PokemonDetailsDtoFromJson(json); + factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(json); } @JsonSerializable(createToJson: false) diff --git a/lib/data/dtos/pokemon_dto.g.dart b/lib/data/dtos/pokemon_dto.g.dart index eeacaf9..c0d0e3c 100644 --- a/lib/data/dtos/pokemon_dto.g.dart +++ b/lib/data/dtos/pokemon_dto.g.dart @@ -20,13 +20,6 @@ PokemonDataDto _$PokemonDataDtoFromJson(Map json) => name: json['name'] as String?, url: json['url'] as String?, imageUrl: json['imageUrl'] as String?, - details: json['details'] == null - ? null - : PokemonDetailsDto.fromJson(json['details'] as Map), - ); - -PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map json) => - PokemonDetailsDto( height: (json['height'] as num?)?.toInt(), weight: (json['weight'] as num?)?.toInt(), abilities: (json['abilities'] as List?) diff --git a/lib/data/mappers/pokemon_mapper.dart b/lib/data/mappers/pokemon_mapper.dart index 17fb214..21f1312 100644 --- a/lib/data/mappers/pokemon_mapper.dart +++ b/lib/data/mappers/pokemon_mapper.dart @@ -16,14 +16,7 @@ extension PokemonDataDtoToModel on PokemonDataDto { CardData toDomain() => CardData( name ?? 'UNKNOWN', imageUrl: imageUrl ?? _imagePlaceholder, - descriptionText: details?.toDescriptionText() ?? 'ID: ${url?.split('/')[6] ?? 'UNKNOWN'}', + descriptionText: 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} g, Abilities: ${abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}', id: url?.split('/')[6] ?? 'UNKNOWN', ); -} - -extension PokemonDetailsDtoToModel on PokemonDetailsDto { - String toDescriptionText() { - final abilitiesText = abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'; - return 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} kg, Abilities: $abilitiesText'; - } } \ No newline at end of file diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index 6115683..527c90b 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -18,11 +18,11 @@ class PokeRepository extends ApiInterface { Future loadData({ OnErrorCallback? onError, String? q, - int page = 1, - int pageSize = 25, + int page = 0, + int pageSize = 10, }) async { try { - final String url = '$_baseUrl/pokemon'; + String url = '$_baseUrl/pokemon'; final Map queryParameters = { 'offset': page, @@ -30,7 +30,7 @@ class PokeRepository extends ApiInterface { }; if (q != null && q.isNotEmpty) { - queryParameters['name'] = q; + url = '$_baseUrl/pokemon/$q'; } final Response response = await _dio.get>( @@ -42,15 +42,29 @@ class PokeRepository extends ApiInterface { final List pokemonDataList = dto.results ?? []; final List updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async { - final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/${pokemonData.url?.split('/')[6]}'; + final id = pokemonData.url?.split('/')[6]; + final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/$id'; final detailsResponse = await _dio.get>(detailsUrl); - final PokemonDetailsDto detailsDto = PokemonDetailsDto.fromJson(detailsResponse.data as Map); - final String imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemonData.url?.split('/')[6]}.png'; + + final int height = detailsResponse.data?['height']; + final int weight = detailsResponse.data?['weight']; + final List abilities = detailsResponse.data?['abilities']; + + final List abilityDtos = abilities.map((ability) { + return AbilityDto( + ability: AbilityDetailDto( + name: ability['ability']['name'], + ), + ); + }).toList(); + return PokemonDataDto( name: pokemonData.name, url: pokemonData.url, - imageUrl: imageUrl, - details: detailsDto, + imageUrl: 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png', + height: height, + weight: weight, + abilities: abilityDtos, ); }));