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>
This commit is contained in:
22
client/lib/core/api/cuisine_repository.dart
Normal file
22
client/lib/core/api/cuisine_repository.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../auth/auth_provider.dart';
|
||||
import 'api_client.dart';
|
||||
import '../../shared/models/cuisine.dart';
|
||||
|
||||
class CuisineRepository {
|
||||
final ApiClient _api;
|
||||
CuisineRepository(this._api);
|
||||
|
||||
Future<List<Cuisine>> fetchCuisines() async {
|
||||
final data = await _api.get('/cuisines');
|
||||
final List<dynamic> items = data['cuisines'] as List;
|
||||
return items
|
||||
.map((e) => Cuisine.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
final cuisineRepositoryProvider = Provider<CuisineRepository>(
|
||||
(ref) => CuisineRepository(ref.watch(apiClientProvider)),
|
||||
);
|
||||
22
client/lib/core/api/tag_repository.dart
Normal file
22
client/lib/core/api/tag_repository.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../auth/auth_provider.dart';
|
||||
import 'api_client.dart';
|
||||
import '../../shared/models/tag.dart';
|
||||
|
||||
class TagRepository {
|
||||
final ApiClient _api;
|
||||
TagRepository(this._api);
|
||||
|
||||
Future<List<Tag>> fetchTags() async {
|
||||
final data = await _api.get('/tags');
|
||||
final List<dynamic> items = data['tags'] as List;
|
||||
return items
|
||||
.map((e) => Tag.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
final tagRepositoryProvider = Provider<TagRepository>(
|
||||
(ref) => TagRepository(ref.watch(apiClientProvider)),
|
||||
);
|
||||
19
client/lib/core/locale/cuisine_provider.dart
Normal file
19
client/lib/core/locale/cuisine_provider.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../api/cuisine_repository.dart';
|
||||
import '../../shared/models/cuisine.dart';
|
||||
import 'language_provider.dart';
|
||||
|
||||
/// Fetches and caches cuisines with localized names.
|
||||
/// Returns list of [Cuisine] objects.
|
||||
/// Re-fetches automatically when languageProvider changes.
|
||||
final cuisinesProvider = FutureProvider<List<Cuisine>>((ref) {
|
||||
ref.watch(languageProvider); // invalidate when language changes
|
||||
return ref.read(cuisineRepositoryProvider).fetchCuisines();
|
||||
});
|
||||
|
||||
/// Convenience provider that returns a slug → localized name map.
|
||||
final cuisineNamesProvider = FutureProvider<Map<String, String>>((ref) async {
|
||||
final cuisines = await ref.watch(cuisinesProvider.future);
|
||||
return {for (final c in cuisines) c.slug: c.name};
|
||||
});
|
||||
19
client/lib/core/locale/tag_provider.dart
Normal file
19
client/lib/core/locale/tag_provider.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../api/tag_repository.dart';
|
||||
import '../../shared/models/tag.dart';
|
||||
import 'language_provider.dart';
|
||||
|
||||
/// Fetches and caches tags with localized names.
|
||||
/// Returns list of [Tag] objects.
|
||||
/// Re-fetches automatically when languageProvider changes.
|
||||
final tagsProvider = FutureProvider<List<Tag>>((ref) {
|
||||
ref.watch(languageProvider); // invalidate when language changes
|
||||
return ref.read(tagRepositoryProvider).fetchTags();
|
||||
});
|
||||
|
||||
/// Convenience provider that returns a slug → localized name map.
|
||||
final tagNamesProvider = FutureProvider<Map<String, String>>((ref) async {
|
||||
final tags = await ref.watch(tagsProvider.future);
|
||||
return {for (final t in tags) t.slug: t.name};
|
||||
});
|
||||
@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/locale/cuisine_provider.dart';
|
||||
import '../../core/locale/tag_provider.dart';
|
||||
import '../../core/locale/unit_provider.dart';
|
||||
import '../../core/theme/app_colors.dart';
|
||||
import '../../shared/models/recipe.dart';
|
||||
@@ -200,7 +202,7 @@ class _PlaceholderImage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
class _MetaChips extends StatelessWidget {
|
||||
class _MetaChips extends ConsumerWidget {
|
||||
final int? prepTimeMin;
|
||||
final int? cookTimeMin;
|
||||
final String? difficulty;
|
||||
@@ -216,8 +218,9 @@ class _MetaChips extends StatelessWidget {
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final totalMin = (prepTimeMin ?? 0) + (cookTimeMin ?? 0);
|
||||
final cuisineNames = ref.watch(cuisineNamesProvider).valueOrNull ?? {};
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
@@ -227,7 +230,9 @@ class _MetaChips extends StatelessWidget {
|
||||
if (difficulty != null)
|
||||
_Chip(icon: Icons.bar_chart, label: _difficultyLabel(difficulty!)),
|
||||
if (cuisine != null)
|
||||
_Chip(icon: Icons.public, label: _cuisineLabel(cuisine!)),
|
||||
_Chip(
|
||||
icon: Icons.public,
|
||||
label: cuisineNames[cuisine!] ?? cuisine!),
|
||||
if (servings != null)
|
||||
_Chip(icon: Icons.people, label: '$servings порц.'),
|
||||
],
|
||||
@@ -240,15 +245,6 @@ class _MetaChips extends StatelessWidget {
|
||||
'hard' => 'Сложно',
|
||||
_ => d,
|
||||
};
|
||||
|
||||
String _cuisineLabel(String c) => switch (c) {
|
||||
'russian' => 'Русская',
|
||||
'asian' => 'Азиатская',
|
||||
'european' => 'Европейская',
|
||||
'mediterranean' => 'Средиземноморская',
|
||||
'american' => 'Американская',
|
||||
_ => 'Другая',
|
||||
};
|
||||
}
|
||||
|
||||
class _Chip extends StatelessWidget {
|
||||
@@ -347,20 +343,24 @@ class _NutCell extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
class _TagsRow extends StatelessWidget {
|
||||
class _TagsRow extends ConsumerWidget {
|
||||
final List<String> tags;
|
||||
|
||||
const _TagsRow({required this.tags});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final tagNames = ref.watch(tagNamesProvider).valueOrNull ?? {};
|
||||
return Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
children: tags
|
||||
.map(
|
||||
(t) => Chip(
|
||||
label: Text(t, style: const TextStyle(fontSize: 11)),
|
||||
label: Text(
|
||||
tagNames[t] ?? t,
|
||||
style: const TextStyle(fontSize: 11),
|
||||
),
|
||||
backgroundColor: AppColors.primary.withValues(alpha: 0.15),
|
||||
padding: EdgeInsets.zero,
|
||||
visualDensity: VisualDensity.compact,
|
||||
@@ -402,7 +402,7 @@ class _IngredientsSection extends ConsumerWidget {
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: Text(ing.name)),
|
||||
Text(
|
||||
'${_formatAmount(ing.amount)} ${ref.watch(unitsProvider).valueOrNull?[ing.unit] ?? ing.unit}',
|
||||
'${_formatAmount(ing.amount)} ${ref.watch(unitsProvider).valueOrNull?[ing.effectiveUnit] ?? ing.effectiveUnit}',
|
||||
style: const TextStyle(
|
||||
color: AppColors.textSecondary, fontSize: 13),
|
||||
),
|
||||
|
||||
13
client/lib/shared/models/cuisine.dart
Normal file
13
client/lib/shared/models/cuisine.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
class Cuisine {
|
||||
final String slug;
|
||||
final String name;
|
||||
|
||||
const Cuisine({required this.slug, required this.name});
|
||||
|
||||
factory Cuisine.fromJson(Map<String, dynamic> json) {
|
||||
return Cuisine(
|
||||
slug: json['slug'] as String? ?? '',
|
||||
name: json['name'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,9 @@ class DiaryEntry {
|
||||
final double? fatG;
|
||||
final double? carbsG;
|
||||
final String source;
|
||||
final String? dishId;
|
||||
final String? recipeId;
|
||||
final double? portionG;
|
||||
|
||||
const DiaryEntry({
|
||||
required this.id,
|
||||
@@ -22,7 +24,9 @@ class DiaryEntry {
|
||||
this.fatG,
|
||||
this.carbsG,
|
||||
required this.source,
|
||||
this.dishId,
|
||||
this.recipeId,
|
||||
this.portionG,
|
||||
});
|
||||
|
||||
factory DiaryEntry.fromJson(Map<String, dynamic> json) {
|
||||
@@ -37,7 +41,9 @@ class DiaryEntry {
|
||||
fatG: (json['fat_g'] as num?)?.toDouble(),
|
||||
carbsG: (json['carbs_g'] as num?)?.toDouble(),
|
||||
source: json['source'] as String? ?? 'manual',
|
||||
dishId: json['dish_id'] as String?,
|
||||
recipeId: json['recipe_id'] as String?,
|
||||
portionG: (json['portion_g'] as num?)?.toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,14 +59,25 @@ class Recipe {
|
||||
class RecipeIngredient {
|
||||
final String name;
|
||||
final double amount;
|
||||
|
||||
/// Unit from Gemini recommendations (free-form string).
|
||||
@JsonKey(defaultValue: '')
|
||||
final String unit;
|
||||
|
||||
/// Unit code from the DB-backed saved recipes endpoint.
|
||||
@JsonKey(name: 'unit_code')
|
||||
final String? unitCode;
|
||||
|
||||
const RecipeIngredient({
|
||||
required this.name,
|
||||
required this.amount,
|
||||
required this.unit,
|
||||
this.unit = '',
|
||||
this.unitCode,
|
||||
});
|
||||
|
||||
/// Returns the best available unit identifier for display / lookup.
|
||||
String get effectiveUnit => unitCode ?? unit;
|
||||
|
||||
factory RecipeIngredient.fromJson(Map<String, dynamic> json) =>
|
||||
_$RecipeIngredientFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$RecipeIngredientToJson(this);
|
||||
|
||||
@@ -55,7 +55,8 @@ RecipeIngredient _$RecipeIngredientFromJson(Map<String, dynamic> json) =>
|
||||
RecipeIngredient(
|
||||
name: json['name'] as String,
|
||||
amount: (json['amount'] as num).toDouble(),
|
||||
unit: json['unit'] as String,
|
||||
unit: json['unit'] as String? ?? '',
|
||||
unitCode: json['unit_code'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$RecipeIngredientToJson(RecipeIngredient instance) =>
|
||||
@@ -63,6 +64,7 @@ Map<String, dynamic> _$RecipeIngredientToJson(RecipeIngredient instance) =>
|
||||
'name': instance.name,
|
||||
'amount': instance.amount,
|
||||
'unit': instance.unit,
|
||||
'unit_code': instance.unitCode,
|
||||
};
|
||||
|
||||
RecipeStep _$RecipeStepFromJson(Map<String, dynamic> json) => RecipeStep(
|
||||
|
||||
@@ -1,47 +1,36 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
import 'recipe.dart';
|
||||
|
||||
part 'saved_recipe.g.dart';
|
||||
|
||||
@JsonSerializable(explicitToJson: true)
|
||||
/// 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;
|
||||
|
||||
@JsonKey(name: 'prep_time_min')
|
||||
final int? prepTimeMin;
|
||||
|
||||
@JsonKey(name: 'cook_time_min')
|
||||
final int? cookTimeMin;
|
||||
|
||||
final int? servings;
|
||||
|
||||
@JsonKey(name: 'image_url')
|
||||
final String? imageUrl;
|
||||
|
||||
@JsonKey(defaultValue: [])
|
||||
final List<RecipeIngredient> ingredients;
|
||||
|
||||
@JsonKey(defaultValue: [])
|
||||
final List<RecipeStep> steps;
|
||||
|
||||
@JsonKey(defaultValue: [])
|
||||
final List<String> tags;
|
||||
|
||||
@JsonKey(name: 'nutrition_per_serving')
|
||||
final NutritionInfo? nutrition;
|
||||
|
||||
final String source;
|
||||
|
||||
@JsonKey(name: 'saved_at')
|
||||
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,
|
||||
@@ -53,12 +42,81 @@ class SavedRecipe {
|
||||
this.ingredients = const [],
|
||||
this.steps = const [],
|
||||
this.tags = const [],
|
||||
this.nutrition,
|
||||
required this.source,
|
||||
required this.savedAt,
|
||||
this.caloriesPerServing,
|
||||
this.proteinPerServing,
|
||||
this.fatPerServing,
|
||||
this.carbsPerServing,
|
||||
});
|
||||
|
||||
factory SavedRecipe.fromJson(Map<String, dynamic> json) =>
|
||||
_$SavedRecipeFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$SavedRecipeToJson(this);
|
||||
/// 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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,57 +1,3 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'saved_recipe.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
SavedRecipe _$SavedRecipeFromJson(Map<String, dynamic> json) => SavedRecipe(
|
||||
id: json['id'] as String,
|
||||
title: json['title'] as String,
|
||||
description: json['description'] as String?,
|
||||
cuisine: json['cuisine'] 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() ?? [],
|
||||
nutrition: json['nutrition_per_serving'] == null
|
||||
? null
|
||||
: NutritionInfo.fromJson(
|
||||
json['nutrition_per_serving'] as Map<String, dynamic>,
|
||||
),
|
||||
source: json['source'] as String,
|
||||
savedAt: DateTime.parse(json['saved_at'] as String),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SavedRecipeToJson(SavedRecipe instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'title': instance.title,
|
||||
'description': instance.description,
|
||||
'cuisine': instance.cuisine,
|
||||
'difficulty': instance.difficulty,
|
||||
'prep_time_min': instance.prepTimeMin,
|
||||
'cook_time_min': instance.cookTimeMin,
|
||||
'servings': instance.servings,
|
||||
'image_url': instance.imageUrl,
|
||||
'ingredients': instance.ingredients.map((e) => e.toJson()).toList(),
|
||||
'steps': instance.steps.map((e) => e.toJson()).toList(),
|
||||
'tags': instance.tags,
|
||||
'nutrition_per_serving': instance.nutrition?.toJson(),
|
||||
'source': instance.source,
|
||||
'saved_at': instance.savedAt.toIso8601String(),
|
||||
};
|
||||
// This file is intentionally empty.
|
||||
// saved_recipe.dart now uses a manually written fromJson/toJson.
|
||||
|
||||
13
client/lib/shared/models/tag.dart
Normal file
13
client/lib/shared/models/tag.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
class Tag {
|
||||
final String slug;
|
||||
final String name;
|
||||
|
||||
const Tag({required this.slug, required this.name});
|
||||
|
||||
factory Tag.fromJson(Map<String, dynamic> json) {
|
||||
return Tag(
|
||||
slug: json['slug'] as String? ?? '',
|
||||
name: json['name'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user