laba5_i_do_this

This commit is contained in:
Extrimal 2024-11-01 19:37:26 +04:00
parent 1624e47439
commit 46ac39cceb
7 changed files with 26 additions and 68 deletions

View File

@ -2,18 +2,19 @@ import 'package:json_annotation/json_annotation.dart';
part 'bars_dto.g.dart';
@JsonSerializable(createToJson: false)
class BarsDto {
final List<BarDataDto>? data;
final List<BarDataDto>? drinks;
const BarsDto({this.data});
const BarsDto({this.drinks});
factory BarsDto.fromJson(Map<String, dynamic> json) => _$BarsDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class BarDataDto {
final String? coctailId;
final String? coctailName;
final String? coctailImage;
final String? idDrink;
final String? strDrink;
final String? strDrinkThumb;
const BarDataDto({this.coctailId, this.coctailName, this.coctailImage});
const BarDataDto({this.idDrink, this.strDrink, this.strDrinkThumb});
factory BarDataDto.fromJson(Map<String, dynamic> json) => _$BarDataDtoFromJson(json);
}

View File

@ -7,13 +7,13 @@ part of 'bars_dto.dart';
// **************************************************************************
BarsDto _$BarsDtoFromJson(Map<String, dynamic> json) => BarsDto(
data: (json['data'] as List<dynamic>?)
drinks: (json['drinks'] as List<dynamic>?)
?.map((e) => BarDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
BarDataDto _$BarDataDtoFromJson(Map<String, dynamic> json) => BarDataDto(
coctailId: json['coctailId'] as String?,
coctailName: json['coctailName'] as String?,
coctailImage: json['coctailImage'] as String?,
idDrink: json['idDrink'] as String?,
strDrink: json['strDrink'] as String?,
strDrinkThumb: json['strDrinkThumb'] as String?,
);

View File

@ -3,8 +3,8 @@ import 'package:mobile_app/domain/models/card.dart';
extension BarDataDtoToModel on BarDataDto{
CardData toDomain() => CardData(
coctailName ?? 'UNKNOWN',
coctailId: coctailId ?? 'UNKNOWN',
coctailImage: coctailImage ?? 'UNKNOWN',
strDrink ?? 'UNKNOWN',
coctailId: idDrink ?? 'UNKNOWN',
coctailImage: strDrinkThumb,
);
}

View File

@ -4,11 +4,11 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class CardData {
final String coctailName;
final String coctailId;
final String coctailImage;
final String? coctailImage;
CardData(
this.coctailName, {
required this.coctailId,
required this.coctailImage
this.coctailImage
});
}

View File

@ -5,8 +5,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:mobile_app/domain/models/card.dart';
import 'package:mobile_app/presentation/details_page/details_page.dart';
import 'package:mobile_app/repositories/mock_repository.dart';
import 'package:mobile_app/repositories/potter_repository.dart';
import 'package:mobile_app/repositories/coctel_repository.dart';
import 'package:mobile_app/repositories/bar_repository.dart';
part 'card.dart';
class MyHomePage extends StatefulWidget {
@ -43,7 +42,7 @@ class Body extends StatefulWidget {
class _BodyState extends State<Body> {
late TextEditingController searchController;
late Future<List<CardData>?> data;
final CocktailRepository repo = CocktailRepository();
final BarRepository repo = BarRepository();
@override
void initState() {

View File

@ -1,15 +1,17 @@
import 'package:dio/dio.dart';
import 'package:mobile_app/domain/models/card.dart';
import 'package:mobile_app/data/mappers/bar_mapper.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../data/dtos/bars_dto.dart';
import '../domain/models/card.dart';
import 'api_interface.dart';
class CocktailRepository extends ApiInterface {
class BarRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://www.thecocktaildb.com/api/json/v1/1';
@override
@ -21,17 +23,9 @@ class CocktailRepository extends ApiInterface {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
if (response.data != null && response.data['drinks'] != null) {
final List<CardData> data = (response.data['drinks'] as List).map((drink) {
return CardData(
drink['strDrink'], // Название коктейля
coctailId: drink['idDrink'], // ID коктейля
coctailImage: drink['strDrinkThumb'],
);
}).toList();
return data;
}
return null;
final BarsDto dto = BarsDto.fromJson(response.data as Map<String, dynamic>);
final List<CardData>? data = dto.drinks?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e) {
return null;
}

View File

@ -1,36 +0,0 @@
import 'package:dio/dio.dart';
import 'package:mobile_app/data/mappers/bars_mapper.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../data/dtos/bars_dto.dart';
import '../domain/models/card.dart';
import 'api_interface.dart';
class PotterRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://api.potterdb.com';
@override
Future<List<CardData>?> loadData(String? q) async {
try {
const String url = '$_baseUrl/v1/characters';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {"filter[name_cont]": q} : null,
);
final BarsDto dto = BarsDto.fromJson(
response.data as Map<String, dynamic>);
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e) {
return null;
}
}
}