feat: add recipe creation, editing, and detail screens; enhance recipe model with instructions and ingredients
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_exception.dart';
|
||||
import '../domain/parsed_recipe.dart';
|
||||
import '../domain/recipe.dart';
|
||||
|
||||
class RecipeRepository {
|
||||
@@ -10,14 +11,10 @@ class RecipeRepository {
|
||||
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.',
|
||||
);
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar fran servern.');
|
||||
}
|
||||
|
||||
return data
|
||||
.map((e) => Recipe.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
@@ -25,9 +22,91 @@ class RecipeRepository {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network,
|
||||
message: 'Kunde inte hamta recept.',
|
||||
type: ApiErrorType.network, message: 'Kunde inte hamta recept.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Recipe> fetchRecipeDetail(int id, {String? token}) async {
|
||||
try {
|
||||
final data = await _api.getJson('/recipes/$id', token: token);
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar fran servern.');
|
||||
}
|
||||
return Recipe.fromJson(data);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network, message: 'Kunde inte hamta recept.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Recipe> createRecipe(Map<String, dynamic> body,
|
||||
{String? token}) async {
|
||||
try {
|
||||
final data =
|
||||
await _api.postJson('/recipes', body: body, token: token);
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar fran servern.');
|
||||
}
|
||||
return Recipe.fromJson(data);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network, message: 'Kunde inte spara recept.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Recipe> updateRecipe(int id, Map<String, dynamic> body,
|
||||
{String? token}) async {
|
||||
try {
|
||||
final data =
|
||||
await _api.putJson('/recipes/$id', body: body, token: token);
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar fran servern.');
|
||||
}
|
||||
return Recipe.fromJson(data);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network, message: 'Kunde inte uppdatera recept.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteRecipe(int id, {String? token}) async {
|
||||
try {
|
||||
await _api.deleteJson('/recipes/$id', token: token);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network, message: 'Kunde inte ta bort recept.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<ParsedRecipe> parseMarkdown(String markdown,
|
||||
{String? token}) async {
|
||||
try {
|
||||
final data = await _api.postJson(
|
||||
'/recipes/parse-markdown',
|
||||
body: {'markdown': markdown},
|
||||
token: token,
|
||||
);
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar fran servern.');
|
||||
}
|
||||
return ParsedRecipe.fromJson(data);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network, message: 'Kunde inte tolka receptet.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user