feat(my_diets): fixed draft, active and personal diets creation, updating

This commit is contained in:
misha
2026-02-09 10:39:51 +03:00
parent 400cc004ef
commit c91e83adaf
12 changed files with 221 additions and 110 deletions

View File

@@ -11,44 +11,56 @@ class CreateDietRepository {
required String name,
required int duration,
required DietState state,
required String meatType,
required String carbType,
required String vegType,
required String lightType,
required String details,
required File image,
required List<List<List<DailySchedule>>> dailySchedule,
required String? meatType,
required String? carbType,
required String? vegType,
required String? lightType,
required String? details,
required File? image,
required List<List<List<DailySchedule>>>? dailySchedule,
}) {
final dietMap = <String, dynamic>{};
for (final (dayIndex, day) in dailySchedule.indexed) {
for (final (mealIndex, meal) in day.indexed) {
for (final (productIndex, product) in meal.indexed) {
final jsonProduct = product.toJson();
dietMap.addAll({
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][name]': jsonProduct['name'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][portion]': jsonProduct['portion'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][quantity]': jsonProduct['quantity'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][product_nutritional_value_id]': jsonProduct['product_nutritional_value_id'],
});
if (dailySchedule != null) {
for (final (dayIndex, day) in dailySchedule.indexed) {
for (final (mealIndex, meal) in day.indexed) {
for (final (productIndex, product) in meal.indexed) {
final jsonProduct = product.toJson();
dietMap.addAll({
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][name]': jsonProduct['name'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][portion]': jsonProduct['portion'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][quantity]': jsonProduct['quantity'],
'daily_schedule[${dayIndex + 1}][${mealIndex + 1}][$productIndex][product_nutritional_value_id]': jsonProduct['product_nutritional_value_id'],
});
}
}
}
}
if (details != null) {
dietMap.addAll({'description_md': details});
}
dietMap.addAll({
'name': name,
'description_md': details,
'duration': duration,
'type[meat]': meatType,
'type[carb]': carbType,
'type[veg]': vegType,
'type[light]': lightType,
'image': MultipartFile.fromFileSync(
image.path,
filename: image.path.split(Platform.pathSeparator).last,
),
});
if (meatType != null) {
dietMap.addAll({'type[meat]': meatType});
}
final formData = FormData.fromMap(dietMap);
if (carbType != null) {
dietMap.addAll({'type[carb]': carbType});
}
if (vegType != null) {
dietMap.addAll({'type[veg]': vegType});
}
if (lightType != null) {
dietMap.addAll({'type[light]': lightType});
}
if (image != null) {
dietMap.addAll({'image': MultipartFile.fromFileSync(image.path, filename: image.path.split(Platform.pathSeparator).last)});
}
dietMap.addAll({'name': name, 'duration': duration});
final formData = FormData.fromMap(dietMap);
switch (state) {
case DietState.active:
return createDietService.createPublishedDiet(formData: formData);
@@ -133,12 +145,7 @@ class CreateDietRepository {
dietMap.addAll({'type[light]': lightType});
}
if (image != null) {
dietMap.addAll({
'image': MultipartFile.fromFileSync(
image.path,
filename: image.path.split(Platform.pathSeparator).last,
),
});
dietMap.addAll({'image': MultipartFile.fromFileSync(image.path, filename: image.path.split(Platform.pathSeparator).last)});
}
final formData = FormData.fromMap(dietMap);