Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        public void GetSingleTodoCacheKey_WithUuidAndTodoId_Matches()
        {
            // Arrange
            const string userId = "f7b3d1d1-4c9c-43d3-a929-a6d06d49885c";
            const int    todoId = 55;

            // Act
            var cacheKey = CacheConstants.GetSingleTodoCacheKey(todoId, userId);

            // Assert
            Assert.Equal("c_todo_55_f7b3d1d1-4c9c-43d3-a929-a6d06d49885c", cacheKey);
        }
Esempio n. 3
0
        /// <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));
        }
Esempio n. 4
0
        /// <inheritdoc />
        /// <exception cref="TodoNotFoundException">When todo is not found</exception>
        public async Task <TodoDto> GetTodoByIdAsync(int todoId, string userId)
        {
            var cacheKey = CacheConstants.GetSingleTodoCacheKey(todoId, userId);

            if (!_cache.TryGetValue(cacheKey, out Todo todo))
            {
                todo = await _db.Todo.SingleOrDefaultAsync(t =>
                                                           t.Id == todoId && t.Owner.Id == userId);

                if (todo == null)
                {
                    throw new TodoNotFoundException();
                }
                _cache.Set(cacheKey, todo, CacheConstants.GetDefaultCacheOptions());
            }
            return(new TodoDto(todo));
        }
Esempio n. 5
0
        /// <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);
        }
Esempio n. 6
0
        /// <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));
            }
        }