feat: implement Iteration 2 — product management

Backend:
- migrations/005: add pg_trgm extension + search indexes on ingredient_mappings
- migrations/006: products table with computed expires_at column
- ingredient: add Search method (aliases + ILIKE + trgm) + HTTP handler
- product: full package — model, repository (CRUD + BatchCreate + ListForPrompt), handler
- gemini: add AvailableProducts field to RecipeRequest, include in prompt
- recommendation: add ProductLister interface, load user products for personalised prompts
- server/main: wire ingredient and product handlers with new routes

Flutter:
- models: Product, IngredientMapping with json_serializable
- ProductService: getProducts, createProduct, updateProduct, deleteProduct, searchIngredients
- ProductsNotifier: create/update/delete with optimistic delete
- ProductsScreen: expiring-soon section, normal section, swipe-to-delete, edit bottom sheet
- AddProductScreen: name field with 300ms debounce autocomplete, qty/unit/days fields
- app_router: /products/add route + Badge on Products nav tab showing expiring count
- MainShell converted to ConsumerWidget for badge reactivity

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-02-21 23:22:30 +02:00
parent 0dbda0cd57
commit b9b9e9fe11
20 changed files with 1585 additions and 32 deletions

View File

@@ -0,0 +1,34 @@
import 'package:json_annotation/json_annotation.dart';
part 'ingredient_mapping.g.dart';
@JsonSerializable()
class IngredientMapping {
final String id;
@JsonKey(name: 'canonical_name')
final String canonicalName;
@JsonKey(name: 'canonical_name_ru')
final String? canonicalNameRu;
final String? category;
@JsonKey(name: 'default_unit')
final String? defaultUnit;
@JsonKey(name: 'storage_days')
final int? storageDays;
const IngredientMapping({
required this.id,
required this.canonicalName,
this.canonicalNameRu,
this.category,
this.defaultUnit,
this.storageDays,
});
/// Display name prefers Russian, falls back to canonical English name.
String get displayName => canonicalNameRu ?? canonicalName;
factory IngredientMapping.fromJson(Map<String, dynamic> json) =>
_$IngredientMappingFromJson(json);
Map<String, dynamic> toJson() => _$IngredientMappingToJson(this);
}

View File

@@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'ingredient_mapping.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
IngredientMapping _$IngredientMappingFromJson(Map<String, dynamic> json) =>
IngredientMapping(
id: json['id'] as String,
canonicalName: json['canonical_name'] as String,
canonicalNameRu: json['canonical_name_ru'] as String?,
category: json['category'] as String?,
defaultUnit: json['default_unit'] as String?,
storageDays: (json['storage_days'] as num?)?.toInt(),
);
Map<String, dynamic> _$IngredientMappingToJson(IngredientMapping instance) =>
<String, dynamic>{
'id': instance.id,
'canonical_name': instance.canonicalName,
'canonical_name_ru': instance.canonicalNameRu,
'category': instance.category,
'default_unit': instance.defaultUnit,
'storage_days': instance.storageDays,
};

View File

@@ -0,0 +1,46 @@
import 'package:json_annotation/json_annotation.dart';
part 'product.g.dart';
@JsonSerializable()
class Product {
final String id;
@JsonKey(name: 'user_id')
final String userId;
@JsonKey(name: 'mapping_id')
final String? mappingId;
final String name;
final double quantity;
final String unit;
final String? category;
@JsonKey(name: 'storage_days')
final int storageDays;
@JsonKey(name: 'added_at')
final DateTime addedAt;
@JsonKey(name: 'expires_at')
final DateTime expiresAt;
@JsonKey(name: 'days_left')
final int daysLeft;
@JsonKey(name: 'expiring_soon')
final bool expiringSoon;
const Product({
required this.id,
required this.userId,
this.mappingId,
required this.name,
required this.quantity,
required this.unit,
this.category,
required this.storageDays,
required this.addedAt,
required this.expiresAt,
required this.daysLeft,
required this.expiringSoon,
});
factory Product.fromJson(Map<String, dynamic> json) =>
_$ProductFromJson(json);
Map<String, dynamic> toJson() => _$ProductToJson(this);
}

View File

@@ -0,0 +1,37 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'product.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Product _$ProductFromJson(Map<String, dynamic> json) => Product(
id: json['id'] as String,
userId: json['user_id'] as String,
mappingId: json['mapping_id'] as String?,
name: json['name'] as String,
quantity: (json['quantity'] as num).toDouble(),
unit: json['unit'] as String,
category: json['category'] as String?,
storageDays: (json['storage_days'] as num).toInt(),
addedAt: DateTime.parse(json['added_at'] as String),
expiresAt: DateTime.parse(json['expires_at'] as String),
daysLeft: (json['days_left'] as num).toInt(),
expiringSoon: json['expiring_soon'] as bool,
);
Map<String, dynamic> _$ProductToJson(Product instance) => <String, dynamic>{
'id': instance.id,
'user_id': instance.userId,
'mapping_id': instance.mappingId,
'name': instance.name,
'quantity': instance.quantity,
'unit': instance.unit,
'category': instance.category,
'storage_days': instance.storageDays,
'added_at': instance.addedAt.toIso8601String(),
'expires_at': instance.expiresAt.toIso8601String(),
'days_left': instance.daysLeft,
'expiring_soon': instance.expiringSoon,
};