/// <summary> /// Update an existing violation type. /// </summary> /// <param name="violationtype"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolationType> > UpdateType(CommonViolationType violationtype, Guid createdById) { var result = new ServiceResponse <CommonViolationType>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { //update CommonViolationTypes table var violationDetails = await _commonCtx.CommonViolationTypes.SingleOrDefaultAsync(a => a.Id == violationtype.Id); violationDetails.UpdateUserId = createdById; violationDetails.UpdateUtc = DateTime.Now; violationDetails.Name = violationtype.Name; violationDetails.Description = violationtype.Description; violationDetails.Disabled = violationtype.Disabled; _commonCtx.CommonViolationTypes.Update(violationDetails); await _commonCtx.SaveChangesAsync(); ///Update violation types across all accounts await UpdateAccountViolationType(violationDetails); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error updating a violation type"); result.Success = false; result.Message = ex.Message; } return(result); }
public async Task <IActionResult> EditType(ViolationTypeViewModel model) { if (ModelState.IsValid) { var violation = new CommonViolationType() { Name = model.Name.Trim(), Id = model.Id, Description = model.Description?.Trim(), Disabled = model.Disabled }; var NameAlreadyExists = await CommonContext.CommonViolationTypes.AnyAsync(q => q.Name.ToLower() == model.Name.Trim().ToLower() && q.Id != model.Id); if (NameAlreadyExists) { // This isn't a security risk because we've verified the Name already exists ModelState.AddModelError(string.Empty, "Name already exists."); } else { var result = await _violationSvc.UpdateType(violation, User.GetLoggedInUserId().Value); //Purge common accounts cache await _cache.RemoveAsync(WebCacheKey.CommonViolationTypes); return(RedirectToAction("Types")); } } return(View(model)); }
/// <summary> /// Updates violation type in the account context for every partition. /// </summary> public async Task UpdateAccountViolationType(CommonViolationType commonViolationType) { var allAccounts = await _commonCtx.CommonAccounts.AsNoTracking().Include(m => m.Partition).Where(m => m.Archived == false).ToListAsync(); //Go through each account and add this CommonViolation to Violation foreach (var account in allAccounts) { using (var accountContext = ContextsUtility.CreateAccountContext(Cryptography.Decrypt(account.Partition.ConnectionString))) { var violationType = await accountContext.ViolationTypes.Where(m => m.AccountId == account.Id && m.CommonViolationTypeId == commonViolationType.Id).SingleOrDefaultAsync(); if (violationType != null) { violationType.Name = commonViolationType.Name; violationType.Description = commonViolationType.Description; await accountContext.SaveChangesAsync(); } } } }
/// <summary> /// Creates a new new violation type in the account context for every partition. /// </summary> public async Task CreateAccountViolationType(CommonViolationType commonViolationType) { var allAccounts = await _commonCtx.CommonAccounts.AsNoTracking().Include(m => m.Partition).Where(m => m.Archived == false).ToListAsync(); //Go through each account and add this CommonViolation to Violation foreach (var account in allAccounts) { using (var accountContext = ContextsUtility.CreateAccountContext(Cryptography.Decrypt(account.Partition.ConnectionString))) { var created = DateTime.UtcNow; var violationType = Mapper.Map <ViolationType>(commonViolationType); violationType.AccountId = account.Id; violationType.CreateUtc = created; //Setting created and UpdatedUtc to the exact same value will let us know if this record has every been changed. violationType.UpdateUtc = created; accountContext.ViolationTypes.Add(violationType); await accountContext.SaveChangesAsync(); } } }
/// <summary> /// create new violation type /// </summary> /// <param name="violationtype"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolationType> > CreateViolationType(CommonViolationType violationtype, Guid createdById) { var result = new ServiceResponse <CommonViolationType>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { violationtype.CreateUserId = createdById; violationtype.UpdateUserId = createdById; _commonCtx.CommonViolationTypes.Add(violationtype); await _commonCtx.SaveChangesAsync(); //Adds this newly created violation to all the account ViolationTypes. await CreateAccountViolationType(violationtype); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error creating new Violation Type"); throw ex; } return(result); }