Files
Nils-Johan Gynther 04b1fc3024
Test Suite / test (24.15.0) (push) Has been cancelled
feat: add rematch functionality for recipe ingredients and enhance inventory management
- Added a new API path for rematching recipe ingredients in `api_paths.dart`.
- Implemented a manual product creation dialog in `inventory_screen.dart` to allow users to create new products directly.
- Integrated the rematch functionality in `recipe_repository.dart` to handle rematching of recipe ingredients.
- Updated the recipe detail screen to include a button for triggering the rematch process.
- Introduced a new `RecipeMatchingService` in the backend to handle ingredient matching logic.
- Added database migration to include `aiEngineEnabled` column in the User table.

Co-authored-by: Copilot <copilot@github.com>
2026-05-06 09:20:31 +02:00

242 lines
7.4 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<RecipeAnalysis> rematchRecipeIngredients(int id,
{String? token}) async {
try {
final data = await _api.postJson(
RecipeApiPaths.rematch(id),
body: const {},
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 köra om matchning.');
}
}
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.');
}
}
}