示例#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);
        }
示例#2
0
        public void GetAllTodosForDayCacheKey_WithUuidAndTodoId_Matches()
        {
            // Arrange
            const string id  = "7d02b709-d300-44e5-90a7-39fc233d0ca9";
            var          day = new DateTime(1999, 2, 10, 4, 22, 1);

            // Act
            var cacheKey = CacheConstants.GetAllTodosForDayCacheKey(id, day);

            // Assert
            Assert.Equal("c_todos_7d02b709-d300-44e5-90a7-39fc233d0ca9_1999_2_10", cacheKey);
        }
示例#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));
        }
示例#4
0
        /// <summary>
        /// Filters the list by a date (ignoring time).
        /// </summary>
        /// <param name="date">Valid date to filter by</param>
        /// <param name="userId">The id of the token owner</param>
        /// <returns>List of todos</returns>
        private async Task <IEnumerable <TodoDto> > GetAllTodosForDayOrderedByDueAsync(DateTime date, string userId)
        {
            var cacheKey = CacheConstants.GetAllTodosForDayCacheKey(userId, date);

            if (!_cache.TryGetValue(cacheKey, out List <TodoDto> todos))
            {
                todos = await(from t in _db.Todo
                              where t.Due.Date == date && t.Owner.Id == userId
                              orderby t.Due
                              select new TodoDto(t)).ToListAsync();
                if (todos.Any())
                {
                    _cache.Set(cacheKey, CacheConstants.GetDefaultCacheOptions());
                }
            }
            return(todos);
        }
示例#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);
        }
示例#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));
            }
        }