public IActionResult CreateTodoItems([FromBody] TodoItemForCreationViewModel todoItemForCreationViewModel) { if (todoItemForCreationViewModel == null) { return(BadRequest()); } if (!ModelState.IsValid) { // return 422 - Unprocessable Entity when validation fails return(new UnprocessableEntityObjectResult(ModelState)); } var todoItem = Mapper.Map <Entities.TodoItem>(todoItemForCreationViewModel); var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value; todoItem.OwnerId = ownerId; // add and save. _todoRepository.AddTodoItem(todoItem); if (!_todoRepository.Save()) { throw new Exception($"Adding an todoitem failed on save."); } var todoItemViewModel = Mapper.Map <TodoItemViewModel>(todoItem); return(CreatedAtRoute("GetTodoItem", new { id = todoItemViewModel.Id }, todoItemViewModel)); }
public async Task <ActionResult> AddTodoItem(TodoItem todoItem) { var todoItemAdd = new TodoItem { Description = todoItem.Description, CreationTime = DateTime.Now, Status = "Open" }; var saveTodoItem = await _context.AddTodoItem(todoItemAdd); return(StatusCode(201)); }
async public Task <IActionResult> Create([FromBody] TodoItem item) { if (item == null) { return(BadRequest()); } try { await _todoRepo.AddTodoItem(item); return(CreatedAtRoute("GetTodo", new { id = item.Id }, item)); } catch (Exception) { return(BadRequest()); } }