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;
}
///
/// Gets all the recipes from _dbaccess and checks if there are any
///
/// the usér connected to the recipes
/// a list of recipes in a ok objectresult
public async Task GetRecipes(int userId)
{
var recipes = await _dbAccess.ReadRecipes(userId);
if (recipes == null || recipes.Count == 0) { return new ConflictObjectResult(new { message = "Could not find any recipes" }); }
var recipeDtos = recipes.Select(r => new GetAllRecipesDTO
{
Id = r.Id,
Name = r.Name,
Description = r.Description
}).ToList();
return new OkObjectResult(recipeDtos);
}
// Gets a specifik with recipe with the ingredient and directions
public async Task 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" }); }
var singleRecipeDTO = new GetSingleRecipeDTO();
singleRecipeDTO.Id = recipe.Id;
singleRecipeDTO.Name = recipe.Name;
singleRecipeDTO.Description = recipe.Description;
singleRecipeDTO.Ingredients = recipe.Ingredients;
singleRecipeDTO.Directions = new List();
foreach (var item in recipe.Directions)
{
string directions = item.Instruktions;
singleRecipeDTO.Directions.Add(directions);
}
return new OkObjectResult(singleRecipeDTO);
}
///
/// Creates a recipe and checks if the recipe name is in use
/// Converts the recipeDTO to a normal recipe
///
/// The recipeDTO that does not include all the id tags
/// The user that is going to get that recipe
/// returns what recipedbaccess gives back
public async Task CreateRecipe(RecipeDTO recipe, int userId)
{
var recipes = await _dbAccess.ReadRecipes(userId);
foreach (var item in recipes)
{
if (item.Name == recipe.Name)
{
return new ConflictObjectResult(new { message = "Recipe name is already in use." });
}
}
Recipe dish = new Recipe();
dish.Name = recipe.Name;
dish.Description = recipe.Description;
dish.Ingredients = new List();
dish.Directions = new List();
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);
}
foreach (var item in recipe.Directions)
{
Directions directions = new Directions();
directions.Instruktions = item;
dish.Directions.Add(directions);
}
return await _dbAccess.CreateRecipe(dish, userId);
}
///
/// Updates the recipe that is saved in the db
///
/// The updated recipe
/// The recipeId on the recipe to be updated
/// The userÍd of the owner of the to be updated recipe
/// returns what recipedbaccess gives back
public async Task 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 = new List();
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);
}
foreach (var item in recipe.Directions)
{
Directions directions = new Directions();
directions.Instruktions = item;
dish.Directions.Add(directions);
}
return await _dbAccess.UpdateRecipe(dish);
}
// Deletes the recipe
public async Task DeleteRecipe(int recipeId)
{
var recipe = await _dbAccess.ReadRecipe(recipeId);
if (recipe != null) { return await _dbAccess.DeleteRecipe(recipe); }
return new ConflictObjectResult(new { message = "Invalid recipe" });
}
}
}