движемся к победе
This commit is contained in:
parent
8421f194c6
commit
0fad387e57
@ -4,34 +4,49 @@ part 'films_dto.g.dart';
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class FilmsDto {
|
||||
final int total;
|
||||
final List<FilmDataDto> items;
|
||||
final int? total; // Может быть null
|
||||
late final List<FilmDataDto>? items; // Может быть null
|
||||
|
||||
const FilmsDto({
|
||||
required this.total,
|
||||
required this.items,
|
||||
// Переопределяем конструктор, чтобы вызывать fillItemsIfEmpty
|
||||
FilmsDto._({
|
||||
this.total, // Может быть null
|
||||
this.items, // Может быть null
|
||||
});
|
||||
|
||||
factory FilmsDto.fromJson(Map<String, dynamic> json) => _$FilmsDtoFromJson(json);
|
||||
factory FilmsDto({
|
||||
int? total, // Может быть null
|
||||
List<FilmDataDto>? items, // Может быть null
|
||||
}) {
|
||||
var filledItems = items ?? [];
|
||||
|
||||
return FilmsDto._(
|
||||
total: total,
|
||||
items: filledItems,
|
||||
);
|
||||
}
|
||||
|
||||
factory FilmsDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$FilmsDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class FilmDataDto {
|
||||
final int? kinopoiskId;
|
||||
final int? kinopoiskId; // Может быть null
|
||||
final String nameRu;
|
||||
final int year;
|
||||
final int? year; // Может быть null
|
||||
final List<GenreDto> genres;
|
||||
final String posterUrl;
|
||||
|
||||
const FilmDataDto({
|
||||
required this.kinopoiskId,
|
||||
this.kinopoiskId, // Может быть null
|
||||
required this.nameRu,
|
||||
required this.year,
|
||||
this.year, // Может быть null
|
||||
required this.genres,
|
||||
required this.posterUrl,
|
||||
});
|
||||
|
||||
factory FilmDataDto.fromJson(Map<String, dynamic> json) => _$FilmDataDtoFromJson(json);
|
||||
factory FilmDataDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$FilmDataDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
@ -40,5 +55,6 @@ class GenreDto {
|
||||
|
||||
const GenreDto({required this.genre});
|
||||
|
||||
factory GenreDto.fromJson(Map<String, dynamic> json) => _$GenreDtoFromJson(json);
|
||||
}
|
||||
factory GenreDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$GenreDtoFromJson(json);
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ part of 'films_dto.dart';
|
||||
// **************************************************************************
|
||||
|
||||
FilmsDto _$FilmsDtoFromJson(Map<String, dynamic> json) => FilmsDto(
|
||||
total: (json['total'] as num).toInt(),
|
||||
items: (json['items'] as List<dynamic>)
|
||||
.map((e) => FilmDataDto.fromJson(e as Map<String, dynamic>))
|
||||
total: (json['total'] as num?)?.toInt(),
|
||||
items: (json['items'] as List<dynamic>?)
|
||||
?.map((e) => FilmDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
FilmDataDto _$FilmDataDtoFromJson(Map<String, dynamic> json) => FilmDataDto(
|
||||
kinopoiskId: (json['kinopoiskId'] as num?)?.toInt(),
|
||||
nameRu: json['nameRu'] as String,
|
||||
year: (json['year'] as num).toInt(),
|
||||
year: (json['year'] as num?)?.toInt(),
|
||||
genres: (json['genres'] as List<dynamic>)
|
||||
.map((e) => GenreDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
|
62
lib/data/dtos/search_dto.dart
Normal file
62
lib/data/dtos/search_dto.dart
Normal file
@ -0,0 +1,62 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'search_dto.g.dart';
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class SearchDto {
|
||||
final int? total; // Может быть null
|
||||
late final List<SearchDataDto>? films;
|
||||
|
||||
|
||||
|
||||
// Переопределяем конструктор, чтобы вызывать fillItemsIfEmpty
|
||||
SearchDto._({
|
||||
this.total, // Может быть null
|
||||
this.films,
|
||||
|
||||
});
|
||||
|
||||
factory SearchDto({
|
||||
int? total, // Может быть null
|
||||
List<SearchDataDto>? films,
|
||||
|
||||
}) {
|
||||
var filledItems = films ?? [];
|
||||
|
||||
return SearchDto._(
|
||||
total: total,
|
||||
films: filledItems,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
factory SearchDto.fromJson(Map<String, dynamic> json) => _$SearchDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class SearchDataDto {
|
||||
final int? kinopoiskId; // Может быть null
|
||||
final String nameRu;
|
||||
final String? year; // Может быть null
|
||||
final List<GenreSearchDto> genres;
|
||||
final String posterUrl;
|
||||
|
||||
const SearchDataDto({
|
||||
this.kinopoiskId, // Может быть null
|
||||
required this.nameRu,
|
||||
this.year, // Может быть null
|
||||
required this.genres,
|
||||
required this.posterUrl,
|
||||
});
|
||||
|
||||
factory SearchDataDto.fromJson(Map<String, dynamic> json) => _$SearchDataDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class GenreSearchDto {
|
||||
final String genre;
|
||||
|
||||
const GenreSearchDto({required this.genre});
|
||||
|
||||
factory GenreSearchDto.fromJson(Map<String, dynamic> json) => _$GenreSearchDtoFromJson(json);
|
||||
}
|
30
lib/data/dtos/search_dto.g.dart
Normal file
30
lib/data/dtos/search_dto.g.dart
Normal file
@ -0,0 +1,30 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'search_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
SearchDto _$SearchDtoFromJson(Map<String, dynamic> json) => SearchDto(
|
||||
total: (json['total'] as num?)?.toInt(),
|
||||
films: (json['films'] as List<dynamic>?)
|
||||
?.map((e) => SearchDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
SearchDataDto _$SearchDataDtoFromJson(Map<String, dynamic> json) =>
|
||||
SearchDataDto(
|
||||
kinopoiskId: (json['kinopoiskId'] as num?)?.toInt(),
|
||||
nameRu: json['nameRu'] as String,
|
||||
year: json['year'] as String?,
|
||||
genres: (json['genres'] as List<dynamic>)
|
||||
.map((e) => GenreSearchDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
posterUrl: json['posterUrl'] as String,
|
||||
);
|
||||
|
||||
GenreSearchDto _$GenreSearchDtoFromJson(Map<String, dynamic> json) =>
|
||||
GenreSearchDto(
|
||||
genre: json['genre'] as String,
|
||||
);
|
@ -2,12 +2,14 @@ import 'package:project1/data/dtos/films_dto.dart';
|
||||
import 'package:project1/domain/models/card.dart';
|
||||
import 'package:project1/domain/models/home.dart';
|
||||
|
||||
import '../dtos/search_dto.dart';
|
||||
|
||||
const _imagePlaceholder =
|
||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||
|
||||
extension FilmsDtoToModel on FilmsDto {
|
||||
HomeData toDomain() => HomeData(
|
||||
data: items.map((e) => e.toDomain()).toList(),
|
||||
data: items?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: null, // Если нужно добавить поддержку пагинации, нужно будет добавить соответствующие поля в HomeData
|
||||
);
|
||||
}
|
||||
@ -29,4 +31,29 @@ extension FilmDataDtoToModel on FilmDataDto {
|
||||
|
||||
extension GenreDtoToModel on GenreDto {
|
||||
String toDomain() => genre;
|
||||
}
|
||||
extension SearchDtoToModel on SearchDto {
|
||||
HomeData toDomain() => HomeData(
|
||||
data: films?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: null, // Если нужно добавить поддержку пагинации, нужно будет добавить соответствующие поля в HomeData
|
||||
);
|
||||
}
|
||||
|
||||
extension SearchDataDtoToModel on SearchDataDto {
|
||||
CardData toDomain() => CardData(
|
||||
nameRu,
|
||||
imageUrl: posterUrl ?? _imagePlaceholder,
|
||||
descriptionText: _makeDescriptionSearchText(year, genres),
|
||||
id: kinopoiskId.toString(),
|
||||
);
|
||||
|
||||
String _makeDescriptionSearchText(String? year, List<GenreSearchDto> genres) {
|
||||
final yearText = year != null ? 'Год выхода: $year\n' : '';
|
||||
final genresText = genres.isNotEmpty ? 'Жанр: ${genres.map((g) => g.genre).join(', ')}' : '';
|
||||
return [yearText, genresText].where((s) => s.isNotEmpty).join();
|
||||
}
|
||||
}
|
||||
|
||||
extension GenreSearchDtoToModel on GenreDto {
|
||||
String toDomain() => genre;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
import 'package:project1/data/dtos/films_dto.dart';
|
||||
import 'package:project1/data/dtos/search_dto.dart';
|
||||
import 'package:project1/data/mappers/films_mapper.dart';
|
||||
import 'package:project1/domain/models/home.dart';
|
||||
|
||||
@ -14,7 +15,7 @@ class FilmsRepository {
|
||||
));
|
||||
|
||||
static const String _baseUrl = 'https://kinopoiskapiunofficial.tech';
|
||||
static const String _apiKey = '67c830e4-b979-48ba-903d-a00c8f96fd4b';
|
||||
static const String _apiKey = 'e6b9a002-9956-465d-af28-a4afa34d7c5d';
|
||||
|
||||
Future<HomeData?> loadData({
|
||||
OnErrorCallback? onError,
|
||||
@ -29,15 +30,14 @@ class FilmsRepository {
|
||||
url = '$_baseUrl/api/v2.1/films/search-by-keyword';
|
||||
}
|
||||
|
||||
final List<String> months = [
|
||||
'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE',
|
||||
'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'
|
||||
];
|
||||
final List<String> months = ['JANUARY'];
|
||||
|
||||
final List<FilmDataDto> allFilms = [];
|
||||
final List<SearchDataDto> someFilms = [];
|
||||
|
||||
for (final month in months) {
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
final Response<dynamic> response =
|
||||
await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: {
|
||||
'year': DateTime.now().year,
|
||||
@ -54,10 +54,30 @@ class FilmsRepository {
|
||||
),
|
||||
);
|
||||
|
||||
final FilmsDto dto = FilmsDto.fromJson(response.data as Map<String, dynamic>);
|
||||
allFilms.addAll(dto.items);
|
||||
}
|
||||
if (response.statusCode == 200) {
|
||||
final FilmsDto dto =
|
||||
FilmsDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final SearchDto dtos =
|
||||
SearchDto.fromJson(response.data as Map<String, dynamic>);
|
||||
|
||||
/*if (dto.items != null) {
|
||||
dto.items = dto.films;
|
||||
}*/
|
||||
// if (dto.items != null) {
|
||||
if (q != null) {
|
||||
someFilms.addAll(dtos.films!);
|
||||
} else {
|
||||
allFilms.addAll(dto.items!);
|
||||
}
|
||||
// }
|
||||
// /*else {
|
||||
// dto.items = dto.films;
|
||||
// }*/
|
||||
} else {
|
||||
onError?.call('Request failed with status: ${response.statusCode}');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
final HomeData data = HomeData(
|
||||
data: allFilms.map((e) => e.toDomain()).toList(),
|
||||
nextPage: page + 1, // Увеличиваем номер страницы для следующего запроса
|
||||
@ -70,6 +90,8 @@ class FilmsRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// зачем этот метод?
|
||||
|
||||
Future<FilmDataDto?> getFilmById(int filmId) async {
|
||||
try {
|
||||
final String url = '$_baseUrl/api/v2.2/films/$filmId';
|
||||
@ -84,11 +106,17 @@ class FilmsRepository {
|
||||
),
|
||||
);
|
||||
|
||||
final FilmDataDto dto = FilmDataDto.fromJson(response.data as Map<String, dynamic>);
|
||||
return dto;
|
||||
if (response.statusCode == 200) {
|
||||
final FilmDataDto dto =
|
||||
FilmDataDto.fromJson(response.data as Map<String, dynamic>);
|
||||
return dto;
|
||||
} else {
|
||||
print('Request failed with status: ${response.statusCode}');
|
||||
return null;
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
print('Request failed with error: ${e.error}');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user