/// <summary> /// Grabs ID of specific coffee object and deletes it.\ /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task DeleteCoffee(int id) { Coffee coffee = await _context.Coffee.FindAsync(id); if (coffee != null) { _context.Remove(coffee); await _context.SaveChangesAsync(); } }
public void EditOrder(OrderModel changedOrder) { var orderToChange = Orders.SingleOrDefault(o => o.OrderId.Equals(changedOrder.OrderId)); //get old ordered products orderToChange.OrderedProducts = OrderedProducts.Where(op => op.OrderId.Equals(orderToChange.OrderId)).ToList(); //if product doesn't exists in old order, add it foreach (var op in changedOrder.OrderedProducts) { if (orderToChange.OrderedProducts.SingleOrDefault(o => o.ProductId.Equals(op.ProductId)) == null) { //orderToChange.OrderedProducts.Add(op); dbContext.Add <OrderedProducts>(op); } } //if product doesn't exists in changed order, delete it foreach (var op in orderToChange.OrderedProducts) { if (changedOrder.OrderedProducts.SingleOrDefault(o => o.ProductId.Equals(op.ProductId)) == null) { dbContext.Remove <OrderedProducts>(op); } } dbContext.SaveChanges(); }