easyeat/backend/API/BusinessLogic/ShoppingListLogic.cs

222 lines
8.8 KiB
C#

using API.DBAccess;
using API.Models.ShoppingListModels;
using Microsoft.AspNetCore.Mvc;
namespace API.BusinessLogic
{
public class ShoppingListLogic
{
private readonly ShoppingListDBAccess _dbAccess;
private readonly RecipeDBAccess _recipeDBAccess;
public ShoppingListLogic(ShoppingListDBAccess dbAccess, RecipeDBAccess recipeDBAccess)
{
_dbAccess = dbAccess;
_recipeDBAccess = recipeDBAccess;
}
// Reads the current shooping list of the user
public async Task<IActionResult> ReadShoppingList(int userId)
{
var user = await _dbAccess.ReadShoppingList(userId);
return new OkObjectResult(user.ShoppingList);
}
// Adds an item to the shoppinglist and checks if the unit should be changed and if the name is the same as an item already on the shoppinglist
public async Task<IActionResult> AddItemToShoppingList(ShoppingListItemDTO listItemDTO, int userId)
{
var user = await _dbAccess.ReadShoppingList(userId);
List<ShoppingListItem> shoppingList = user.ShoppingList;
if (shoppingList.Any(s => s.Name == listItemDTO.Name))
{
ShoppingListItem item = shoppingList.Where(s => s.Name == listItemDTO.Name).FirstOrDefault();
shoppingList.Remove(item);
if (item.Unit == listItemDTO.Unit)
{
item.Amount += listItemDTO.Amount;
}
else if (item.Unit == "g" && listItemDTO.Unit == "kg")
{
item.Amount = (item.Amount / 1000) + listItemDTO.Amount;
item.Unit = "kg";
}
else if (item.Unit == "ml" && listItemDTO.Unit == "l")
{
item.Amount = (item.Amount / 1000) + listItemDTO.Amount;
item.Unit = "l";
}
else if (item.Unit == "dl" && listItemDTO.Unit == "l")
{
item.Amount = (item.Amount / 10) + listItemDTO.Amount;
item.Unit = "l";
}
item.Checked = false;
if (item.Unit == "g" && item.Amount >= 1000)
{
item.Unit = "kg";
item.Amount = item.Amount / 1000;
}
else if (item.Unit == "ml" && item.Amount >= 1000)
{
item.Unit = "l";
item.Amount = item.Amount / 1000;
}
else if (item.Unit == "dl" && item.Amount >= 10)
{
item.Unit = "l";
item.Amount = item.Amount / 10;
}
user.ShoppingList.Add(item);
}
else
{
ShoppingListItem newItem = new ShoppingListItem();
newItem.Name = listItemDTO.Name;
newItem.Amount = listItemDTO.Amount;
newItem.Unit = listItemDTO.Unit;
newItem.Checked = false;
user.ShoppingList.Add(newItem);
}
bool succes = await _dbAccess.AddItemToShoppingList(user);
var updatedShoppingList = await _dbAccess.ReadShoppingList(userId);
if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name).First()); }
return new ConflictObjectResult(new { message = "Could not save to database" });
}
// Gets the shoppinglist and tries to find the item and when it does it checks/unchecks that item
public async Task<IActionResult> CheckItemInShoppingList(int userId, int itemId)
{
var user = await _dbAccess.ReadShoppingList(userId);
int itemIndex = user.ShoppingList.FindIndex(x => x.Id == itemId);
user.ShoppingList[itemIndex].Checked = !user.ShoppingList[itemIndex].Checked;
return await _dbAccess.UpdateShoppingList(user);
}
// Updates an item on the shopping list to what the user specified
public async Task<IActionResult> UpdateItemInShoppingList(int userId, int itemId, ShoppingListItemDTO listItemDTO)
{
var user = await _dbAccess.ReadShoppingList(userId);
int itemIndex = user.ShoppingList.FindIndex(x => x.Id == itemId);
user.ShoppingList[itemIndex].Unit = listItemDTO.Unit;
user.ShoppingList[itemIndex].Name = listItemDTO.Name;
user.ShoppingList[itemIndex].Amount = listItemDTO.Amount;
if (user.ShoppingList[itemIndex].Unit == "g" && user.ShoppingList[itemIndex].Amount >= 1000)
{
user.ShoppingList[itemIndex].Unit = "kg";
user.ShoppingList[itemIndex].Amount = user.ShoppingList[itemIndex].Amount / 1000;
}
else if (user.ShoppingList[itemIndex].Unit == "ml" && user.ShoppingList[itemIndex].Amount >= 1000)
{
user.ShoppingList[itemIndex].Unit = "l";
user.ShoppingList[itemIndex].Amount = user.ShoppingList[itemIndex].Amount / 1000;
}
else if (user.ShoppingList[itemIndex].Unit == "dl" && user.ShoppingList[itemIndex].Amount >= 10)
{
user.ShoppingList[itemIndex].Unit = "l";
user.ShoppingList[itemIndex].Amount = user.ShoppingList[itemIndex].Amount / 10;
}
return await _dbAccess.UpdateShoppingList(user);
}
// Deletes an item from the shopping list if it is on the users shoppinglist
public async Task<IActionResult> DeleteItemInShoppingList(int userId, int itemId)
{
var user = await _dbAccess.ReadShoppingList(userId);
var item = user.ShoppingList.Where(x => x.Id == itemId).FirstOrDefault();
if (item == null) { return new ConflictObjectResult(new { message = "Could not find item" }); }
return await _dbAccess.DeleteItemFromShoppinglist(item);
}
// Adds an entire recipes ingredients to the shoppinglist
public async Task<IActionResult> AddRecipeToShoppingList(int userId, int recipeId)
{
var user = await _dbAccess.ReadShoppingList(userId);
var recipe = await _recipeDBAccess.ReadRecipe(recipeId);
var ingredients = recipe.Ingredients;
foreach (var ingredient in ingredients)
{
List<ShoppingListItem> shoppingList = user.ShoppingList;
if (shoppingList.Any(s => s.Name == ingredient.Name))
{
ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault();
shoppingList.Remove(item);
if (item.Unit == ingredient.Unit)
{
item.Amount += ingredient.Amount;
}
else if (item.Unit == "g" && ingredient.Unit == "kg")
{
item.Amount = (item.Amount / 1000) + ingredient.Amount;
item.Unit = "kg";
}
else if (item.Unit == "ml" && item.Unit == "l")
{
item.Amount = (item.Amount / 1000) + ingredient.Amount;
item.Unit = "l";
}
else if (item.Unit == "dl" && item.Unit == "l")
{
item.Amount = (item.Amount / 10) + ingredient.Amount;
item.Unit = "l";
}
item.Checked = false;
if (item.Unit == "g" && item.Amount >= 1000)
{
item.Unit = "kg";
item.Amount = item.Amount / 1000;
}
else if (item.Unit == "ml" && item.Amount >= 1000)
{
item.Unit = "l";
item.Amount = item.Amount / 1000;
}
else if (item.Unit == "dl" && item.Amount >= 10)
{
item.Unit = "l";
item.Amount = item.Amount / 10;
}
user.ShoppingList.Add(item);
}
else
{
ShoppingListItem newItem = new ShoppingListItem();
newItem.Name = ingredient.Name;
newItem.Amount = ingredient.Amount;
newItem.Unit = ingredient.Unit;
newItem.Checked = false;
user.ShoppingList.Add(newItem);
}
}
return await _dbAccess.UpdateShoppingList(user);
}
}
}