Files
eatvio_app/lib/features/create_recipe/entities/recipe_create_entity.dart
2025-05-21 17:42:43 +03:00

153 lines
4.0 KiB
Dart

import 'dart:io';
import 'package:eatvio_app/features/create_recipe/models/ingredient.dart';
import 'package:eatvio_app/features/create_recipe/models/recipe_model.dart';
import 'package:eatvio_app/features/create_recipe/models/step.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'recipe_create_entity.freezed.dart';
enum TypeOfRecipe {
firstMeals,
secondMeals,
salads,
snacks,
sweets,
drinks,
other;
@override
String toString() {
switch (this) {
case TypeOfRecipe.firstMeals:
return 'Первые блюда';
case TypeOfRecipe.secondMeals:
return 'Вторые блюда';
case TypeOfRecipe.salads:
return 'Салаты';
case TypeOfRecipe.snacks:
return 'Закуски';
case TypeOfRecipe.sweets:
return 'Сладости';
case TypeOfRecipe.drinks:
return 'Напитки';
case TypeOfRecipe.other:
return 'Другое';
}
}
String toApiType() {
switch (this) {
case TypeOfRecipe.firstMeals:
return 'first_meals';
case TypeOfRecipe.secondMeals:
return 'second_meals';
case TypeOfRecipe.salads:
return 'salads';
case TypeOfRecipe.snacks:
return 'snacks';
case TypeOfRecipe.sweets:
return 'sweets';
case TypeOfRecipe.drinks:
return 'drinks';
case TypeOfRecipe.other:
return 'other';
}
}
static TypeOfRecipe? fromApiType(String value) {
switch (value) {
case 'first_meals':
return TypeOfRecipe.firstMeals;
case 'second_meals':
return TypeOfRecipe.secondMeals;
case 'salads':
return TypeOfRecipe.salads;
case 'snacks':
return TypeOfRecipe.snacks;
case 'sweets':
return TypeOfRecipe.sweets;
case 'drinks':
return TypeOfRecipe.drinks;
case 'other':
return TypeOfRecipe.other;
default:
return null;
}
}
static TypeOfRecipe? fromString(String value) {
switch (value) {
case 'Первые блюда':
return TypeOfRecipe.firstMeals;
case 'Вторые блюда':
return TypeOfRecipe.secondMeals;
case 'Салаты':
return TypeOfRecipe.salads;
case 'Закуски':
return TypeOfRecipe.snacks;
case 'Сладости':
return TypeOfRecipe.sweets;
case 'Напитки':
return TypeOfRecipe.drinks;
case 'Другое':
return TypeOfRecipe.other;
default:
return null;
}
}
}
@freezed
abstract class RecipeCreateEntity with _$RecipeCreateEntity {
const RecipeCreateEntity._();
const factory RecipeCreateEntity({
String? title,
TypeOfRecipe? typeOfRecipe,
int? countOfPortions,
double? energyInOnePortion,
int? totalCookHours,
int? totalCookMinutes,
int? activeCookHours,
int? activeCookMinutes,
String? description,
File? image,
String? videoUrl,
required List<Ingredient> ingredients,
required List<StepEntity> steps,
required List<String> tags,
}) = _RecipeCreateEntity;
RecipeModel toModel() => RecipeModel(
title: title,
type: typeOfRecipe?.toApiType(),
servings: countOfPortions,
energyPerServing: energyInOnePortion?.toInt(),
totalCookingTime: ((totalCookHours ?? 0) * 60) + (totalCookMinutes ?? 0),
activeCookTime: ((activeCookHours ?? 0) * 60) + (activeCookMinutes ?? 0),
description: description,
image: image?.path,
videoUrl: videoUrl,
ingredients: ingredients,
steps: steps.map((step) {
if (step.hasProps()) return Step(image: step.image?.path ?? '', description: step.description ?? '');
}).toList(),
tags: tags,
);
}
@freezed
abstract class StepEntity with _$StepEntity {
const StepEntity._();
const factory StepEntity({
String? description,
File? image,
}) = _StepEntity;
bool hasProps() {
return description != null;
}
}