/// <summary> /// Creates a new new violation category in the account context for every partition. /// </summary> public async Task UpdateAccountViolation(CommonViolation commonViolation) { 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 accountViolation = await accountContext.Violations.Where(m => m.AccountId == account.Id && m.CommonViolationId == commonViolation.Id).SingleOrDefaultAsync(); if (accountViolation != null) { var accountViolationCategory = await accountContext.ViolationCategorys.AsNoTracking().Where(m => m.AccountId == account.Id && m.CommonCategoryId == commonViolation.CategoryId).SingleOrDefaultAsync(); if (accountViolationCategory != null) { accountViolation.CategoryId = accountViolationCategory.Id; accountViolation.Name = commonViolation.Name; accountViolation.Description = commonViolation.Description; accountViolation.HelpUrl = commonViolation.HelpUrl; accountViolation.Actions = commonViolation.Actions; accountViolation.RequiredFields = commonViolation.RequiredFields; accountViolation.ReminderMinutes = commonViolation.ReminderMinutes; //accountViolation.WarningQuizUrl = commonViolation.WarningQuizUrl; await accountContext.SaveChangesAsync(); } } } } }
/// <summary> /// create new violation /// </summary> /// <param name="Violation"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolation> > CreateViolation(CommonViolation Violation, Guid createdById) { var result = new ServiceResponse <CommonViolation>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { Violation.CreateUserId = createdById; Violation.UpdateUserId = createdById; _commonCtx.CommonViolations.Add(Violation); await _commonCtx.SaveChangesAsync(); //Add this violation to all accounts await CreateAccountViolation(Violation); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error creating new Violation"); throw ex; } return(result); }
/// <summary> /// Creates a new new violation in the account context for every partition. /// </summary> public async Task CreateAccountViolation(CommonViolation commonViolation) { 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 accountViolationCategory = await accountContext.ViolationCategorys.AsNoTracking().Where(m => m.AccountId == account.Id && m.CommonCategoryId == commonViolation.CategoryId).SingleOrDefaultAsync(); if (accountViolationCategory != null) { var created = DateTime.UtcNow; var accountViolation = Mapper.Map <Violation>(commonViolation); accountViolation.CategoryId = accountViolationCategory.Id; accountViolation.CustomRequiredFields = commonViolation.RequiredFields; accountViolation.CustomActions = commonViolation.Actions; accountViolation.AccountId = account.Id; accountViolation.CreateUtc = created; //Setting created and UpdatedUtc to the exact same value will let us know if this record has every been changed. accountViolation.UpdateUtc = created; accountContext.Violations.Add(accountViolation); await accountContext.SaveChangesAsync(); } } } }
public async Task <IActionResult> Edit(ViolationViewModel model) { if (ModelState.IsValid) { var Violation = new CommonViolation() { Name = model.Name.Trim(), Description = model.Description?.Trim(), Disabled = model.Disabled, HelpUrl = model.HelpUrl, Actions = model.Actions, CategoryId = model.CategoryId, Category = await CommonContext.CommonViolationCategories.SingleAsync(m => m.Id == model.CategoryId), Id = model.Id, RequiredFields = model.Fields, ReminderMinutes = model.ReminderMinutes, ReminderMessage = model.ReminderMessage }; var NameAlreadyExists = await CommonContext.CommonViolations.AnyAsync( q => q.Name.ToLower() == model.Name.Trim().ToLower() && q.CategoryId == model.CategoryId && q.Id != model.Id ); if (NameAlreadyExists) { // This isn't a security risk because we've verified the Name already exists ModelState.AddModelError(string.Empty, "Violation Name with this Violation Category already exists."); } else { var result = await _violationSvc.UpdateViolation(Violation, User.GetLoggedInUserId().Value); //Purge common accounts cache await _cache.RemoveAsync(WebCacheKey.CommonViolations); return(RedirectToAction("Index")); } } await PopulateDropDownCategory(model); return(View(model)); }
/// <summary> /// Update an existing violation. /// </summary> /// <param name="Violation"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolation> > UpdateViolation(CommonViolation Violation, Guid createdById) { var result = new ServiceResponse <CommonViolation>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { //update CommonViolation table var violationDetails = await _commonCtx.CommonViolations.SingleOrDefaultAsync(a => a.Id == Violation.Id); violationDetails.UpdateUserId = createdById; violationDetails.UpdateUtc = DateTime.Now; violationDetails.Name = Violation.Name; violationDetails.Description = Violation.Description; violationDetails.Disabled = Violation.Disabled; violationDetails.Actions = Violation.Actions; violationDetails.CategoryId = Violation.CategoryId; violationDetails.HelpUrl = Violation.HelpUrl; violationDetails.RequiredFields = Violation.RequiredFields; violationDetails.ReminderMessage = Violation.ReminderMessage; violationDetails.ReminderMinutes = Violation.ReminderMinutes; //violationDetails.WarningQuizUrl = Violation.WarningQuizUrl; _commonCtx.CommonViolations.Update(violationDetails); await _commonCtx.SaveChangesAsync(); await UpdateAccountViolation(violationDetails); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error updating a violation"); result.Success = false; result.Message = ex.Message; } return(result); }