add recipe managment
This commit is contained in:
@@ -2,12 +2,13 @@
|
|||||||
{
|
{
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Category;
|
using Francesco.Recipes.World.Models.BackendModels.Category;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Unit;
|
|
||||||
|
|
||||||
public interface IRecipeRepository
|
public interface IRecipeRepository
|
||||||
{
|
{
|
||||||
Task<Recipe> GetRecipeAsync(Guid recipeId);
|
Task<Recipe> GetRecipeAsync(Guid recipeId);
|
||||||
|
|
||||||
|
Task<Recipe?> GetRecipeByIdAsync(Guid id);
|
||||||
|
|
||||||
Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId);
|
Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId);
|
||||||
|
|
||||||
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
|
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
|
||||||
@@ -16,8 +17,6 @@
|
|||||||
|
|
||||||
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
|
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
|
||||||
|
|
||||||
Task<Unit> AddUnitToRecipeAsync(string name, string symbol);
|
|
||||||
|
|
||||||
Task<Recipe> CreateRecipeForCategoryAsync(Category category, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime);
|
Task<Recipe> CreateRecipeForCategoryAsync(Category category, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
|
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
|
using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Unit;
|
|
||||||
using Francesco.Recipes.World.Repositories.Ingredient;
|
using Francesco.Recipes.World.Repositories.Ingredient;
|
||||||
using Francesco.Recipes.World.Repositories.Unit;
|
using Francesco.Recipes.World.Repositories.Unit;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -30,6 +29,17 @@
|
|||||||
return recipe ?? throw new InvalidDataException($"Address {recipeId} not found.");
|
return recipe ?? throw new InvalidDataException($"Address {recipeId} not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Recipe?> GetRecipeByIdAsync(Guid recipeId)
|
||||||
|
{
|
||||||
|
return await _context.Recipes
|
||||||
|
.Include(r => r.RecipeIngredients)
|
||||||
|
.ThenInclude(ri => ri.Ingredient)
|
||||||
|
.Include(r => r.RecipeIngredients)
|
||||||
|
.ThenInclude(ri => ri.Unit)
|
||||||
|
.Include(r => r.MediaFiles)
|
||||||
|
.FirstOrDefaultAsync(r => r.Id == recipeId);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId)
|
public async Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId)
|
||||||
{
|
{
|
||||||
var recipe = await GetRecipeAsync(recipeId);
|
var recipe = await GetRecipeAsync(recipeId);
|
||||||
@@ -161,15 +171,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
||||||
|
{
|
||||||
|
if (!difficulty.HasValue)
|
||||||
{
|
{
|
||||||
return await _context.Recipes
|
return await _context.Recipes
|
||||||
.Where(r => r.Difficulty == difficulty)
|
.Include(r => r.Category)
|
||||||
|
.Include(r => r.RecipeIngredients)
|
||||||
|
.ThenInclude(ri => ri.Ingredient)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Unit> AddUnitToRecipeAsync(string name, string symbol)
|
return await _context.Recipes
|
||||||
{
|
.Where(r => r.Difficulty == difficulty.Value)
|
||||||
return await _unitRepository.AddUnitAsync(name, symbol);
|
.Include(r => r.Category)
|
||||||
|
.Include(r => r.RecipeIngredients)
|
||||||
|
.ThenInclude(ri => ri.Ingredient)
|
||||||
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user