9fe85a719c
Test Suite / test (24.15.0) (push) Has been cancelled
- Added RecipeAnalysisService to handle recipe ingredient analysis, including methods for checking ingredient availability and calculating quantities. - Introduced new TypeScript definitions for recipe analysis results, including ingredient status and summary. - Created corresponding Dart models for recipe analysis, including RecipeIngredientAnalysis, RecipeAnalysisSummary, and RecipeShoppingCandidate. - Updated Flutter UI to reflect changes in ingredient availability status. - Fixed color opacity issue in recipe image card.
220 lines
6.8 KiB
Dart
220 lines
6.8 KiB
Dart
import '../../../core/api/api_client.dart';
|
|
import '../../../core/api/api_exception.dart';
|
|
import '../../../core/api/api_paths.dart';
|
|
import '../domain/parsed_recipe.dart';
|
|
import '../domain/recipe.dart';
|
|
import '../domain/inventory_preview.dart';
|
|
import '../domain/recipe_analysis.dart';
|
|
|
|
class RecipeRepository {
|
|
final ApiClient _api;
|
|
|
|
RecipeRepository(this._api);
|
|
|
|
Future<List<Recipe>> fetchRecipes({String? token}) async {
|
|
try {
|
|
final dynamic data = await _api.getJson(RecipeApiPaths.list, token: token);
|
|
if (data is! List) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från 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 hämta recept.');
|
|
}
|
|
}
|
|
|
|
Future<Recipe> fetchRecipeDetail(int id, {String? token}) async {
|
|
try {
|
|
final data = await _api.getJson(RecipeApiPaths.detail(id), token: token);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return Recipe.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte hämta recept.');
|
|
}
|
|
}
|
|
|
|
Future<Recipe> createRecipe(Map<String, dynamic> body,
|
|
{String? token}) async {
|
|
try {
|
|
final data =
|
|
await _api.postJson(RecipeApiPaths.list, body: body, token: token);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från 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.patchJson(
|
|
RecipeApiPaths.update(id),
|
|
body: body,
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från 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(RecipeApiPaths.remove(id), token: token);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte ta bort recept.');
|
|
}
|
|
}
|
|
|
|
Future<Recipe> setRecipeVisibility(int id, {required bool isPublic, String? token}) async {
|
|
try {
|
|
final data = await _api.patchJson(
|
|
RecipeApiPaths.setVisibility(id),
|
|
body: {'isPublic': isPublic},
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return Recipe.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte uppdatera synlighet.');
|
|
}
|
|
}
|
|
|
|
Future<Recipe> shareRecipeWithUsername(int id, {required String username, String? token}) async {
|
|
try {
|
|
final data = await _api.postJson(
|
|
RecipeApiPaths.share(id),
|
|
body: {'username': username},
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return Recipe.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte dela receptet.');
|
|
}
|
|
}
|
|
|
|
Future<Recipe> unshareRecipeWithUsername(int id, {required String username, String? token}) async {
|
|
try {
|
|
final data = await _api.deleteJson(
|
|
RecipeApiPaths.unshare(id, username),
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return Recipe.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte ta bort delning.');
|
|
}
|
|
}
|
|
|
|
Future<InventoryPreview> fetchInventoryPreview(int id,
|
|
{String? token}) async {
|
|
try {
|
|
final data = await _api.getJson(
|
|
RecipeApiPaths.inventoryPreview(id),
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return InventoryPreview.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network,
|
|
message: 'Kunde inte hämta inventariestatus.');
|
|
}
|
|
}
|
|
|
|
Future<RecipeAnalysis> fetchRecipeAnalysis(int id,
|
|
{String? token}) async {
|
|
try {
|
|
final data = await _api.getJson(
|
|
RecipeApiPaths.analysis(id),
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return RecipeAnalysis.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network,
|
|
message: 'Kunde inte hämta receptanalys.');
|
|
}
|
|
}
|
|
|
|
Future<ParsedRecipe> parseMarkdown(String markdown,
|
|
{String? token}) async {
|
|
try {
|
|
final data = await _api.postJson(
|
|
RecipeApiPaths.parseMarkdown,
|
|
body: {'markdown': markdown},
|
|
token: token,
|
|
);
|
|
if (data is! Map<String, dynamic>) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
|
}
|
|
return ParsedRecipe.fromJson(data);
|
|
} on ApiException {
|
|
rethrow;
|
|
} catch (_) {
|
|
throw const ApiException(
|
|
type: ApiErrorType.network, message: 'Kunde inte tolka receptet.');
|
|
}
|
|
}
|
|
}
|