public void test_CreateNewSubTask_Service() { //Returns a Task with Id = 1 var parentTask = this.GetFunctionalTask(); var creatingTaskDto = new CreatingTaskDto() { Name = validTaskName, Description = validTaskDescription, ParentTaskId = parentTask.Id }; var taskRepository = new Mock <ITaskRepository>(); //When SelectById with Parent's Id then return Parent taskRepository.Setup(rep => rep.SelectById(parentTask.Id)) .Returns(parentTask); var taskService = new TaskService(new Mock <IUnitOfWork>().Object, taskRepository.Object); //Check if Parent has NOT a SubTask Assert.IsTrue((parentTask.SubTasks?.Count ?? 0) == 0); taskService.CreateNewSubTask(creatingTaskDto); //Parent Task is still in memory because taskRepository use it's pointer //Check if Parent has a SubTask Assert.IsTrue((parentTask.SubTasks?.Count ?? 0) == 1); }
/// <summary> /// Method to create a new Task and immediatily associate it as a Child to another Task /// </summary> public TaskDto CreateNewSubTask(CreatingTaskDto creatingTaskDto, bool commit = false) { //Select the informed Parent Task or Throw Exception var parentTask = _taskRepository.SelectById(creatingTaskDto.ParentTaskId) ?? throw new BusinessLogicException("To create a new SubTask a existing Task is needed"); //Use the private map to Map the DTO from Application to a Domain entity var childTask = creatingTaskDto.MapToDomain(); //Associates the new SubTask to the informed Parent Task this.AddSubTaskToTask(parentTask, childTask); //Set all the changes to be commited by the Unit of Work _taskRepository.Update(parentTask); if (commit) { //Commit the changes done by the Service _uow.Commit(); } //Do not return Domain to Application return(parentTask.MapToDto()); }
public void test_ThrowException_UpdatingTask_TaskDescription_TooLong() { var task = this.GetFunctionalTask(); var creatingTaskDto = new CreatingTaskDto() { Name = "A Perfect Name", Description = new string('A', this.maxLengthTaskDescription + 1) }; task.UpdateNameAndDescription(creatingTaskDto); }
public void test_ThrowException_UpdatingTask_TaskDescription_Null() { var task = this.GetFunctionalTask(); var creatingTaskDto = new CreatingTaskDto() { Name = "A Perfect Name", Description = null }; task.UpdateNameAndDescription(creatingTaskDto); }
public void test_ThrowException_UpdatingTask_TaskName_TooShort() { var task = this.GetFunctionalTask(); var creatingTaskDto = new CreatingTaskDto() { Name = new string('A', this.minLengthTaskName - 1), Description = "A Perfect Description" }; task.UpdateNameAndDescription(creatingTaskDto); }
/// <summary> /// Method for updating a Task /// </summary> public TaskDto UpdateTask(int id, CreatingTaskDto creatingTaskDto, bool commit = false) { //Find the Task that will be updated or Throw Exception var updatedTask = _taskRepository.SelectById(id) ?? throw new BusinessLogicException("Error while updating Task, informed Id didn't return any result"); updatedTask.UpdateNameAndDescription(creatingTaskDto); //If DTO informs that this Task is a Child Task and that it's Parent has changed if (creatingTaskDto.ParentTaskId > 0 && (updatedTask.ParentTaskId ?? 0) != creatingTaskDto.ParentTaskId) { //if the Child Task already have a Parent if (updatedTask.ParentTaskId.HasValue) { //Find the Parent Task of the updated Task or Throw Exception var currentParentTask = _taskRepository.SelectById(updatedTask.ParentTaskId.Value) ?? throw new BusinessLogicException("Error while updating Task, the curent parent of the updated Task could not be found"); var removedSubTask = currentParentTask.RemoveSubTask(updatedTask); _taskRepository.Update(currentParentTask); //And then remove it's relation with the Child Task CreateSqlCommandToDeleteSubTask(parentTaskId: currentParentTask.Id, childTaskId: updatedTask.Id); } //Find the NEW Parent Task of the updated Task or Throw Exception var newParentTask = _taskRepository.SelectById(creatingTaskDto.ParentTaskId) ?? throw new BusinessLogicException("Error while updating Task, could not find the new parent Task"); //Add the Child Task to the new Parent this.AddSubTaskToTask(newParentTask, updatedTask); } //Set all the changes to be commited by the Unit of Work _taskRepository.Update(updatedTask); if (commit) { //Commit the changes done by the Service _uow.Commit(); } //Do not return Domain to Application return(updatedTask.MapToDto()); }
public IActionResult Post(CreatingTaskDto creatingTaskDto) { TaskDto taskDto; if (creatingTaskDto.ParentTaskId > 0) { taskDto = _taskService.CreateNewSubTask(creatingTaskDto, commit: true); } else { taskDto = _taskService.CreateNewTask(creatingTaskDto, commit: true); } return(Ok(taskDto)); }
/// <summary> /// Method to create a new Task /// </summary> public TaskDto CreateNewTask(CreatingTaskDto creatingTaskDto, bool commit = false) { //Use the private map to Map the DTO from Application to a Domain entity var task = creatingTaskDto.MapToDomain(); //Set all the changes to be commited by the Unit of Work _taskRepository.Insert(task); if (commit) { //Commit the changes done by the Service _uow.Commit(); } //Do not return Domain to Application return(task.MapToDto()); }
public void test_ThrowException_CreateNewSubTask_Service_ParentTask_Is_Null() { var creatingTaskDto = new CreatingTaskDto() { Name = validTaskName, Description = validTaskDescription, ParentTaskId = 1 }; var taskRepository = new Mock <ITaskRepository>(); //For any given Id it returns Null taskRepository.Setup(rep => rep.SelectById(It.IsAny <int>())) .Returns(null as Domain.Task); var taskService = new TaskService(new Mock <IUnitOfWork>().Object, taskRepository.Object); taskService.CreateNewSubTask(creatingTaskDto); }
public static Task MapToDomain(this CreatingTaskDto creatingTaskDto) => new Task(creatingTaskDto.Name, creatingTaskDto.Description);
public void UpdateNameAndDescription(CreatingTaskDto creatingTaskDto) { this.Name = creatingTaskDto.Name; this.Description = creatingTaskDto.Description; }
public ActionResult Put(int id, [FromBody] CreatingTaskDto creatingTaskDto) { var taskDto = _taskService.UpdateTask(id, creatingTaskDto, commit: true); return(Ok(taskDto)); }