diff --git a/lib/data/dtos/games_dto.dart b/lib/data/dtos/games_dto.dart index 6bc1128..5a90232 100644 --- a/lib/data/dtos/games_dto.dart +++ b/lib/data/dtos/games_dto.dart @@ -2,59 +2,24 @@ import 'package:json_annotation/json_annotation.dart'; part 'games_dto.g.dart'; -@JsonSerializable(createToJson: false) -class GamesDto { - final List? data; - final MetaDto? meta; - - const GamesDto({ - this.data, - this.meta, - }); - - factory GamesDto.fromJson(Map json) => _$GamesDtoFromJson(json); -} - @JsonSerializable(createToJson: false) class GameDataDto { - final String? id; - final List? attributes; - - const GameDataDto({this.id, this.attributes}); - - factory GameDataDto.fromJson(Map json) => _$GameDataDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class GameAttributesDataDto { - final String? alias; + final int? id; + final String? title; final String? descriptionShort; final String? description; final String? photoUrl; - final String? year; + final int? year; - const GameAttributesDataDto({this.alias, this.descriptionShort, this.description, this.photoUrl, this.year}); + const GameDataDto({ + this.id, + this.title, + this.descriptionShort, + this.description, + this.photoUrl, + this.year, + }); - factory GameAttributesDataDto.fromJson(Map json) => - _$GameAttributesDataDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class MetaDto { - final PaginationDto? pagination; - - const MetaDto({this.pagination}); - - factory MetaDto.fromJson(Map json) => _$MetaDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class PaginationDto { - final int? current; - final int? next; - final int? last; - - const PaginationDto({this.current, this.next, this.last}); - - factory PaginationDto.fromJson(Map json) => _$PaginationDtoFromJson(json); + factory GameDataDto.fromJson(Map json) => + _$GameDataDtoFromJson(json); } diff --git a/lib/data/dtos/games_dto.g.dart b/lib/data/dtos/games_dto.g.dart index 02e9499..25489a6 100644 --- a/lib/data/dtos/games_dto.g.dart +++ b/lib/data/dtos/games_dto.g.dart @@ -6,42 +6,11 @@ part of 'games_dto.dart'; // JsonSerializableGenerator // ************************************************************************** -GamesDto _$GamesDtoFromJson(Map json) => GamesDto( - data: (json['data'] as List?) - ?.map((e) => GameDataDto.fromJson(e as Map)) - .toList(), - meta: json['meta'] == null - ? null - : MetaDto.fromJson(json['meta'] as Map), - ); - GameDataDto _$GameDataDtoFromJson(Map json) => GameDataDto( - id: json['id'] as String?, - attributes: (json['attributes'] as List?) - ?.map( - (e) => GameAttributesDataDto.fromJson(e as Map)) - .toList(), - ); - -GameAttributesDataDto _$GameAttributesDataDtoFromJson( - Map json) => - GameAttributesDataDto( - alias: json['alias'] as String?, + id: (json['id'] as num?)?.toInt(), + title: json['title'] as String?, descriptionShort: json['descriptionShort'] as String?, description: json['description'] as String?, photoUrl: json['photoUrl'] as String?, - year: json['year'] as String?, - ); - -MetaDto _$MetaDtoFromJson(Map json) => MetaDto( - pagination: json['pagination'] == null - ? null - : PaginationDto.fromJson(json['pagination'] as Map), - ); - -PaginationDto _$PaginationDtoFromJson(Map json) => - PaginationDto( - current: (json['current'] as num?)?.toInt(), - next: (json['next'] as num?)?.toInt(), - last: (json['last'] as num?)?.toInt(), + year: (json['year'] as num?)?.toInt(), ); diff --git a/lib/data/mappers/games_mapper.dart b/lib/data/mappers/games_mapper.dart index fba8c0a..fdf9fc3 100644 --- a/lib/data/mappers/games_mapper.dart +++ b/lib/data/mappers/games_mapper.dart @@ -5,22 +5,15 @@ import 'package:flutter_test_app/domain/models/home.dart'; const _imagePlaceholder = 'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png'; -extension GamesDtoToModel on GamesDto { - HomeData toDomain() => HomeData( - data: data?.map((e) => e.toDomain()).toList(), - nextPage: meta?.pagination?.next, - ); -} - extension GameDataDtoToModel on GameDataDto { CardData toDomain() => CardData( - attributes?.alias ?? 'UNKNOWN', - imageUrl: attributes?.photoUrl ?? _imagePlaceholder, - descriptionText: _makeDescriptionText(attributes?.descriptionShort, attributes?.description, attributes?.year), - id: id, + title ?? 'UNKNOWN', + imageUrl: photoUrl ?? _imagePlaceholder, + descriptionText: _makeDescriptionText(descriptionShort, description, year), + id: id.toString(), ); - String _makeDescriptionText(String? descriptionShort, String? description, String? year) { + String _makeDescriptionText(String? descriptionShort, String? description, int? year) { return 'Год издания: $year. Описание: $descriptionShort'; } } diff --git a/lib/data/repositories/game_repository.dart b/lib/data/repositories/game_repository.dart index 18b22d9..d03ba6e 100644 --- a/lib/data/repositories/game_repository.dart +++ b/lib/data/repositories/game_repository.dart @@ -24,16 +24,19 @@ class GameRepository extends ApiInterface { try { const String url = '$_baseUrl/games'; - final Response response = await _dio.get>( + final Response response = await _dio.get>( url, queryParameters: { - 'offset': page, + 'offset': 10, 'limit': pageSize, }, ); - final GamesDto dto = GamesDto.fromJson(response.data as Map); - final HomeData data = dto.toDomain(); + final List dto = (response.data as List) + .map((e) => GameDataDto.fromJson(e)) + .toList(); + final HomeData data = + HomeData(data: dto.map((e) => e.toDomain()).toList()); return data; } on DioException catch (e) { onError?.call(e.error?.toString()); diff --git a/lib/domain/models/card.dart b/lib/domain/models/card.dart index 5d2d9dc..4cd7e71 100644 --- a/lib/domain/models/card.dart +++ b/lib/domain/models/card.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter_test_app/lib/data/dtos/games_dto.dart'; class CardData { final String text; @@ -9,10 +8,10 @@ class CardData { final String? id; CardData( - this.text, { - required this.descriptionText, - this.icon = Icons.ac_unit_outlined, - this.imageUrl, - this.id, - }); + this.text, { + required this.descriptionText, + this.icon = Icons.ac_unit_outlined, + this.imageUrl, + this.id, + }); }