/// <summary> /// Looks for the accountuser in cache. If it doesn't exists, looks for accountuser in database. If found, adds it to cache. /// </summary> public async Task <CommonUserAccountModel> GetCommonAccountUserAsync(long accountNumber, Guid userId) { var cacheKey = WebCacheKey.CommonUserAccount(accountNumber, userId); var expiry = TimeSpan.FromHours(24); // Try to pull it from the cache first. var commonUserAccountModel = await _cache.GetWithSlidingExpirationAsync <CommonUserAccountModel>(cacheKey, expiry); if (commonUserAccountModel != null) { return(commonUserAccountModel); } var commonUserAccount = await _commonContext.UserAccounts .AsNoTracking() .SingleOrDefaultAsync(cua => cua.UserId == userId && cua.Account.Number == accountNumber); if (commonUserAccount != null) { commonUserAccountModel = Mapper.Map <CommonUserAccountModel>(commonUserAccount); // Found it. Add it to the cache. await _cache.SetAsync(cacheKey, commonUserAccountModel, expiry); } return(commonUserAccountModel); }
public async Task <IActionResult> Edit(AccountUserViewModelEdit model) { if (ModelState.IsValid) { var existingUser = await CommonContext.UserAccounts.Include(x => x.User) .Where(m => m.UserId == model.UserId && m.AccountId == CommonAccount.Id).SingleOrDefaultAsync(); if (existingUser == null) { throw new Exception($"User cannot be found. UserId:{model.UserId}"); } if (User.GetLoggedInUserId().Value == model.UserId && !model.Permissions.HasFlag(AccountPermissions.AccountAdministrator)) { ModelState.AddModelError(string.Empty, "You cannot remove the Account Administrator permission"); model.FirstName = existingUser.User.FirstName; model.LastName = existingUser.User.LastName; model.Email = existingUser.User.Email; return(View(model)); } if (User.GetLoggedInUserId().Value == model.UserId && model.Disabled) { ModelState.AddModelError(string.Empty, "You cannot disable your own account"); model.FirstName = existingUser.User.FirstName; model.LastName = existingUser.User.LastName; model.Email = existingUser.User.Email; return(View(model)); } existingUser.Permissions = model.Permissions; existingUser.Disabled = model.Disabled; existingUser.UpdateUserId = LoggedInUser.Id; existingUser.UpdateUtc = DateTime.UtcNow; using (var tx = CommonContext.Database.BeginTransaction()) { CommonContext.UserAccounts.Update(existingUser); await CommonContext.SaveChangesAsync(); tx.Commit(); var cacheKey = WebCacheKey.CommonUserAccount(CommonAccount.Number, existingUser.UserId.Value); await _cache.RemoveAsync(cacheKey); } return(RedirectToAction("Users")); } return(View(model)); }
public async Task <IActionResult> PurgeCache() { var accountUsers = await CommonContext.UserAccounts.Include(m => m.Account).AsNoTracking().ToListAsync(); var cachKeys = new List <string>(); var accounts = accountUsers.Select(m => m.Account).Distinct(); //CommonAccounts foreach (var account in accounts) { cachKeys.Add(WebCacheKey.CommonAccount(account.Number)); } cachKeys.Add(WebCacheKey.CommonAccounts); //Account Users foreach (var accountUser in accountUsers) { cachKeys.Add(WebCacheKey.CommonUserAccount(accountUser.Account.Number, accountUser.UserId.Value)); } var users = await CommonContext.Users.Select(m => m.Id).ToListAsync(); foreach (var user in users) { cachKeys.Add(WebCacheKey.LoggedInUser(user)); } cachKeys.Add(WebCacheKey.HealthCheck); cachKeys.Add(WebCacheKey.CommonViolationTypes); cachKeys.Add(WebCacheKey.CommonViolationCategories); cachKeys.Add(WebCacheKey.CommonViolations); cachKeys.Add(WebCacheKey.Violations); await _cache.RemoveAsync(cachKeys.ToArray()); return(RedirectToAction("Index")); }