public async Task <IActionResult> Put([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } product.ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity).FindFirst(ClaimTypes.Sid).Value); product.ModifiedDate = DateTime.Now; _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Exists(id)) { return(NotFound()); } else { throw; } } return(Ok(product)); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] Traffic traffic) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != traffic.Id) { return(BadRequest()); } _context.Entry(traffic).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TrafficExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] Promotion promotion) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != promotion.Id) { return(BadRequest()); } promotion.ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity).FindFirst(ClaimTypes.Sid).Value); promotion.ModifiedDate = DateTime.Now; try { using (var context = _context) { using (var transaction = context.Database.BeginTransaction()) { try { promotion.ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity) .FindFirst(ClaimTypes.Sid).Value); promotion.ModifiedDate = DateTime.Now; _context.Entry(promotion).State = EntityState.Modified; var promotionsProducts = _context.PromotionProduct.Where(a => a.PromotionId == id); foreach (var promotionsProduct in promotionsProducts) { if (promotion.ProductIds.Contains(promotionsProduct.ProductId)) { continue; } _context.PromotionProduct.Remove(promotionsProduct); } foreach (var productId in promotion.ProductIds) { if (!promotionsProducts.Any(a => a.ProductId == productId)) { context.PromotionProduct.Add(new PromotionProduct { ProductId = productId, PromotionId = id, ModifiedDate = DateTime.Now, ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity) .FindFirst(ClaimTypes.Sid).Value), IsActive = true }); } } var promotionsPromoters = _context.PromotionPromoter.Where(a => a.PromotionId == id); foreach (var promotionsPromoter in promotionsPromoters) { if (promotion.PromoterIds.Contains(promotionsPromoter.PromoterId)) { continue; } _context.PromotionPromoter.Remove(promotionsPromoter); } foreach (var promoterId in promotion.PromoterIds) { if (!promotionsPromoters.Any(a => a.PromoterId == promoterId)) { context.PromotionPromoter.Add(new PromotionPromoter { PromoterId = promoterId, PromotionId = id, ModifiedDate = DateTime.Now, ModifiedUserId = Convert.ToInt32(((ClaimsIdentity)HttpContext.User.Identity) .FindFirst(ClaimTypes.Sid).Value), IsActive = true }); } } await context.SaveChangesAsync(); // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails transaction.Commit(); } catch (Exception e) { transaction.Rollback(); return(BadRequest(e.Message)); } } } } catch (DbUpdateConcurrencyException) { if (!Exists(id)) { return(NotFound()); } else { throw; } } return(Ok(promotion)); }