Example #1
0
        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);
        }
        /// <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
            });
        }