public async Task <IActionResult> CreateTodo([FromBody] CreateTodoViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var id = await _todoService.CreateTodoAsync(model, GetUserId()); return(CreatedAtRoute( MethodNames.GetSingleTodoMethodName, new { todoId = id }, null )); } catch (UserNotFoundException) { return(Unauthorized()); } }
public async Task <ActionResult <TodoModel> > CreateTodoAsync(CreateTodoModel createTodoModel) { var todoModel = new TodoModel { Description = createTodoModel.Description, IsCompleted = createTodoModel.IsCompleted }; var createdTodo = await _todoService.CreateTodoAsync(todoModel); return(CreatedAtAction(nameof(GetTodoAsync), new { id = createdTodo.Id }, createdTodo)); }
public async Task <IActionResult> CreateTodoAsync([FromBody] TodoListDto dto) { if (!ModelState.IsValid) { return(BadRequest("The given model is incorrectly formatted.")); } var todo = await _todoService.CreateTodoAsync(dto); if (todo == null) { return(BadRequest("Todo could not be created.")); } return(CreatedAtRoute("GetTodoById", new { id = todo.Id }, todo)); }
public async Task <IActionResult> Create(Todo todo, Guid TodoType) { if (ModelState.IsValid) { todo.AddModel(UserId); var _todoType = await _todoService.FindTodoTypeByIdAsync(TodoType); todo.TodoType = _todoType; var result = await _todoService.CreateTodoAsync(todo); if (result) { return(Redirect("/")); } return(RedirectToAction("Index")); } else { return(View(todo)); } }
public async Task CreateTodo() { var fakeUserid = Guid.NewGuid(); var fakeType = new TodoType { Name = "活动" }; fakeType.AddModel(fakeUserid); var fakeTodo = new Todo { Title = "创建明日社区活动", TodoType = fakeType, OffTime = DateTime.Now }; fakeTodo.AddModel(fakeUserid); await _todoService.CreateTodoAsync(fakeTodo); var title = await _inMemoryContext.Todos.Where(c => c.Title == fakeTodo.Title).Select(c => c.Title).FirstOrDefaultAsync(); Assert.Equal(1, await _inMemoryContext.Todos.Where(c => c.Id == fakeTodo.Id).CountAsync()); Assert.False(string.IsNullOrEmpty(title)); }
public async Task <IActionResult> Create([FromBody] CreateTodoRequest todoRequest) { if (string.IsNullOrEmpty(todoRequest.Name)) { return(BadRequest()); } var newTodoId = Guid.NewGuid(); var todo = new Todo { Id = newTodoId, Name = todoRequest.Name, IsCompleted = todoRequest.IsCompleted }; await _todoService.CreateTodoAsync(todo); var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var locationUri = baseUrl + "/" + ApiRoutes.Todos.Get.Replace("{todoId}", newTodoId.ToString()); return(Created(locationUri, _mapper.Map <TodoResponse>(todo))); }
//public IActionResult Create_Todo([FromBody] Todo todoToCreate) //{ // var _ = _todoService.CreateTodoAsync(todoToCreate); // return CreatedAtRoute("GetTodo", new { id = todoToCreate.Id }, todoToCreate); //} public async Task <Todo> Create_Todo([FromForm] Todo todoToCreate) { return(await _todoService.CreateTodoAsync(todoToCreate)); }
public async Task <IActionResult> CreateTodo(TodoFormDto dto) { var todo = await _todoService.CreateTodoAsync(dto); return(CreatedAtAction(nameof(GetTodo), new { todo.Id }, todo)); }