示例#1
0
 public override Entities.Task CreateTask(Entities.Task task)
 {
     Entities.Task newTask = null;
     using (TimeTrackerDbContext context = this.GetContext())
     {
         newTask = context.Tasks.Add(task);
         context.SaveChanges();
     }
     this.context = null;
     return this.GetTask(newTask.ID);
 }
示例#2
0
 public override Entities.Tag CreateTag(Entities.Tag tag)
 {
     Entities.Tag newTag = null;
     using (TimeTrackerDbContext context = this.GetContext())
     {
         newTag = context.Tags.Add(tag);
         context.SaveChanges();
     }
     this.context = null;
     return this.GetTag(newTag.ID);
 }
示例#3
0
 public override Entities.Category CreateCategory(Entities.Category category)
 {
     Entities.Category newCategory = null;
     using (TimeTrackerDbContext context = this.GetContext())
     {
         newCategory = context.Categories.Add(category);
         context.SaveChanges();
     }
     this.context = null;
     return this.GetCategory(newCategory.ID);
 }
示例#4
0
 public override void Dispose()
 {
     if (this.context != null)
     {
         try
         {
             this.context.Dispose();
         }
         catch (System.ObjectDisposedException)
         {
         }
         this.context = null;
     }
 }
示例#5
0
 public EntityManager(string nameOrConnectionString, string provider)
 {
     // TODO: Complete member initialization
     this.ConnnectionString = nameOrConnectionString;
     this.Provider = provider;
     using (TimeTrackerDbContext contex = new TimeTrackerDbContext())
     {
         Console.WriteLine(contex.Database.Connection);
         if (!contex.Database.Exists())
         {
             contex.Database.CreateIfNotExists();
         }
     }
 }
示例#6
0
        public override Entities.Tag DeleteTag(Entities.Tag tag)
        {
            Entities.Tag newTag = null;
            using (TimeTrackerDbContext context = this.GetContext())
            {
                newTag = context.Tags.FirstOrDefault(t => t.ID == tag.ID);
                if (newTag != null)
                {
                    context.Tags.Remove(newTag);
                    //tasks ?

                    context.SaveChanges();
                }
            }
            this.context = null;
            return newTag;
        }
示例#7
0
        public override Entities.Category DeleteCategory(Entities.Category category)
        {
            Entities.Category newCategory = null;
            using (TimeTrackerDbContext context = this.GetContext())
            {
                newCategory = context.Categories.FirstOrDefault(c => c.ID == category.ID);
                if (newCategory != null)
                {
                    newCategory = context.Categories.Remove(newCategory);
                    // tasks?

                    context.SaveChanges();
                }
            }
            this.context = null;
            return newCategory;
        }
示例#8
0
        public override Entities.Task DeleteTask(Entities.Task task)
        {
            Entities.Task newTask = null;
            using (TimeTrackerDbContext context = this.GetContext())
            {
                newTask = context.Tasks.FirstOrDefault(t => t.ID == task.ID);
                if (newTask != null)
                {
                    context.Tasks.Remove(newTask);
                    //tags ?
                    //Categories ?

                    context.SaveChanges();
                }
            }
            this.context = null;
            return newTask;
        }
示例#9
0
 protected virtual TimeTrackerDbContext GetContext()
 {
     if (this.context == null)
     {
         this.context = new TimeTrackerDbContext();
     }
     return this.context;
 }
示例#10
0
        public override Entities.Category UpdateCategory(Entities.Category category)
        {
            Entities.Category newCategory = null;
            using (TimeTrackerDbContext context = this.GetContext())
            {
                newCategory = context.Categories.FirstOrDefault(c => c.ID == category.ID);
                if (newCategory != null)
                {
                    newCategory.Group = category.Group;
                    newCategory.Name = category.Name;
                    //tasks?

                    context.SaveChanges();
                }
            }
            this.context = null;
            return this.GetCategory(newCategory.ID);
        }
示例#11
0
 public override IEnumerable<Entities.Tag> GetTaskTags(Entities.Task task)
 {
     Entities.Task newTask = null;
     IEnumerable<Entities.Tag> result = new List<Entities.Tag>();
     using (TimeTrackerDbContext context = this.GetContext())
     {
         newTask = context.Tasks.FirstOrDefault(t => t.ID == task.ID);
         if (newTask != null)
         {
            result = newTask.Tags.ToList();
         }
     }
     this.context = null;
     return result;
 }
示例#12
0
 public override Entities.Task UpdateTask(Entities.Task task)
 {
     Entities.Task newTask = null;
     using (TimeTrackerDbContext context = this.GetContext())
     {
         newTask = context.Tasks.FirstOrDefault(t => t.ID == task.ID);
         if (newTask != null)
         {
             newTask.CategoryID = task.CategoryID;
             newTask.End = task.End;
             newTask.Name = task.Name;
             newTask.Description = task.Description;
             newTask.Start = task.Start;
             context.SaveChanges();
         }
     }
     this.context = null;
     return this.GetTask(newTask.ID);
 }
示例#13
0
        public override Entities.Task TagTask(Entities.Task task, IEnumerable<Entities.Tag> tags)
        {
            Entities.Task newTask = null;
            using (TimeTrackerDbContext context = this.GetContext())
            {
                newTask = context.Tasks.FirstOrDefault(t => t.ID == task.ID);
                if (newTask != null)
                {
                    foreach (Entities.Tag tag in tags)
                    {
                        if (newTask.Tags.FirstOrDefault(t => t.ID == tag.ID) == null)
                        {
                            Entities.Tag addTag = context.Tags.FirstOrDefault(t => t.ID == tag.ID);
                            if(addTag != null)
                            {
                                newTask.Tags.Add(addTag);
                            }
                        }
                    }
                    context.SaveChanges();
                    newTask = this.GetTask(newTask.ID);
                    List<Entities.Tag> removeTags = new List<Entities.Tag>();
                    foreach (Entities.Tag tag in newTask.Tags)
                    {
                        if (tags.FirstOrDefault(t => t.ID == tag.ID) == null)
                        {
                            Entities.Tag removeTag = context.Tags.FirstOrDefault(t => t.ID == tag.ID);
                            if (removeTag != null)
                            {
                                removeTags.Add(removeTag);
                            }
                        }
                    }
                    if (removeTags.Count > 0)
                    {
                        foreach (Entities.Tag tag in removeTags)
                        {
                            newTask.Tags.Remove(tag);
                        }
                        context.SaveChanges();
                    }

                }
            }
            this.context = null;
            return (newTask != null) ? this.GetTask(newTask.ID) : null;
        }