/// <summary> /// Creates a new new violation category in the account context for every partition. /// </summary> public async Task UpdateAccountViolationCategory(CommonViolationCategory commonViolationCategory) { 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.Where(m => m.AccountId == account.Id && m.CommonCategoryId == commonViolationCategory.Id).SingleOrDefaultAsync(); if (accountViolationCategory != null) { var accountViolationType = await accountContext.ViolationTypes.AsNoTracking().Where(m => m.AccountId == account.Id && m.CommonViolationTypeId == commonViolationCategory.TypeId).SingleOrDefaultAsync(); if (accountViolationType != null) { accountViolationCategory.TypeId = accountViolationType.Id; accountViolationCategory.Name = commonViolationCategory.Name; accountViolationCategory.Description = commonViolationCategory.Description; await accountContext.SaveChangesAsync(); } } } } }
/// <summary> /// Creates a new new violation category in the account context for every partition. /// </summary> public async Task CreateAccountViolationCategory(CommonViolationCategory commonViolationCategory) { 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 accountViolationType = await accountContext.ViolationTypes.AsNoTracking().Where(m => m.AccountId == account.Id && m.CommonViolationTypeId == commonViolationCategory.TypeId).SingleOrDefaultAsync(); if (accountViolationType != null) { var created = DateTime.UtcNow; var violationCategory = Mapper.Map <ViolationCategory>(commonViolationCategory); violationCategory.TypeId = accountViolationType.Id; violationCategory.AccountId = account.Id; violationCategory.CreateUtc = created; //Setting created and UpdatedUtc to the exact same value will let us know if this record has every been changed. violationCategory.UpdateUtc = created; accountContext.ViolationCategorys.Add(violationCategory); await accountContext.SaveChangesAsync(); } } } }
public async Task <IActionResult> EditCategory(ViolationTypeViewModel model) { if (ModelState.IsValid) { var violation = new CommonViolationCategory() { Name = model.Name.Trim(), Description = model.Description?.Trim(), Disabled = model.Disabled, TypeId = model.TypeId, Type = await CommonContext.CommonViolationTypes.SingleAsync(m => m.Id == model.TypeId), Id = model.Id }; var NameAlreadyExists = await CommonContext.CommonViolationCategories.AnyAsync( q => q.Name.ToLower() == model.Name.Trim().ToLower() && q.TypeId == model.TypeId && 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 Category Name with this Violation Type already exists."); } else { var result = await _violationSvc.UpdateViolationCategory(violation, User.GetLoggedInUserId().Value); //Purge common Violation Category cache await _cache.RemoveAsync(WebCacheKey.CommonViolationCategories); return(RedirectToAction("Categories")); } } await PopulateDropDownType(model); return(View(model)); }
/// <summary> /// Update an existing violation category. /// </summary> /// <param name="Category"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolationCategory> > UpdateViolationCategory(CommonViolationCategory Category, Guid createdById) { var result = new ServiceResponse <CommonViolationCategory>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { //update CommonViolationTypes table var violationDetails = await _commonCtx.CommonViolationCategories.SingleOrDefaultAsync(a => a.Id == Category.Id); violationDetails.UpdateUserId = createdById; violationDetails.UpdateUtc = DateTime.Now; violationDetails.Name = Category.Name; violationDetails.Description = Category.Description; violationDetails.Disabled = Category.Disabled; violationDetails.TypeId = Category.TypeId; _commonCtx.CommonViolationCategories.Update(violationDetails); await _commonCtx.SaveChangesAsync(); await UpdateAccountViolationCategory(violationDetails); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error updating a violation category"); result.Success = false; result.Message = ex.Message; } return(result); }
/// <summary> /// create new violation category /// </summary> /// <param name="commonCategory"></param> /// <param name="createdById"></param> /// <returns></returns> public async Task <ServiceResponse <CommonViolationCategory> > CreateViolationCategory(CommonViolationCategory commonCategory, Guid createdById) { var result = new ServiceResponse <CommonViolationCategory>(); try { using (var scope = _commonCtx.Database.BeginTransaction()) { commonCategory.CreateUserId = createdById; commonCategory.UpdateUserId = createdById; _commonCtx.CommonViolationCategories.Add(commonCategory); await _commonCtx.SaveChangesAsync(); await CreateAccountViolationCategory(commonCategory); scope.Commit(); } } catch (Exception ex) { _logger.Error(ex, "Error creating new Violation category"); throw ex; } return(result); }