コード例 #1
0
        public List <TaskNote> GetDescendants(int id)
        {
            using (TaskContext db = new TaskContext(options))
            {
                TaskNote task = db.TaskNotes.Where(t => t.Id == id).SingleOrDefault();
                if (task == null)
                {
                    throw new NotFoundException("Task is not found in database", id);
                }

                db.TaskTreeNodes.Where(ttn => ttn.AncestorId == task.Id).Include(ttn => ttn.Descendat).Load();

                List <TaskNote> taskNotes = new List <TaskNote>();
                if (task.TaskTreeNodes == null)
                {
                    return(taskNotes);
                }

                foreach (TaskTreeNode item in task.TaskTreeNodes)
                {
                    taskNotes.Add(item.Descendat);
                }

                return(taskNotes);
            }
        }
コード例 #2
0
 public virtual void Update(TaskNote task)
 {
     using (TaskContext db = new TaskContext(options))
     {
         db.TaskNotes.Update(task);
         db.SaveChanges();
     }
 }
コード例 #3
0
        public bool HasDescendants(int id)
        {
            using (TaskContext db = new TaskContext(options))
            {
                TaskNote task = db.TaskNotes.Include(t => t.TaskTreeNodes).Where(t => t.Id == id).Single();
                if (task == null)
                {
                    throw new NotFoundException("Task is not found in database", id);
                }

                return(task.TaskTreeNodes.Count == 0 ? false : true);
            }
        }
コード例 #4
0
        public bool IsTerminal(int id)
        {
            using (TaskContext db = new TaskContext(options))
            {
                TaskNote task = db.TaskNotes.Include(t => t.TaskTreeNodes).Where(t => t.Id == id).SingleOrDefault();
                if (task == null)
                {
                    throw new NotFoundException("Task is not found in database", id);
                }

                return(task.TaskTreeNodes.Count == 0);
            }
        }
コード例 #5
0
        public TaskNote GetAncestor(int descendatId)
        {
            using (TaskContext db = new TaskContext(options))
            {
                TaskNote task = db.TaskNotes.Where(t => t.Id == descendatId).FirstOrDefault();
                if (task == null)
                {
                    throw new NotFoundException("Task is not found in database", descendatId);
                }

                db.TaskTreeNodes.Where(ttn => ttn.DescendantId == task.Id).Include(ttn => ttn.Ancestor).Load();

                return(task.TaskTreeNode?.Ancestor);
            }
        }
コード例 #6
0
        public virtual void Remove(int id)
        {
            using (TaskContext db = new TaskContext(options))
            {
                TaskNote task = db.TaskNotes.Include(t => t.TaskTreeNode).Include(t => t.TaskTreeNodes).Where(t => t.Id == id).FirstOrDefault();
                if (task == null)
                {
                    throw new NotFoundException("Task is not found in database", id);
                }
                if (task.TaskTreeNodes.Count != 0)
                {
                    throw new NonTerminalException("Task is not terminal node", id);
                }

                if (task.TaskTreeNode != null)
                {
                    db.TaskTreeNodes.Remove(task.TaskTreeNode);
                }
                db.TaskNotes.Remove(task);
                db.SaveChanges();
            }
        }
コード例 #7
0
        public virtual void Add(TaskNote task, TaskNote ancestor = null)
        {
            using (TaskContext db = new TaskContext(options))
            {
                if (db.TaskNotes.Find(task.Id) != null)
                {
                    throw new AddingException("Task already in database", task.Id);
                }

                db.TaskNotes.Add(task);
                if (ancestor != null)
                {
                    if (db.TaskNotes.Find(ancestor.Id) == null)
                    {
                        throw new NotFoundException("Parent is not found in database", ancestor.Id);
                    }
                    db.TaskTreeNodes.Add(new TaskTreeNode {
                        AncestorId = ancestor.Id, DescendantId = task.Id
                    });
                }
                db.SaveChanges();
            }
        }