Changed delete item in shopping list to work and add item to only return an item and not an array with one item
This commit is contained in:
parent
bf588c62ed
commit
7e1583f485
@ -28,11 +28,11 @@ namespace API.BusinessLogic
|
|||||||
{
|
{
|
||||||
var user = await _dbAccess.ReadShoppingList(userId);
|
var user = await _dbAccess.ReadShoppingList(userId);
|
||||||
|
|
||||||
List<ShoppingList> shoppingList = user.ShoppingList;
|
List<ShoppingListItem> shoppingList = user.ShoppingList;
|
||||||
|
|
||||||
if (shoppingList.Any(s => s.Name == listItemDTO.Name))
|
if (shoppingList.Any(s => s.Name == listItemDTO.Name))
|
||||||
{
|
{
|
||||||
ShoppingList item = shoppingList.Where(s => s.Name == listItemDTO.Name).FirstOrDefault();
|
ShoppingListItem item = shoppingList.Where(s => s.Name == listItemDTO.Name).FirstOrDefault();
|
||||||
shoppingList.Remove(item);
|
shoppingList.Remove(item);
|
||||||
|
|
||||||
if (item.Unit == listItemDTO.Unit)
|
if (item.Unit == listItemDTO.Unit)
|
||||||
@ -76,7 +76,7 @@ namespace API.BusinessLogic
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ShoppingList newItem = new ShoppingList();
|
ShoppingListItem newItem = new ShoppingListItem();
|
||||||
newItem.Name = listItemDTO.Name;
|
newItem.Name = listItemDTO.Name;
|
||||||
newItem.Amount = listItemDTO.Amount;
|
newItem.Amount = listItemDTO.Amount;
|
||||||
newItem.Unit = listItemDTO.Unit;
|
newItem.Unit = listItemDTO.Unit;
|
||||||
@ -88,7 +88,7 @@ namespace API.BusinessLogic
|
|||||||
|
|
||||||
var updatedShoppingList = await _dbAccess.ReadShoppingList(userId);
|
var updatedShoppingList = await _dbAccess.ReadShoppingList(userId);
|
||||||
|
|
||||||
if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name)); }
|
if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name).First()); }
|
||||||
|
|
||||||
return new ConflictObjectResult(new { message = "Could not save to database" });
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
}
|
}
|
||||||
@ -140,11 +140,11 @@ namespace API.BusinessLogic
|
|||||||
{
|
{
|
||||||
var user = await _dbAccess.ReadShoppingList(userId);
|
var user = await _dbAccess.ReadShoppingList(userId);
|
||||||
|
|
||||||
int itemIndex = user.ShoppingList.FindIndex(x => x.Id == itemId);
|
var item = user.ShoppingList.Where(x => x.Id == itemId).FirstOrDefault();
|
||||||
|
|
||||||
user.ShoppingList.RemoveAt(itemIndex);
|
if (item == null) { return new ConflictObjectResult(new { message = "Could not find item" }); }
|
||||||
|
|
||||||
return await _dbAccess.UpdateShoppingList(user);
|
return await _dbAccess.DeleteItemFromShoppinglist(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds an entire recipes ingredients to the shoppinglist
|
// Adds an entire recipes ingredients to the shoppinglist
|
||||||
@ -156,11 +156,11 @@ namespace API.BusinessLogic
|
|||||||
|
|
||||||
foreach (var ingredient in ingredients)
|
foreach (var ingredient in ingredients)
|
||||||
{
|
{
|
||||||
List<ShoppingList> shoppingList = user.ShoppingList;
|
List<ShoppingListItem> shoppingList = user.ShoppingList;
|
||||||
|
|
||||||
if (shoppingList.Any(s => s.Name == ingredient.Name))
|
if (shoppingList.Any(s => s.Name == ingredient.Name))
|
||||||
{
|
{
|
||||||
ShoppingList item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault();
|
ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault();
|
||||||
shoppingList.Remove(item);
|
shoppingList.Remove(item);
|
||||||
|
|
||||||
if (item.Unit == ingredient.Unit)
|
if (item.Unit == ingredient.Unit)
|
||||||
@ -205,7 +205,7 @@ namespace API.BusinessLogic
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ShoppingList newItem = new ShoppingList();
|
ShoppingListItem newItem = new ShoppingListItem();
|
||||||
newItem.Name = ingredient.Name;
|
newItem.Name = ingredient.Name;
|
||||||
newItem.Amount = ingredient.Amount;
|
newItem.Amount = ingredient.Amount;
|
||||||
newItem.Unit = ingredient.Unit;
|
newItem.Unit = ingredient.Unit;
|
||||||
|
@ -67,7 +67,7 @@ namespace API.BusinessLogic
|
|||||||
Password = hashedPassword,
|
Password = hashedPassword,
|
||||||
Salt = salt,
|
Salt = salt,
|
||||||
Recipes = new List<Recipe>(),
|
Recipes = new List<Recipe>(),
|
||||||
ShoppingList = new List<ShoppingList>(),
|
ShoppingList = new List<ShoppingListItem>(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await _dbAccess.CreateUser(user);
|
return await _dbAccess.CreateUser(user);
|
||||||
|
@ -11,7 +11,7 @@ namespace API.DBAccess
|
|||||||
|
|
||||||
public DbSet<Recipe> Recipes { get; set; }
|
public DbSet<Recipe> Recipes { get; set; }
|
||||||
|
|
||||||
public DbSet<ShoppingList> ShoppingList { get; set; }
|
public DbSet<ShoppingListItem> ShoppingList { get; set; }
|
||||||
|
|
||||||
public DBContext(DbContextOptions<DBContext> options) : base(options) { }
|
public DBContext(DbContextOptions<DBContext> options) : base(options) { }
|
||||||
}
|
}
|
||||||
|
@ -46,5 +46,15 @@ namespace API.DBAccess
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> DeleteItemFromShoppinglist(ShoppingListItem item)
|
||||||
|
{
|
||||||
|
_context.ShoppingList.Remove(item);
|
||||||
|
bool saved = await _context.SaveChangesAsync() >= 0;
|
||||||
|
|
||||||
|
if (saved) { return new OkObjectResult(saved); }
|
||||||
|
|
||||||
|
return new ConflictObjectResult(new { message = "Could not save to database" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace API.Models.ShoppingListModels
|
namespace API.Models.ShoppingListModels
|
||||||
{
|
{
|
||||||
public class ShoppingList
|
public class ShoppingListItem
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
@ -21,6 +21,6 @@ namespace API.Models.UserModels
|
|||||||
|
|
||||||
public List<Recipe> Recipes { get; set; }
|
public List<Recipe> Recipes { get; set; }
|
||||||
|
|
||||||
public List<ShoppingList> ShoppingList { get; set; }
|
public List<ShoppingListItem> ShoppingList { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user