kursovaya
This commit is contained in:
parent
cea1545b9a
commit
b0d8c80efd
@ -2,59 +2,24 @@ import 'package:json_annotation/json_annotation.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)
|
||||
class GameDataDto {
|
||||
final String? id;
|
||||
final List<GameAttributesDataDto>? attributes;
|
||||
|
||||
const GameDataDto({this.id, this.attributes});
|
||||
|
||||
factory GameDataDto.fromJson(Map<String, dynamic> 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<String, dynamic> json) =>
|
||||
_$GameAttributesDataDtoFromJson(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);
|
||||
factory GameDataDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$GameDataDtoFromJson(json);
|
||||
}
|
||||
|
@ -6,42 +6,11 @@ part of 'games_dto.dart';
|
||||
// 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(
|
||||
id: json['id'] as String?,
|
||||
attributes: (json['attributes'] as List<dynamic>?)
|
||||
?.map(
|
||||
(e) => GameAttributesDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
GameAttributesDataDto _$GameAttributesDataDtoFromJson(
|
||||
Map<String, dynamic> 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<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(),
|
||||
year: (json['year'] as num?)?.toInt(),
|
||||
);
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
@ -24,16 +24,19 @@ class GameRepository extends ApiInterface {
|
||||
try {
|
||||
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,
|
||||
queryParameters: {
|
||||
'offset': page,
|
||||
'offset': 10,
|
||||
'limit': pageSize,
|
||||
},
|
||||
);
|
||||
|
||||
final GamesDto dto = GamesDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final HomeData data = dto.toDomain();
|
||||
final List<GameDataDto> dto = (response.data as List<dynamic>)
|
||||
.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());
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user