kursovaya

This commit is contained in:
elena 2024-12-21 16:19:27 +04:00
parent cea1545b9a
commit b0d8c80efd
5 changed files with 34 additions and 105 deletions

View File

@ -2,59 +2,24 @@ import 'package:json_annotation/json_annotation.dart';
part 'games_dto.g.dart'; part 'games_dto.g.dart';
@JsonSerializable(createToJson: false)
class GamesDto {
final List<GameDataDto>? data;
final MetaDto? meta;
const GamesDto({
this.data,
this.meta,
});
factory GamesDto.fromJson(Map<String, dynamic> json) => _$GamesDtoFromJson(json);
}
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class GameDataDto { class GameDataDto {
final String? id; final int? id;
final List<GameAttributesDataDto>? attributes; final String? title;
const GameDataDto({this.id, this.attributes});
factory GameDataDto.fromJson(Map<String, dynamic> json) => _$GameDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class GameAttributesDataDto {
final String? alias;
final String? descriptionShort; final String? descriptionShort;
final String? description; final String? description;
final String? photoUrl; 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<String, dynamic> json) => factory GameDataDto.fromJson(Map<String, dynamic> json) =>
_$GameAttributesDataDtoFromJson(json); _$GameDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> 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<String, dynamic> json) => _$PaginationDtoFromJson(json);
} }

View File

@ -6,42 +6,11 @@ part of 'games_dto.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
GamesDto _$GamesDtoFromJson(Map<String, dynamic> json) => GamesDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => GameDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
meta: json['meta'] == null
? null
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
);
GameDataDto _$GameDataDtoFromJson(Map<String, dynamic> json) => GameDataDto( GameDataDto _$GameDataDtoFromJson(Map<String, dynamic> json) => GameDataDto(
id: json['id'] as String?, id: (json['id'] as num?)?.toInt(),
attributes: (json['attributes'] as List<dynamic>?) title: json['title'] as String?,
?.map(
(e) => GameAttributesDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
GameAttributesDataDto _$GameAttributesDataDtoFromJson(
Map<String, dynamic> json) =>
GameAttributesDataDto(
alias: json['alias'] as String?,
descriptionShort: json['descriptionShort'] as String?, descriptionShort: json['descriptionShort'] as String?,
description: json['description'] as String?, description: json['description'] as String?,
photoUrl: json['photoUrl'] as String?, photoUrl: json['photoUrl'] as String?,
year: json['year'] as String?, year: (json['year'] as num?)?.toInt(),
);
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
pagination: json['pagination'] == null
? null
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
);
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
PaginationDto(
current: (json['current'] as num?)?.toInt(),
next: (json['next'] as num?)?.toInt(),
last: (json['last'] as num?)?.toInt(),
); );

View File

@ -5,22 +5,15 @@ import 'package:flutter_test_app/domain/models/home.dart';
const _imagePlaceholder = const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png'; '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 { extension GameDataDtoToModel on GameDataDto {
CardData toDomain() => CardData( CardData toDomain() => CardData(
attributes?.alias ?? 'UNKNOWN', title ?? 'UNKNOWN',
imageUrl: attributes?.photoUrl ?? _imagePlaceholder, imageUrl: photoUrl ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(attributes?.descriptionShort, attributes?.description, attributes?.year), descriptionText: _makeDescriptionText(descriptionShort, description, year),
id: id, id: id.toString(),
); );
String _makeDescriptionText(String? descriptionShort, String? description, String? year) { String _makeDescriptionText(String? descriptionShort, String? description, int? year) {
return 'Год издания: $year. Описание: $descriptionShort'; return 'Год издания: $year. Описание: $descriptionShort';
} }
} }

View File

@ -24,16 +24,19 @@ class GameRepository extends ApiInterface {
try { try {
const String url = '$_baseUrl/games'; const String url = '$_baseUrl/games';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>( final Response<dynamic> response = await _dio.get<List<dynamic>>(
url, url,
queryParameters: { queryParameters: {
'offset': page, 'offset': 10,
'limit': pageSize, 'limit': pageSize,
}, },
); );
final GamesDto dto = GamesDto.fromJson(response.data as Map<String, dynamic>); final List<GameDataDto> dto = (response.data as List<dynamic>)
final HomeData data = dto.toDomain(); .map((e) => GameDataDto.fromJson(e))
.toList();
final HomeData data =
HomeData(data: dto.map((e) => e.toDomain()).toList());
return data; return data;
} on DioException catch (e) { } on DioException catch (e) {
onError?.call(e.error?.toString()); onError?.call(e.error?.toString());

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test_app/lib/data/dtos/games_dto.dart';
class CardData { class CardData {
final String text; final String text;