Files
recipe-app/flutter/lib/features/recipes/data/recipe_repository.dart
T

34 lines
841 B
Dart

import '../../../core/api/api_client.dart';
import '../../../core/api/api_exception.dart';
import '../domain/recipe.dart';
class RecipeRepository {
final ApiClient _api;
RecipeRepository(this._api);
Future<List<Recipe>> fetchRecipes({String? token}) async {
try {
final data = await _api.getJson('/recipes', token: token);
if (data is! List) {
throw const ApiException(
type: ApiErrorType.unknown,
message: 'Ogiltigt svar fran servern.',
);
}
return data
.map((e) => Recipe.fromJson(e as Map<String, dynamic>))
.toList();
} on ApiException {
rethrow;
} catch (_) {
throw const ApiException(
type: ApiErrorType.network,
message: 'Kunde inte hamta recept.',
);
}
}
}