Files
food-ai/client/lib/shared/models/saved_recipe.dart
dbastrikin 61feb91bba feat: core schema redesign — dishes, structured recipes, cuisines, tags (iteration 7)
Replaces the flat JSONB-based recipe schema with a normalized relational model:

Schema (migrations consolidated to 001_initial_schema + 002_seed_data):
- New: dishes, dish_translations, dish_tags — canonical dish catalog
- New: cuisines, tags, dish_categories with _translations tables + full seed data
- New: recipe_ingredients, recipe_steps with _translations (replaces JSONB blobs)
- New: user_saved_recipes thin bookmark (drops saved_recipes + saved_recipe_translations)
- New: product_ingredients M2M table
- recipes: now a cooking variant of a dish (dish_id FK, no title/JSONB columns)
- recipe_translations: repurposed to per-language notes only
- products: mapping_id → primary_ingredient_id
- menu_items: recipe_id FK → recipes; adds dish_id
- meal_diary: adds dish_id, recipe_id → recipes, portion_g

Backend (Go):
- New packages: internal/cuisine, internal/tag, internal/dish (registry + handler + repo)
- New GET /cuisines, GET /tags (public), GET /dishes, GET /dishes/{id}, GET /recipes/{id}
- recipe, savedrecipe, menu, diary, product, ingredient packages updated for new schema

Flutter:
- New models: Cuisine, Tag; new providers: cuisineNamesProvider, tagNamesProvider
- recipe.dart: RecipeIngredient gains unit_code + effectiveUnit getter
- saved_recipe.dart: thin model, manual fromJson, computed nutrition getter
- diary_entry.dart: adds dishId, recipeId, portionG
- recipe_detail_screen.dart: localized cuisine/tag names via providers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 18:01:24 +02:00

123 lines
3.8 KiB
Dart

import 'recipe.dart';
/// A user's bookmarked recipe. Display fields are joined from dishes + recipes.
class SavedRecipe {
final String id;
final String recipeId;
final String title;
final String? description;
/// Mapped from cuisine_slug in the API response.
final String? cuisine;
final String? difficulty;
final int? prepTimeMin;
final int? cookTimeMin;
final int? servings;
final String? imageUrl;
final List<RecipeIngredient> ingredients;
final List<RecipeStep> steps;
final List<String> tags;
final DateTime savedAt;
// Individual nutrition columns (nullable).
final double? caloriesPerServing;
final double? proteinPerServing;
final double? fatPerServing;
final double? carbsPerServing;
const SavedRecipe({
required this.id,
required this.recipeId,
required this.title,
this.description,
this.cuisine,
this.difficulty,
this.prepTimeMin,
this.cookTimeMin,
this.servings,
this.imageUrl,
this.ingredients = const [],
this.steps = const [],
this.tags = const [],
required this.savedAt,
this.caloriesPerServing,
this.proteinPerServing,
this.fatPerServing,
this.carbsPerServing,
});
/// Builds a [NutritionInfo] from the individual per-serving columns,
/// or null when no calorie data is available.
NutritionInfo? get nutrition {
if (caloriesPerServing == null) return null;
return NutritionInfo(
calories: caloriesPerServing!,
proteinG: proteinPerServing ?? 0,
fatG: fatPerServing ?? 0,
carbsG: carbsPerServing ?? 0,
approximate: false,
);
}
factory SavedRecipe.fromJson(Map<String, dynamic> json) {
return SavedRecipe(
id: json['id'] as String? ?? '',
recipeId: json['recipe_id'] as String? ?? '',
title: json['title'] as String? ?? '',
description: json['description'] as String?,
cuisine: json['cuisine_slug'] as String?,
difficulty: json['difficulty'] as String?,
prepTimeMin: (json['prep_time_min'] as num?)?.toInt(),
cookTimeMin: (json['cook_time_min'] as num?)?.toInt(),
servings: (json['servings'] as num?)?.toInt(),
imageUrl: json['image_url'] as String?,
ingredients: (json['ingredients'] as List<dynamic>?)
?.map((e) =>
RecipeIngredient.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
steps: (json['steps'] as List<dynamic>?)
?.map(
(e) => RecipeStep.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
tags: (json['tags'] as List<dynamic>?)
?.map((e) => e as String)
.toList() ??
[],
savedAt: DateTime.tryParse(json['saved_at'] as String? ?? '') ??
DateTime.now(),
caloriesPerServing:
(json['calories_per_serving'] as num?)?.toDouble(),
proteinPerServing:
(json['protein_per_serving'] as num?)?.toDouble(),
fatPerServing: (json['fat_per_serving'] as num?)?.toDouble(),
carbsPerServing: (json['carbs_per_serving'] as num?)?.toDouble(),
);
}
Map<String, dynamic> toJson() => {
'id': id,
'recipe_id': recipeId,
'title': title,
'description': description,
'cuisine_slug': cuisine,
'difficulty': difficulty,
'prep_time_min': prepTimeMin,
'cook_time_min': cookTimeMin,
'servings': servings,
'image_url': imageUrl,
'ingredients': ingredients.map((e) => e.toJson()).toList(),
'steps': steps.map((e) => e.toJson()).toList(),
'tags': tags,
'saved_at': savedAt.toIso8601String(),
'calories_per_serving': caloriesPerServing,
'protein_per_serving': proteinPerServing,
'fat_per_serving': fatPerServing,
'carbs_per_serving': carbsPerServing,
};
}