Files
eatvio_app/lib/features/recipe/models/my_recipe_model.dart

110 lines
3.6 KiB
Dart

import 'package:eatvio_app/features/create_recipe/models/recipe_model.dart';
import 'package:eatvio_app/features/create_recipe/models/step.dart' as cr_step;
import 'package:eatvio_app/features/create_recipe/models/ingredient.dart' as cr_ingredient;
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'my_recipe_model.g.dart';
part 'my_recipe_model.freezed.dart';
@freezed
abstract class MyRecipePaginationModel with _$MyRecipePaginationModel {
const factory MyRecipePaginationModel({
required List<MyRecipeModel> data,
}) = _MyRecipePaginationModel;
factory MyRecipePaginationModel.fromJson(Map<String, Object?> json) => _$MyRecipePaginationModelFromJson(json);
}
@freezed
abstract class MyRecipeModel with _$MyRecipeModel {
const factory MyRecipeModel({
required int id,
required String image,
required String state,
@JsonKey(name: 'cooking_time') required int cookingTime,
@JsonKey(name: 'active_time') required int activeCookingTime,
@JsonKey(name: 'description_md') required String description,
required List<Step>? steps,
@JsonKey(name: 'video_url') required String? videoUrl,
required String? title,
required String slug,
@JsonKey(name: 'energy_per_serving') required double calories,
@JsonKey(name: 'total_likes') required int likesCount,
@JsonKey(name: 'total_views') required int viewsCount,
}) = _MyRecipeModel;
factory MyRecipeModel.fromJson(Map<String, Object?> json) => _$MyRecipeModelFromJson(json);
}
extension MyRecipeModelX on MyRecipeModel {
RecipeModel toRecipeModel() {
return RecipeModel(
title: title,
description: description,
image: image,
totalCookingTime: cookingTime,
activeCookTime: activeCookingTime,
energyPerServing: calories.toInt(),
videoUrl: videoUrl,
// The API expects 'active' to publish
state: 'active',
steps: steps?.map((e) => cr_step.Step(image: e.image, description: e.description)).toList().cast<cr_step.Step?>(),
// For My Recipes we don't have ingredients directly accessible in this basic model,
// but when it gets sent to publish, RecipeRepository merges properties correctly if available.
ingredients: [],
tags: [],
);
}
Recipe toRecipe() {
return Recipe(
id: id,
image: image,
state: state,
cookingTime: cookingTime,
activeCookingTime: activeCookingTime,
ingredients: [],
description: description,
steps: steps,
videoUrl: videoUrl,
title: title,
slug: slug,
calories: calories,
likesCount: likesCount,
viewsCount: viewsCount,
type: 'recipe',
commentsCount: 0,
wasLikedByMe: false,
isFavorite: false,
tags: [],
author: const Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: true),
);
}
}
extension RecipeToRecipeModelX on Recipe {
RecipeModel toRecipeModel() {
return RecipeModel(
title: title,
description: description,
image: image,
totalCookingTime: cookingTime,
activeCookTime: activeCookingTime,
energyPerServing: calories.toInt(),
videoUrl: videoUrl,
state: 'active',
steps: steps?.map((e) => cr_step.Step(image: e.image, description: e.description)).toList(),
ingredients: ingredients?.map((e) => cr_ingredient.Ingredient(
name: e.name,
portion: e.portion,
unit: e.unit ?? '',
productId: e.productId,
)).toList() ?? [],
tags: tags.map((e) => e.name).toList(),
);
}
}