/// <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 GetAllTodosCacheKey_WithUuid_Matches() { // Arrange const string id = "79744ca4-c450-40ed-8ea4-daf0569b70e1"; // Act var cacheKey = CacheConstants.GetAllTodosCacheKey(id); // Assert Assert.Equal("c_todos_79744ca4-c450-40ed-8ea4-daf0569b70e1", cacheKey); }
/// <inheritdoc /> /// <exception cref="TodoNotFoundException">When todo is not found</exception> public async Task RemoveTodoByIdAsync(int todoId, string userId) { var todo = await _db.Todo.SingleOrDefaultAsync(t => t.Id == todoId && t.Owner.Id == userId); if (todo == null) { throw new TodoNotFoundException(); } _db.Remove(todo); await _db.SaveChangesAsync(); // Clear all related caches _cache.Remove(CacheConstants.GetSingleTodoCacheKey(todoId, userId)); _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId)); _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, todo.Due)); }
/// <summary> /// Returns all todos with no filtering. /// </summary> /// <param name="userId">The id of the tokwn owner</param> /// <returns>List of todos</returns> private async Task <IEnumerable <TodoDto> > GetAllTodosOrderedByDueWithNoFilterAsync(string userId) { var cacheKey = CacheConstants.GetAllTodosCacheKey(userId); if (!_cache.TryGetValue(cacheKey, out List <TodoDto> todos)) { todos = await(from t in _db.Todo where t.Owner.Id == userId orderby t.Due select new TodoDto(t)).ToListAsync(); if (todos.Any()) { _cache.Set(cacheKey, CacheConstants.GetDefaultCacheOptions()); } } return(todos); }
/// <inheritdoc /> /// <exception cref="UserNotFoundException">When todo is not found</exception> public async Task <int> CreateTodoAsync(CreateTodoViewModel todo, string userId) { var user = await _userManager.FindByIdAsync(userId); if (user == null) { throw new UserNotFoundException(); } var newTodo = new Todo(todo, user); await _db.AddAsync(newTodo); await _db.SaveChangesAsync(); // Add to single cache and clear list cache _cache.Set(CacheConstants.GetSingleTodoCacheKey(newTodo.Id, userId), newTodo, CacheConstants.GetDefaultCacheOptions()); _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId)); _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, newTodo.Due)); return(newTodo.Id); }
/// <inheritdoc /> /// <exception cref="TodoNotFoundException">When todo is not found</exception> public async Task EditTodoAsync(EditTodoViewModel model, string userId) { var todo = await _db.Todo.SingleOrDefaultAsync(t => t.Id == model.Id && t.Owner.Id == userId); if (todo == null) { throw new TodoNotFoundException(); } var oldDate = todo.Due; todo.Edit(model); await _db.SaveChangesAsync(); // Set cache for single and clear related lists _cache.Set(CacheConstants.GetSingleTodoCacheKey(todo.Id, userId), todo, CacheConstants.GetDefaultCacheOptions()); _cache.Remove(CacheConstants.GetAllTodosCacheKey(userId)); _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, oldDate)); if (oldDate.Date != todo.Due.Date) { _cache.Remove(CacheConstants.GetAllTodosForDayCacheKey(userId, todo.Due)); } }