diff --git a/backend/API/BusinessLogic/ShoppingListLogic.cs b/backend/API/BusinessLogic/ShoppingListLogic.cs index 6386134..129a6d7 100644 --- a/backend/API/BusinessLogic/ShoppingListLogic.cs +++ b/backend/API/BusinessLogic/ShoppingListLogic.cs @@ -34,45 +34,8 @@ namespace API.BusinessLogic { 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); + + user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); } else { @@ -82,7 +45,7 @@ namespace API.BusinessLogic newItem.Unit = listItemDTO.Unit; newItem.Checked = false; - user.ShoppingList.Add(newItem); + user.ShoppingList.Add(await UnitAdjustment(newItem)); } bool succes = await _dbAccess.AddItemToShoppingList(user); @@ -163,45 +126,13 @@ namespace API.BusinessLogic 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"; - } + ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO(); + listItemDTO.Name = ingredient.Name; + listItemDTO.Amount = ingredient.Amount; + listItemDTO.Unit = ingredient.Unit; + listItemDTO.Checked = false; - 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); + user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); } else { @@ -211,11 +142,58 @@ namespace API.BusinessLogic newItem.Unit = ingredient.Unit; newItem.Checked = false; - user.ShoppingList.Add(newItem); + user.ShoppingList.Add(await UnitAdjustment(newItem)); } } return await _dbAccess.UpdateShoppingList(user); } + + public async Task UnitAdjustmentSameName(ShoppingListItemDTO listItemDTO, ShoppingListItem listItem) + { + if (listItem.Unit == listItemDTO.Unit) + { + listItem.Amount += listItemDTO.Amount; + } + else if (listItem.Unit == "g" && listItemDTO.Unit == "kg") + { + listItem.Amount = (listItem.Amount / 1000) + listItemDTO.Amount; + listItem.Unit = "kg"; + } + else if (listItem.Unit == "ml" && listItemDTO.Unit == "l") + { + listItem.Amount = (listItem.Amount / 1000) + listItemDTO.Amount; + listItem.Unit = "l"; + } + else if (listItem.Unit == "dl" && listItemDTO.Unit == "l") + { + listItem.Amount = (listItem.Amount / 10) + listItemDTO.Amount; + listItem.Unit = "l"; + } + + listItem.Checked = false; + + return await UnitAdjustment(listItem); + } + + public async Task UnitAdjustment(ShoppingListItem listItem) + { + if (listItem.Unit == "g" && listItem.Amount >= 1000) + { + listItem.Unit = "kg"; + listItem.Amount = listItem.Amount / 1000; + } + else if (listItem.Unit == "ml" && listItem.Amount >= 1000) + { + listItem.Unit = "l"; + listItem.Amount = listItem.Amount / 1000; + } + else if (listItem.Unit == "dl" && listItem.Amount >= 10) + { + listItem.Unit = "l"; + listItem.Amount = listItem.Amount / 10; + } + return listItem; + } } }