private async Task <TodoTaskDTO> CreateToDoTaskTest(int listId , string title, string description) { // Arrange CreateTodoTaskDTO createToDoTaskDTO1 = new CreateTodoTaskDTO { ToDoListId = listId, Title = title, Description = description }; var content = JsonConvert.SerializeObject(createToDoTaskDTO1); var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); // Act var response = await _client.PostAsync("/api/TodoTasks/", stringContent); // Assert response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); TodoTaskDTO todoTaskDTO = JsonConvert.DeserializeObject <TodoTaskDTO>(responseString); Assert.NotNull(todoTaskDTO); return(todoTaskDTO); }
public Task <TodoDTO> CreateTodoTaskAsync(string TodoId, CreateTodoTaskDTO subTodo) { Expression <Func <Todo, bool> > todoExpr = x => x.Id.ToString() == TodoId; var todo = _todoRepository.FindOne(todoExpr).ConvertTo(); return(Task.FromResult(todo)); }
public void CreateTaskTest_ReturnCreatedTask() { #region Arrange User user = new User(1, "Name1", "Email1", "Pass1"); CreateTodoTaskDTO createToDoTaskDTO = new CreateTodoTaskDTO { ToDoListId = 1, Title = "Title1", Description = "Description1" }; TodoList todoList = new TodoList { Id = 1, UserId = 1, Title = "List1" }; TodoTask createdTodoTask = new TodoTask(createToDoTaskDTO); Extensions.Extensions.IsUnitTest = true; model = new Mock <IRepository>(); model.Setup(repo => repo.GetUserById(user.Id)).Returns(Task.FromResult(user)); model.Setup(repo => repo.GetTodoListByListIdAndUserId( createToDoTaskDTO.ToDoListId, user.Id)).Returns(Task.FromResult(todoList)); model.Setup(repo => repo.AddTodoTask(createdTodoTask)); #endregion // Act controller = new TodoTasksController(model.Object); var result = controller.CreateTask(createToDoTaskDTO); // Assert var okObjectResult = Assert.IsType <CreatedResult>(result.Result); Assert.Equal(createdTodoTask.Id, (okObjectResult.Value as TodoTaskDTO).Id); }
public void CreateTaskTest_ReturnUserNotFound() { #region Arrange User user = null; CreateTodoTaskDTO createToDoTaskDTO = new CreateTodoTaskDTO { ToDoListId = 1, Title = "Title1", Description = "Description1" }; TodoList todoList = new TodoList { Id = 1, UserId = 1, Title = "List1" }; TodoTask createdTodoTask = new TodoTask(createToDoTaskDTO); int userId = 2; Extensions.Extensions.IsUnitTest = true; model = new Mock <IRepository>(); model.Setup(repo => repo.GetUserById(userId)).Returns(Task.FromResult(user)); #endregion // Act controller = new TodoTasksController(model.Object); var result = controller.CreateTask(createToDoTaskDTO); // Assert var okObjectResult = Assert.IsType <NotFoundObjectResult>(result.Result); }
public void CreateTaskTest_ReturnBadRequestInvalidModel() { // Arrange User user = new User(1, "Name1", "Email1", "Pass1"); CreateTodoTaskDTO createToDoTaskDTO = null; Extensions.Extensions.IsUnitTest = true; model = new Mock <IRepository>(); // Act controller = new TodoTasksController(model.Object); var result = controller.CreateTask(createToDoTaskDTO); // Assert var okObjectResult = Assert.IsType <BadRequestObjectResult>(result.Result); }
public ActionResult <Task <CreateTodoTaskDTO> > CreateTodoTask([FromQuery] string TodoId, [FromBody] CreateTodoTaskDTO createTodoTaskDTO) { try { _todoService.CreateTodoTaskAsync(TodoId, createTodoTaskDTO); } catch (TodoValidationException todoValidationEx) when(todoValidationEx.InnerException is NotFoundUserException) { return(NotFound(todoValidationEx.InnerException.Message)); } return(NoContent()); }
public TodoTask(CreateTodoTaskDTO createToDoTaskDTO) { ToDoListId = createToDoTaskDTO.ToDoListId; Title = createToDoTaskDTO.Title; Description = createToDoTaskDTO.Description; }
public async Task <IActionResult> CreateTask([FromBody] CreateTodoTaskDTO createToDoTaskDTO) { if (!ModelState.IsValid) { return(BadRequest("Error: Model state is not valid.")); } if (createToDoTaskDTO == null) { return(BadRequest("Error: Model state is not valid.")); } if (createToDoTaskDTO.ToDoListId < 1) { return(BadRequest("Error: Todo list id cannot be negative.")); } if (createToDoTaskDTO.Title == null || createToDoTaskDTO.Title == String.Empty) { return(BadRequest("Error: Todo task title cannot be empty.")); } if (createToDoTaskDTO.Description == null || createToDoTaskDTO.Description == String.Empty) { return(BadRequest("Error: Todo task description cannot be empty.")); } int userId = User.GetUserId(); User user = await _context.GetUserById(userId); if (user == null) { return(NotFound("Error: User with this id not found.")); } TodoList existTodoList = await _context .GetTodoListByListIdAndUserId(createToDoTaskDTO.ToDoListId, userId); if (existTodoList == null) { return(NotFound("Error: This user not have todo list with this id")); } TodoTask todoTask = new TodoTask(createToDoTaskDTO.ToDoListId , createToDoTaskDTO.Title, createToDoTaskDTO.Description); todoTask.TaskStatus = TodoTask.Status.AWAIT; _context.AddTodoTask(todoTask); if (Extensions.Extensions.IsUnitTest) { return(Created("localhost", new TodoTaskDTO(todoTask))); } string webRootPath = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; string objectLocation = webRootPath + "/" + "api/TodoTasks/" + todoTask.Id.ToString(); return(Created(objectLocation, new TodoTaskDTO(todoTask))); }