/// <inheritdoc /> /// <exception cref="UserNotFoundException">Thrown when user is not found</exception> public async Task RemoveUserByIdAsync(string userId) { var userToRemove = await _userManager.FindByIdAsync(userId); if (userToRemove == null) { throw new UserNotFoundException(); } await _db.Entry(userToRemove).Collection(u => u.Todos).LoadAsync(); // Clear cached todos owned by the user foreach (var todo in userToRemove.Todos) { _cache.Remove(CacheConstants.GetSingleTodoCacheKey( todo.Id, userToRemove.Id)); _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey( userToRemove.Id, todo.Due.Date)); } _cache.Remove(CacheConstants.GetAllTodosCacheKey(userToRemove.Id)); _db.RemoveRange(userToRemove.Todos); await _userManager.DeleteAsync(userToRemove); await _db.SaveChangesAsync(); // Clear user if cached and all user list. _cache.Remove(CacheConstants.GetSingleUserCacheKey(userToRemove.Id)); _cache.Remove(CacheConstants.AllUsersCacheKey); }
public void GetSingleUserCacheKey_WithUuid_Matches() { // Arrange const string userId = "31f4a428-33b7-45c3-a886-a63d93543166"; // Act var cacheKey = CacheConstants.GetSingleUserCacheKey(userId); // Assert Assert.Equal("c_user_31f4a428-33b7-45c3-a886-a63d93543166", cacheKey); }
/// <inheritdoc /> /// <exception cref="UserNotFoundException">Thrown when user is not found</exception> public async Task <ApplicationUserDto> GetUserByIdAsync(string userId) { var cacheKey = CacheConstants.GetSingleUserCacheKey(userId); if (!_cache.TryGetValue(cacheKey, out ApplicationUser user)) { user = await _userManager.FindByIdAsync(userId); if (user == null) { throw new UserNotFoundException(); } _cache.Set(cacheKey, user, CacheConstants.GetDefaultCacheOptions()); } return(new ApplicationUserDto(user)); }