100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
using API.DBAccess;
|
|
using API.Models.RecipeModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace API.BusinessLogic
|
|
{
|
|
public class RecipeLogic
|
|
{
|
|
private readonly RecipeDBAccess _dbAccess;
|
|
|
|
public RecipeLogic(RecipeDBAccess dbAccess)
|
|
{
|
|
_dbAccess = dbAccess;
|
|
}
|
|
|
|
public async Task<IActionResult> GetPrefereredRecipes(int userId)
|
|
{
|
|
var recipes = await _dbAccess.ReadRecipes(userId);
|
|
if (recipes == null || recipes.Count == 0) { return new ConflictObjectResult(new { message = "Could not find any recipes" }); }
|
|
|
|
return new OkObjectResult(recipes);
|
|
}
|
|
|
|
public async Task<IActionResult> GetRecipe(int recipeId)
|
|
{
|
|
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
|
|
|
if (recipe == null || recipe.Id == 0) { return new ConflictObjectResult(new { message = "Could not find any recipe" }); }
|
|
|
|
return new OkObjectResult(recipe);
|
|
}
|
|
|
|
public async Task<IActionResult> CreateRecipe(RecipeDTO recipe, int prefereredRecipesId)
|
|
{
|
|
var prefereredRecipes = await _dbAccess.ReadAllRecipe(prefereredRecipesId);
|
|
|
|
foreach (var item in prefereredRecipes.Recipes)
|
|
{
|
|
if (item.Name == recipe.Name)
|
|
{
|
|
return new ConflictObjectResult(new { message = "Recipe name is already in use." });
|
|
}
|
|
}
|
|
|
|
Recipe recipe1 = new Recipe();
|
|
recipe1.Name = recipe.Name;
|
|
recipe1.Directions = recipe.Directions;
|
|
recipe1.Description = recipe.Description;
|
|
recipe1.Ingredients = new List<Ingredient>();
|
|
foreach (var item in recipe.Ingredients)
|
|
{
|
|
Ingredient ingredient = new Ingredient();
|
|
ingredient.Unit = item.Unit;
|
|
ingredient.Name = item.Name;
|
|
ingredient.Amount = item.Amount;
|
|
recipe1.Ingredients.Add(ingredient);
|
|
}
|
|
|
|
return await _dbAccess.CreateRecipe(recipe1, prefereredRecipesId);
|
|
}
|
|
|
|
public async Task<IActionResult> EditRecipe(RecipeDTO recipe, int recipeId, int userId)
|
|
{
|
|
var recipes = await _dbAccess.ReadRecipes(userId);
|
|
var dish = await _dbAccess.ReadRecipe(recipeId);
|
|
|
|
foreach (var item in recipes)
|
|
{
|
|
if (item.Name == recipe.Name)
|
|
{
|
|
return new ConflictObjectResult(new { message = "Recipe name is already in use." });
|
|
}
|
|
}
|
|
|
|
dish.Name = recipe.Name;
|
|
dish.Description = recipe.Description;
|
|
dish.Directions = recipe.Directions;
|
|
foreach (var item in recipe.Ingredients)
|
|
{
|
|
Ingredient ingredient = new Ingredient();
|
|
ingredient.Unit = item.Unit;
|
|
ingredient.Name = item.Name;
|
|
ingredient.Amount = item.Amount;
|
|
dish.Ingredients.Add(ingredient);
|
|
}
|
|
|
|
return await _dbAccess.UpdateRecipe(dish);
|
|
}
|
|
|
|
public async Task<IActionResult> DeleteRecipe(int recipeId)
|
|
{
|
|
var recipe = await _dbAccess.ReadRecipe(recipeId);
|
|
|
|
if (recipe != null) { return await _dbAccess.DeleteUser(recipe); }
|
|
|
|
return new ConflictObjectResult(new { message = "Invalid user" });
|
|
}
|
|
}
|
|
}
|