Пример #1
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);
        }