public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task Commit <T>(CancellationToken token) where T : BaseDomainModel { var user = _accessor.HttpContext?.User.Identity?.Name ?? "Anonymous User"; foreach (var entity in _db.ChangeTracker.Entries <T>()) { if (entity.State == EntityState.Added) { entity.Entity.CreatedAt = DateTimeOffset.Now; entity.Entity.UpdatedAt = DateTimeOffset.Now; if (string.IsNullOrEmpty(entity.Entity.CreatedBy)) { entity.Entity.CreatedBy = user; entity.Entity.UpdatedBy = user; } } if (entity.State == EntityState.Modified) { entity.Entity.UpdatedAt = DateTimeOffset.Now; if (string.IsNullOrEmpty(entity.Entity.UpdatedBy)) { entity.Entity.UpdatedBy = user.ToUpperInvariant(); } } } await _db.SaveChangesAsync(token).ConfigureAwait(false); }