pmu/lib/repositories/CocktailRepository.dart
2024-11-01 18:41:53 +04:00

33 lines
1.2 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_app/data/mappers/%D1%81ocktail_mapper.dart';
import 'package:flutter_app/domain/models/card.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../data/dtos/cocktailss_dto.dart';
import 'api_interface.dart';
class CocktailRepository 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
Future<List<CardData>?> loadData(String? q) async {
try {
final String url = q != null
? '$_baseUrl/search.php?s=$q' // Поиск по названию
: '$_baseUrl/filter.php?c=Ordinary_Drink'; // Получение списка коктейлей по категории
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
final CocktailssDto dto = CocktailssDto.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;
}
}
}