public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { var pdt = _context.Products.Find(id); if (pdt == null) { return(NotFound()); } throw; } return(NoContent()); }
//edit config value public bool EditConfig(Config config) { using (var context = new eComContext()) { context.Entry(config).State = EntityState.Modified; return(context.SaveChanges() > 0); } }
//edit category public void UpdateCategory(Category category) { using (var context = new eComContext()) { context.Entry(category).State = EntityState.Modified; context.SaveChanges(); } }
//edit product public void UpdateProduct(Product product) { using (var context = new eComContext()) { context.Entry(product).State = EntityState.Modified; context.SaveChanges(); } }
//add product public void SaveProduct(Product product) { using (var context = new eComContext()) { context.Entry(product.Category).State = EntityState.Unchanged; //prevent adding category again with different id context.Products.Add(product); context.SaveChanges(); } }
//edit order status (by admin) public bool UpdateOrderStatus(string status, int id) { using (var context = new eComContext()) { var order = context.Orders.Where(o => o.Id == id).FirstOrDefault(); order.Status = status; context.Entry(order).State = EntityState.Modified; return(context.SaveChanges() > 0); } }
//update main picture in home page (by admin) public bool UpdateMainPicture(string key, string imageUrl) { using (var context = new eComContext()) { var existConfig = context.Configurations.Where(c => c.Key == key).FirstOrDefault(); if (existConfig != null) { existConfig.Value = imageUrl; context.Entry(existConfig).State = EntityState.Modified; return(context.SaveChanges() > 0); } return(false); } }