Files
food-ai/client/lib/features/menu/menu_service.dart
dbastrikin b38190ff5b feat: add product selection step before meal planning
Inserts a new PlanProductsSheet as step 1 of the planning flow.
Users see their current products as a multi-select checklist (all
selected by default) before choosing the planning mode and dates.

- Empty state explains the benefit and offers "Add products" CTA
  while always allowing "Plan without products" to skip
- Selected product IDs flow through PlanMenuSheet →
  PlanDatePickerSheet → MenuService.generateForDates → backend
- Backend: added ProductIDs field to generate-menu request body;
  uses ListForPromptByIDs when set, ListForPrompt otherwise
- Backend: added Repository.ListForPromptByIDs (filtered SQL query)
- All 12 ARB locale files updated with planProducts* keys

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-23 16:07:28 +02:00

106 lines
3.8 KiB
Dart

import '../../core/api/api_client.dart';
import '../../shared/models/diary_entry.dart';
import '../../shared/models/menu.dart';
import '../../shared/models/shopping_item.dart';
class MenuService {
final ApiClient _client;
MenuService(this._client);
// ── Menu ──────────────────────────────────────────────────
Future<MenuPlan?> getMenu({String? week}) async {
final params = <String, dynamic>{};
if (week != null) params['week'] = week;
final data = await _client.get('/menu', params: params);
// Backend returns {"week_start": "...", "days": null} when no plan exists.
if (data['id'] == null) return null;
return MenuPlan.fromJson(data);
}
Future<MenuPlan> generateMenu({String? week}) async {
final body = <String, dynamic>{};
if (week != null) body['week'] = week;
final data = await _client.post('/ai/generate-menu', data: body);
return MenuPlan.fromJson(data);
}
/// Generates meals for specific [dates] (YYYY-MM-DD) and [mealTypes].
/// When [productIds] is non-empty, only those products are passed to AI.
/// Returns the updated MenuPlan for each affected week.
Future<List<MenuPlan>> generateForDates({
required List<String> dates,
required List<String> mealTypes,
List<String> productIds = const [],
}) async {
final body = <String, dynamic>{
'dates': dates,
'meal_types': mealTypes,
};
if (productIds.isNotEmpty) body['product_ids'] = productIds;
final data = await _client.post('/ai/generate-menu', data: body);
final plans = data['plans'] as List<dynamic>;
return plans
.map((planJson) => MenuPlan.fromJson(planJson as Map<String, dynamic>))
.toList();
}
Future<void> updateMenuItem(String itemId, String recipeId) async {
await _client.put('/menu/items/$itemId', data: {'recipe_id': recipeId});
}
Future<void> deleteMenuItem(String itemId) async {
await _client.deleteVoid('/menu/items/$itemId');
}
// ── Shopping list ─────────────────────────────────────────
Future<List<ShoppingItem>> getShoppingList({String? week}) async {
final params = <String, dynamic>{};
if (week != null) params['week'] = week;
final data = await _client.getList('/shopping-list', params: params);
return data
.map((e) => ShoppingItem.fromJson(e as Map<String, dynamic>))
.toList();
}
Future<List<ShoppingItem>> generateShoppingList({String? week}) async {
final body = <String, dynamic>{};
if (week != null) body['week'] = week;
final data = await _client.postList('/shopping-list/generate', data: body);
return data
.map((e) => ShoppingItem.fromJson(e as Map<String, dynamic>))
.toList();
}
Future<void> toggleShoppingItem(int index, bool checked,
{String? week}) async {
final params = <String, dynamic>{};
if (week != null) params['week'] = week;
await _client.patch(
'/shopping-list/items/$index/check',
data: {'checked': checked},
params: params,
);
}
// ── Diary ─────────────────────────────────────────────────
Future<List<DiaryEntry>> getDiary(String date) async {
final data = await _client.getList('/diary', params: {'date': date});
return data
.map((e) => DiaryEntry.fromJson(e as Map<String, dynamic>))
.toList();
}
Future<DiaryEntry> createDiaryEntry(Map<String, dynamic> body) async {
final data = await _client.post('/diary', data: body);
return DiaryEntry.fromJson(data);
}
Future<void> deleteDiaryEntry(String id) async {
await _client.deleteVoid('/diary/$id');
}
}