public async Task <LunchItem> AddItemInstanceAsync(int lunchId, int productId) { var lunch = await GetLunchByIdAsync(lunchId); var product = await _catalog.GetById(productId); var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem == null) { shoppingCartLunchItem = new LunchItem { Lunch = lunch, Product = product, Quantity = 1 }; _appDbContext.LunchItems.Add(shoppingCartLunchItem); } else { shoppingCartLunchItem.Quantity += 1; _appDbContext.LunchItems.Update(shoppingCartLunchItem); } _appDbContext.SaveChanges(); return(shoppingCartLunchItem); }
public async Task <LunchItem> RemoveItemInstanceAsync(int lunchId, int productId) { LunchItem result = null; var lunch = await GetLunchByIdAsync(lunchId); var product = await _catalog.GetById(productId); var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem != null) { if (shoppingCartLunchItem.Quantity > 1) { shoppingCartLunchItem.Quantity -= 1; _appDbContext.LunchItems.Update(shoppingCartLunchItem); result = shoppingCartLunchItem; } else { _appDbContext.LunchItems.Remove(shoppingCartLunchItem); } _appDbContext.SaveChanges(); } return(result); }
public IActionResult Create([FromBody] LunchItem item) { if (item == null) { return(BadRequest()); } m_context.LunchItems.Add(item); m_context.SaveChanges(); return(CreatedAtRoute("GetLunch", new { ID = item.ID }, item)); }
public LunchItem RemoveItem(int lunchId, int productId) { LunchItem result = null; var lunch = GetLunch(lunchId); var product = _appDbContext.Products.First(x => x.ProductId == productId); var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem != null) { _appDbContext.LunchItems.Remove(shoppingCartLunchItem); _appDbContext.SaveChanges(); } return(result); }
public async Task <LunchItem> RemoveItemAsync(int lunchId, int productId) { LunchItem result = null; var lunch = await GetLunchByIdAsync(lunchId); var product = await _catalog.GetById(productId); var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem != null) { _appDbContext.LunchItems.Remove(shoppingCartLunchItem); _appDbContext.SaveChanges(); } return(result); }
public LunchItem AddItem(int lunchId, int productId) { var lunch = GetLunch(lunchId); var product = _appDbContext.Products.First(x => x.ProductId == productId); var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem == null) { shoppingCartLunchItem = new LunchItem { Lunch = lunch, Product = product, Quantity = 1 }; _appDbContext.LunchItems.Add(shoppingCartLunchItem); _appDbContext.SaveChanges(); } return(shoppingCartLunchItem); }
public async Task <LunchItem> AddItemAsync(int lunchId, int productId, int?quantity) { var lunch = await GetLunchByIdAsync(lunchId); var product = await _catalog.GetById(productId); var shoppingCartLunchItem = lunch.Items?.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem == null) { shoppingCartLunchItem = new LunchItem { Lunch = lunch, Product = product, Quantity = quantity.HasValue ? quantity.Value : 1 }; _appDbContext.LunchItems.Add(shoppingCartLunchItem); _appDbContext.SaveChanges(); } return(shoppingCartLunchItem); }
public IActionResult Update(long ID, [FromBody] LunchItem item) { if (item == null || item.ID != ID) { return(BadRequest()); } var luch = m_context.LunchItems.FirstOrDefault(t => t.ID == ID); if (luch == null) { return(NotFound()); } luch.CreatedAt = DateTimeOffset.Now.ToUnixTimeSeconds(); luch.Food = item.Food; m_context.LunchItems.Update(luch); m_context.SaveChanges(); return(new NoContentResult()); }
public LunchItem RemoveItemInstance(int lunchId, int productId) { LunchItem result = null; var lunch = GetLunch(lunchId); var product = _appDbContext.Products.First(x => x.ProductId == productId); var shoppingCartLunchItem = lunch.Items.FirstOrDefault(x => x.Product == product); if (shoppingCartLunchItem != null) { if (shoppingCartLunchItem.Quantity > 1) { shoppingCartLunchItem.Quantity -= 1; _appDbContext.LunchItems.Update(shoppingCartLunchItem); result = shoppingCartLunchItem; } else { _appDbContext.LunchItems.Remove(shoppingCartLunchItem); } _appDbContext.SaveChanges(); } return(result); }