public async Task <IActionResult> EditViolation(Guid Id) { var model = new AccountViolationViewModel(); var violationDetails = await _accountCtx.Violations.Include(x => x.Questions).ThenInclude(x => x.CreateUser) .SingleOrDefaultAsync(violation => violation.Id == Id); model = Mapper.Map <AccountViolationViewModel>(violationDetails); return(View(model)); }
// [Authorize(Roles = SystemPermission.Administrator)] public async Task <IActionResult> EditViolation(AccountViolationViewModel model) { if (ModelState.IsValid) { var accountviolation = new Violation() { CustomName = model.CustomName?.Trim(), CustomDescription = model.CustomDescription?.Trim(), CustomHelpUrl = model.CustomHelpUrl, CustomActions = model.CustomActions, CustomRequiredFields = model.CustomRequiredFields, Code = model.Code, Fee = model.Fee, ReminderMessage = model.ReminderMessage, ReminderMinutes = model.ReminderMinutes, Id = model.Id }; if (!string.IsNullOrWhiteSpace(model.CustomName)) { var NameAlreadyExists = await _accountCtx.Violations.AnyAsync( q => q.CustomName.ToLower() == model.CustomName.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, "Custom Name already exists."); return(View(model)); } } if (!string.IsNullOrWhiteSpace(model.Code)) { var CodeAlreadyExists = await _accountCtx.Violations.AnyAsync( q => q.Code == model.Code && q.Id != model.Id); if (CodeAlreadyExists) { // This isn't a security risk because we've verified the Name already exists ModelState.AddModelError(string.Empty, "Code already exists."); return(View(model)); } } var result = await _violationSvc.UpdateUserAccountViolation(accountviolation, User.GetLoggedInUserId().Value, _accountCtx); //Purge common accounts cache await _cache.RemoveAsync(WebCacheKey.Violations); return(RedirectToAction("Violations")); } //TODO: this should be cached and retrieved from a service var Types = await _accountCtx.ViolationTypes.Where(m => m.AccountId.Equals(CommonAccount.Id)).Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString(), }) .ToListAsync(); model.Types.AddRange(Types); //TODO: this should be cached and retrieved from a service var Categories = await _accountCtx.ViolationCategorys.Include(m => m.Type).Where(m => m.AccountId.Equals(CommonAccount.Id)).Select(m => new SelectListItem { Text = m.Name + " (" + m.Type.Name + " )", Value = m.Id.ToString(), }).ToListAsync(); model.Categories.AddRange(Categories); return(View(model)); }