/// <summary>
        /// Executes the command
        /// </summary>
        public async Task Execute(int id)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // gets the current user
            User currentUser = await userRepository.Where(e => e.Email == userInfoProvider.UserName).AsNoTracking().FirstOrDefaultAsync();

            if (currentUser is null)
            {
                throw new BusinessException("User does not exist");
            }

            // gets the todo-list to remove
            TodoList removingTodoList = await todoListRepository.GetById(id).FirstOrDefaultAsync();

            if (removingTodoList is null)
            {
                throw new BusinessException("Removing todo-list does not exist");
            }
            if (!currentUser.IsAdmin && removingTodoList.UserId != currentUser.Id)
            {
                throw new BusinessException("User cannot delete a todo-list not belonging him");
            }

            // removes the todo-list
            await todoListRepository.RemoveAsync(removingTodoList.Id);

            // saves made changes
            await changesSaver.SaveChangesAsync();
        }
예제 #2
0
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <returns>created todo-list</returns>
        public async Task <TodoList> Execute(TodoListInputModel model)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // pretreatment of model
            model.CheckAndPrepare();

            // gets the current user
            User currentUser = await userRepository.Where(e => e.Email == userInfoProvider.UserName).AsNoTracking().FirstOrDefaultAsync();

            if (currentUser is null)
            {
                throw new BusinessException("User does not exist");
            }

            // creating object
            TodoList todoList = new TodoList
            {
                Title  = model.Title,
                UserId = currentUser.Id,
            };
            await todoListRepository.AddAsync(todoList);

            // saving made changes
            await changesSaver.SaveChangesAsync();

            return(todoList);
        }
예제 #3
0
        /// <summary>
        /// Get grid data for UI
        /// </summary>
        public async Task <DataSourceResult> BuildForGrid(DataSourceRequest request)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            return(await todoListRepository
                   .Where(e => e.User.Email == userInfoProvider.UserName)
                   .ProjectTo <TodoListViewModel>()
                   .ToDataSourceResultAsync(request));
        }
예제 #4
0
        /// <summary>
        /// Get grid data for UI
        /// </summary>
        public async Task <DataSourceResult> BuildForGrid(int todoListId, DataSourceRequest request)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            return(await todoListRepository
                   .Where(e => e.Id == todoListId && (userInfoProvider.UserRole == UserRole.Admin || e.User.Email == userInfoProvider.UserName))
                   .SelectMany(e => e.Items)
                   .ProjectTo <TodoListItemViewModel>()
                   .ToDataSourceResultAsync(request));
        }
예제 #5
0
        /// <summary>
        /// Build the query
        /// </summary>
        public async Task <TodoListViewModel> Build(int id)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // if item belongs to the current user
            if (!await todoListRepository.AnyAsync(e => e.Id == id && e.User.Email == userInfoProvider.UserName))
            {
                throw new BusinessException("User does not have the todo-list with specified id");
            }

            return(await todoListRepository.GetById(id).ProjectTo <TodoListViewModel>().FirstOrDefaultAsync());
        }
예제 #6
0
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <returns>updated todo-list item</returns>
        public async Task <TodoListItem> Execute(int id, TodoListItemInputModel model)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // pretreatment of model
            model.CheckAndPrepare();

            // specific validation
            if (!typeof(TodoListItemState).IsEnumDefined(model.State))
            {
                throw new BusinessException("State of todo-list item specified not correctly");
            }

            // gets the current user
            User currentUser = await userRepository.Where(e => e.Email == userInfoProvider.UserName).AsNoTracking().FirstOrDefaultAsync();

            if (currentUser is null)
            {
                throw new BusinessException("User does not exist");
            }

            // gets the todo-list item
            TodoListItem todoListItem = await todoListItemRepository.GetById(id).Include(e => e.TodoList.User).FirstOrDefaultAsync();

            if (todoListItem is null)
            {
                throw new BusinessException("Todo-list item does not exist");
            }
            if (!currentUser.IsAdmin && todoListItem.TodoList.UserId != currentUser.Id)
            {
                throw new BusinessException("User cannot change a todo-list item not belonging to his todo-list");
            }

            // updates the todo-list
            todoListItem.Title = model.Title;
            todoListItem.State = model.State;

            // saves made changes
            await changesSaver.SaveChangesAsync();

            return(todoListItem);
        }
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <returns>created todo-list item</returns>
        public async Task <TodoListItem> Execute(TodoListItemInputModel model)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // pretreatment of model
            model.CheckAndPrepare();

            // specific validation
            if (!typeof(TodoListItemState).IsEnumDefined(model.State))
            {
                throw new BusinessException("State of todo-list item specified not correctly");
            }

            // gets the current user
            User currentUser = await userRepository.Where(e => e.Email == userInfoProvider.UserName).AsNoTracking().Include(e => e.TodoLists).FirstOrDefaultAsync();

            if (currentUser is null)
            {
                throw new BusinessException("User does not exist");
            }
            if (!currentUser.TodoLists.Any(e => e.Id == model.TodoListId))
            {
                throw new BusinessException("Todo-list for which you want to add the specified item does not belong to the current user");
            }

            // creating an object
            TodoListItem todoListItem = new TodoListItem
            {
                Title      = model.Title,
                TodoListId = model.TodoListId,
                State      = model.State
            };
            await todoListItemRepository.AddAsync(todoListItem);

            // saving made changes
            await changesSaver.SaveChangesAsync();

            return(todoListItem);
        }
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <returns>updated todo-list</returns>
        public async Task <TodoList> Execute(int id, TodoListInputModel model)
        {
            // Check access rights: User
            await accessRightChecker.CheckUserAuthToken();

            // pretreatment of model
            model.CheckAndPrepare();

            // gets the current user
            User currentUser = await userRepository.Where(e => e.Email == userInfoProvider.UserName).AsNoTracking().FirstOrDefaultAsync();

            if (currentUser is null)
            {
                throw new BusinessException("User does not exist");
            }

            // gets the todo-list
            TodoList todoList = await todoListRepository.GetById(id).FirstOrDefaultAsync();

            if (todoList is null)
            {
                throw new BusinessException("Todo-list does not exist");
            }
            if (!currentUser.IsAdmin && todoList.UserId != currentUser.Id)
            {
                throw new BusinessException("User cannot change a todo-list not belonging him");
            }

            // updates the todo-list
            todoList.Title = model.Title;

            // saves made changes
            await changesSaver.SaveChangesAsync();

            return(todoList);
        }