import 'package:dartz/dartz.dart'; import 'package:dio/dio.dart'; import 'package:get/get.dart'; import 'package:snap_and_cook_mobile/data/remote/models/ingredient_model.dart'; import 'package:snap_and_cook_mobile/data/remote/requests/detect_ingredient_request.dart'; import '../../../data/remote/services/recipe_service.dart'; import '../../entities/recipe.dart'; import 'recipe_interface.dart'; class RecipeUseCase implements RecipeInterface { final service = Get.find(); @override Future>> fetchRecipes( CancelToken cancelToken, { required int size, required int currentPage, String? search, }) async { try { final response = await service.getAllRecipes(cancelToken, size, currentPage, search); List recipes = []; response.data?.forEach((element) { recipes.add(element.toEntity()); }); return Right(recipes); } on DioError catch (e) { return Left(e); } } @override Future> fetchDetailRecipe( CancelToken cancelToken, String uuid) async { try { final response = await service.getDetailRecipe(cancelToken, uuid); return Right(response.data?.toEntity() ?? Recipe()); } on DioError catch (e) { return Left(e); } catch (e) { return Left(DioError(requestOptions: RequestOptions(path: ""))); } } @override Future>> fetchRecipeRecommendations( CancelToken cancelToken, List ingredients, List utensils) async { try { final response = await service.getRecipeRecommendation( cancelToken, DetectIngredientRequest( ingredients: ingredients, utensils: utensils)); List recipes = []; response.data?.forEach((element) { recipes.add(element.toEntity()); }); return Right(recipes); } on DioError catch (e) { print("DioError is ${e}"); return Left(e); } catch (e) { print("Error is ${e}"); return Left(DioError(requestOptions: RequestOptions(path: ""))); } } }