示例#1
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);
        }
        /// <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);
        }
示例#3
0
 public async Task Update(int id, [FromBody] TodoListInputModel model) => await updateTodoListCommand.Execute(id, model);
示例#4
0
 public async Task Create([FromBody] TodoListInputModel model) => await createTodoListCommand.Execute(model);