public CreateTaskOutput Execute(CreateTaskInput input) { if (input is null) { throw new ArgumentNullException(nameof(input)); } if (input.IsValid()) { //create new Domain TaskItem from the supplied input data TaskItem newTask = InputToTaskItem(input); if (taskManager.Add(newTask)) { ITaskItemRepository taskItemRepo = taskItemRepositoryFactory.New(); //Create a TaskItemDAL to save to the database TaskItemDAL taskItemDAL = TaskItemAndInputToDAL(newTask, input); //add the new TaskItemDAL to the database if (taskItemRepo.Add(taskItemDAL)) { //save the changed make to the TaskItemRepository if (taskItemRepo.Save()) { //create DTO to return as Output data TaskItemDTO taskItemDTO = InputToTaskItemDTO(input); //fill output data and return return(new CreateTaskOutput { Success = true, TaskItemDTO = taskItemDTO }); } else { //failed to save state of repository //remove taskItem from domain TaskManager if (!taskManager.Remove(newTask)) { //TaskItem could not be removed. we're now screwed . . . //TODO: decide what to do here } return(new CreateTaskOutput { Success = false, Error = "Unable to save the new Task." }); } } else { //failed to save task to repository //remove taskItem from domain TaskManager if (!taskManager.Remove(newTask)) { //TaskItem could not be removed. we're now screwed . . . //TODO: decide what to do here } return(new CreateTaskOutput { Success = false, Error = "Unable to save the new Task." }); } } else { //unable to add new TaskItem to domain TaskManager return(new CreateTaskOutput { Success = false, Error = "Unable to process the new Task." }); } } else { //Input is not valid return(new CreateTaskOutput { Success = false, Error = input.GetErrorMessage() }); } }
/// <summary> /// Deletes a Task Item from the application /// </summary> /// <param name="input"> /// Holds the input necessary for deleting a TaskItem /// </param> /// <returns> /// The result of deleting the TaskItem /// </returns> public DeleteTaskUseCaseOutput Execute(DeleteTaskUseCaseInput input) { Guid idToDelete = input.Id; ITaskItemRepository taskRepo = taskItemRepositoryFactory.New(); //retrieve task item to delete Maybe <TaskItemDAL> maybeTask = taskRepo.GetById(idToDelete); if (maybeTask.HasValue) { TaskItemDAL taskToDelete = maybeTask.Value; //delete task item from repository if (taskRepo.Delete(idToDelete) == false) { //unable to delete task return(new DeleteTaskUseCaseOutput() { Error = "Unable to to delete TaskItem", Success = false }); } //delete task item from domain if (taskItemManager.Remove(idToDelete) == false) { //failed to remove TaskItem from domain //TODO : handle this appropriately return(new DeleteTaskUseCaseOutput() { Error = "Unable to to delete TaskItem", Success = false }); } INotificationRepository notificationRepo = notificationRepositoryFactory.New(); //iterate through notifications in repository and delete those generated by the deleted task foreach (NotificationDAL notificationDAL in notificationRepo.GetAll()) { if (notificationDAL.taskId == idToDelete) { if (notificationRepo.Delete(notificationDAL) == false) { //unable to delete notification //TODO : handle this appropriately } } } //delete task notifications in domain if (notificationManager.Remove(idToDelete) == false) { //failed to remove task notifications from domain //TODO : handle this appropriately } notificationRepo.Save(); taskRepo.Save(); } else { return(new DeleteTaskUseCaseOutput() { Error = "Unable to find TaskItem to delete", Success = false }); } return(new DeleteTaskUseCaseOutput() { Success = true }); }