public void InitializeDomainFromDatabase( ITaskItemRepositoryFactory taskItemRepositoryFactory, INotificationManager notificationManager, ITaskManager taskManager, IClock clock) { /* * hook up to notfication added event now, so to retrieve new notifications generated by * the TaskItems being created below */ notificationManager.NotificationAdded += OnNotificationAdded; /* * read in task items from database. Create domain taskItems from * data and add items to taskManager */ ITaskItemRepository taskItemRepository = taskItemRepositoryFactory.New(); foreach (TaskItemDAL task in taskItemRepository.GetAll()) { INotificationFrequency notificationFrequency = null; if (task.customNotificationFrequency.HasValue) { CustomNotificationFrequencyDAL frequencyDAL = task.customNotificationFrequency.Value; notificationFrequency = NotificationFrequencyFactory.New( //TODO: do something safer than just a cast (NotificationFrequencyType)task.notificationFrequencyType, frequencyDAL.time ); } else { notificationFrequency = NotificationFrequencyFactory.New( //TODO: do something safer than just a cast (NotificationFrequencyType)task.notificationFrequencyType ); } taskManager.Add( new TaskItem( task.title, task.description, new Colour(task.r, task.g, task.b), task.startTime, notificationManager, notificationFrequency, clock, task.lastNotificationTime, task.id ) ); } }
/// <summary> /// Executes the logic of the <see cref="ViewTasksUseCase"/>. Retrieves TaskItem data and /// stores it in the <see cref="ViewTasksUseCase"/>'s Output property. /// </summary> public ViewTasksOutput Execute(ViewTasksInput input) { ITaskItemRepository taskRepo = taskItemRepositoryFactory.New(); ViewTasksOutput output = new ViewTasksOutput { Success = true }; /* * go through all taskItems in database then add them to the Output property as * TaskItemDTO's. */ foreach (TaskItem domainTask in taskManager.GetAll()) { //retrieve dataLayer task which carries notification frequency description Maybe <TaskItemDAL> maybeTask = taskRepo.GetById(domainTask.ID); if (maybeTask.HasValue) { TaskItemDAL taskDAL = maybeTask.Value; TimeSpan customFrequencyTime = new TimeSpan(); //if the current taskItem has a custom frequency type, then retrieve its Time value if (taskDAL.customNotificationFrequency.HasValue) { CustomNotificationFrequencyDAL frequencyDAL = taskDAL.customNotificationFrequency.Value; customFrequencyTime = frequencyDAL.time; } DTO.TaskItemDTO taskDTO = new DTO.TaskItemDTO() { Id = domainTask.ID, Title = domainTask.Title, Description = domainTask.Description, R = domainTask.Colour.R, G = domainTask.Colour.G, B = domainTask.Colour.B, StartTime = domainTask.StartTime, //TODO: prefer to do a better conversion that just a cast to an enum NotificationFrequencyType = (NotificationFrequencyType)taskDAL.notificationFrequencyType, CustomNotificationFrequency = customFrequencyTime }; output.TaskItems.Add(taskDTO); } } return(output); }
protected void OnNotificationAdded(object source, Notification notification) { //update database with new notification NotificationDAL notificationDal = new NotificationDAL { taskId = notification.Producer.ID, time = notification.Time }; INotificationRepository notificationRepo = notificationRepositoryFactory.New(); notificationRepo.Add(notificationDal); ITaskItemRepository taskRepo = taskItemRepositoryFactory.New(); List <TaskItemDAL> tasks = new List <TaskItemDAL>(taskRepo.GetAll()); if (notification.Producer is TaskItem task) { //update the notifications producer in the database TaskItemDAL taskItemDAL = tasks.Find(t => t.id == task.ID); taskItemDAL.lastNotificationTime = task.LastNotificationTime; if (taskRepo.Update(taskItemDAL) == false) { //could not update task in database } //create DTO to invoke NotificationAdded with NotificationDTO dto = new NotificationDTO() { TaskId = task.ID, Time = notification.Time, Title = notification.Producer.Title, R = task.Colour.R, G = task.Colour.G, B = task.Colour.B }; //invoked event delegates NotificationAdded?.Invoke(source, dto); } taskRepo.Save(); notificationRepo.Save(); }
/// <summary> /// Executes the ViewNotificationsUseCase. Sets the Output property of the UseCase object /// once complete /// </summary> public ViewNotificationsOutput Execute(ViewNotificationsInput input) { //no input for use case ITaskItemRepository taskRepo = taskItemRepositoryFactory.New(); ViewNotificationsOutput output = new ViewNotificationsOutput { Success = true }; /* * Get all notifications that are present in the application, convert them to DTO's, then * add them to a collection to return */ INotificationRepository notificationRepository = notificationRepositoryFactory.New(); foreach (NotificationDAL notification in notificationRepository.GetAll()) { Maybe <TaskItemDAL> maybeTask = taskRepo.GetById(notification.taskId); if (maybeTask.HasValue) { TaskItemDAL taskDAL = maybeTask.Value; NotificationDTO dto = new NotificationDTO() { TaskId = taskDAL.id, Title = taskDAL.title, Time = notification.time, R = taskDAL.r, G = taskDAL.g, B = taskDAL.b }; output.Notifications.Add(dto); } } return(output); }
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 }); }