feat: implement API client with JSON handling and error mapping; enhance routing and state management in app shell

This commit is contained in:
Nils-Johan Gynther
2026-04-22 07:29:21 +02:00
parent 82ba334f2d
commit e8de1d3625
12 changed files with 586 additions and 133 deletions
@@ -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();
}
}