PIbd-31_IevlewaMD_PMD/lib/data/dto/album_dto.dart

130 lines
4.0 KiB
Dart
Raw Normal View History

2024-11-08 13:07:43 +04:00
import 'package:json_annotation/json_annotation.dart';
import 'package:uuid/uuid.dart';
@JsonSerializable(createToJson: false)
class AlbumDto {
final List<AlbumDataDto>? data;
2024-11-26 01:44:56 +04:00
final AlbumPaginationDto? pagination; // Добавлено для пагинации
2024-11-08 13:07:43 +04:00
2024-11-26 01:44:56 +04:00
const AlbumDto({this.data, this.pagination});
// Здесь убираем factory и оставляем ваш метод fetchAlbums
// factory AlbumDto.fromJson(Map<String, dynamic> json) => _$AlbumDtoFromJson(json);
// Ваш метод fetchAlbums будет принимать JSON и вызывать конструктор
static AlbumDto fetchAlbums(Map<String, dynamic> json) {
return AlbumDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => AlbumDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
pagination: json['pagination'] != null
? AlbumPaginationDto.fromJson(json['pagination'])
: null,
);
}
2024-11-08 13:07:43 +04:00
}
2024-11-26 01:44:56 +04:00
@JsonSerializable(createToJson: false)
class AlbumPaginationDto {
@JsonKey(name: 'current_page')
final int? currentPage;
@JsonKey(name: 'has_next_page')
final bool? hasNextPage;
@JsonKey(name: 'last_visible_page')
final int? lastVisiblePage;
const AlbumPaginationDto({this.currentPage, this.hasNextPage, this.lastVisiblePage});
// Здесь убираем factory
// factory AlbumPaginationDto.fromJson(Map<String, dynamic> json) => _$AlbumPaginationDtoFromJson(json);
// Метод для конструирования экземпляра
static AlbumPaginationDto fromJson(Map<String, dynamic> json) {
return AlbumPaginationDto(
currentPage: json['current_page'],
hasNextPage: json['has_next_page'],
lastVisiblePage: json['last_visible_page'],
);
}
}
2024-11-08 13:07:43 +04:00
@JsonSerializable(createToJson: false)
class AlbumDataDto {
final String id;
final String? title;
final String? artist;
String? year;
List<String>? genres;
List<String>? tracks;
String? summary;
String? url;
final AlbumDataImagesDto? images;
AlbumDataDto({
String? id,
this.title,
this.artist,
this.year,
this.genres,
this.tracks,
this.summary,
this.url,
this.images,
}) : id = id ?? const Uuid().v4(); // Генерация id
2024-11-26 01:44:56 +04:00
// Здесь убираем factory
// factory AlbumDataDto.fromJson(Map<String, dynamic> json) => _$AlbumDataDtoFromJson(json);
// Метод для конструирования экземпляра
static AlbumDataDto fromJson(Map<String, dynamic> json) {
return AlbumDataDto(
id: json['id'],
title: json['title'],
artist: json['artist'],
year: json['year'],
genres: (json['genres'] as List<dynamic>?)?.map((e) => e as String).toList(),
tracks: (json['tracks'] as List<dynamic>?)?.map((e) => e as String).toList(),
summary: json['summary'],
url: json['url'],
images: json['images'] != null
? AlbumDataImagesDto.fromJson(json['images'])
: null,
);
}
2024-11-08 13:07:43 +04:00
}
@JsonSerializable(createToJson: false)
class AlbumDataImagesDto {
final AlbumDataImagesJPGDto? jpg;
const AlbumDataImagesDto({this.jpg});
2024-11-26 01:44:56 +04:00
// Здесь убираем factory
// factory AlbumDataImagesDto.fromJson(Map<String, dynamic> json) => _$AlbumDataImagesDtoFromJson(json);
// Метод для конструирования экземпляра
static AlbumDataImagesDto fromJson(Map<String, dynamic> json) {
return AlbumDataImagesDto(
jpg: json['jpg'] != null
? AlbumDataImagesJPGDto.fromJson(json['jpg'])
: null,
);
}
2024-11-08 13:07:43 +04:00
}
@JsonSerializable(createToJson: false)
class AlbumDataImagesJPGDto {
final String? image_url;
const AlbumDataImagesJPGDto({this.image_url});
2024-11-26 01:44:56 +04:00
// Здесь убираем factory
// factory AlbumDataImagesJPGDto.fromJson(Map<String, dynamic> json) => _$AlbumDataImagesJPGDtoFromJson(json);
// Метод для конструирования экземпляра
static AlbumDataImagesJPGDto fromJson(Map<String, dynamic> json) {
return AlbumDataImagesJPGDto(
image_url: json['image_url'],
);
}
2024-11-08 13:07:43 +04:00
}