Beispiel #1
0
        public async Task <IActionResult> EditListItem([FromBody] TodoItemEditFields fields)
        {
            var responsibleParty = await dbContext.Users.FirstOrDefaultAsync(user => user.Id == fields.ResponsiblePartyId);

            if (responsibleParty == null)
            {
                ModelState.AddModelError(nameof(fields.ResponsiblePartyId), "Responsible party does not exist");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var existingTodoItem = await dbContext.TodoItems
                                   .Include(item => item.TodoList)
                                   .FirstOrDefaultAsync(item => item.TodoItemId == fields.TodoItemId);

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

            TodoItemEditFieldsFactory.Update(fields, existingTodoItem);

            dbContext.Update(existingTodoItem);
            await dbContext.SaveChangesAsync();

            return(Ok(TodoItemEditFieldsFactory.Create(existingTodoItem)));
        }
        public async Task <IActionResult> Edit(TodoItemEditFields fields)
        {
            if (!ModelState.IsValid)
            {
                return(View(fields));
            }

            var todoItem = dbContext.SingleTodoItem(fields.TodoItemId);

            TodoItemEditFieldsFactory.Update(fields, todoItem);

            dbContext.Update(todoItem);
            await dbContext.SaveChangesAsync();

            return(RedirectToListDetail(todoItem.TodoListId));
        }