feat: implement API client with JSON handling and error mapping; enhance routing and state management in app shell
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_exception.dart';
|
||||
import '../domain/recipe.dart';
|
||||
|
||||
class RecipeRepository {
|
||||
@@ -8,13 +8,26 @@ class RecipeRepository {
|
||||
RecipeRepository(this._api);
|
||||
|
||||
Future<List<Recipe>> fetchRecipes({String? token}) async {
|
||||
final response = await _api.get('/recipes', token: token);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Failed to load recipes: ${response.statusCode}');
|
||||
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.',
|
||||
);
|
||||
}
|
||||
final List<dynamic> data = jsonDecode(response.body) as List<dynamic>;
|
||||
return data
|
||||
.map((e) => Recipe.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user