Beispiel #1
0
        public async Task <IActionResult> UpdateTodo(string user, long id, [FromBody] EditTodoModel model)
        {
            if (!TodoAppUser.IsAllowed(user))
            {
                return(NotFound());
            }

            var todo = await db.Todos
                       .FirstOrDefaultAsync(t => t.Id == id && t.UserName == user);

            if (todo == null)
            {
                return(NotFound());
            }

            todo.Description   = model.Description;
            todo.IsComplete    = model.IsComplete;
            todo.LastUpdatedAt = DateTimeOffset.Now;

            db.Todos.Update(todo);

            await db.SaveChangesAsync();

            return(Ok(todo));
        }
        public async Task <EditTodoModel> GetTodoForEdit(string id)
        {
            var toDo = this.GetById(id);

            if (toDo != null)
            {
                var result = new EditTodoModel()
                {
                    Description = toDo.Description,
                    AlertOn     = toDo.AlertOn,
                    IsActive    = toDo.IsActive
                };

                return(result);
            }

            throw new InvalidOperationException(GlobalContstants.InvalidOperationErrors.GetCurrentEdit);
        }
        public async Task <bool> EditTodoAsync(string id, EditTodoModel model)
        {
            var todo = this.GetById(id);

            if (todo != null)
            {
                todo.Description = model.Description;
                todo.AlertOn     = model.AlertOn;
                todo.CreatedOn   = DateTime.Now;

                this.db.Todos.Update(todo);

                await this.db.SaveChangesAsync();

                return(true);
            }

            throw new InvalidOperationException(GlobalContstants.InvalidOperationErrors.EditTodo);
        }
        public async Task <IActionResult> Edit([FromRoute] string todoId, EditTodoModel model)
        {
            await this.toDoService.EditTodoAsync(todoId, model);

            return(this.RedirectToAction("All", "Todo"));
        }