public IList<string> AllCategoriesToList()
 {
     using (var context = new EFDbContext())
     {
         return context.Categories.Select(c => c.Name).ToList();
     }
 }
 public IList<string> AllTagsToList()
 {
     using (var context = new EFDbContext())
     {
         return context.Tags.Select(t => t.Name).ToList();
     }
 }
 public List<Category> Categories()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Categories
             .Include(x => x.Posts)
             .ToList();
     }
 }
 public bool CreatePost(Post newPost)
 {
     try
     {
         using (var context = new EFDbContext())
         {
             context.Posts.Add(newPost);
             context.SaveChanges();
             return true;
         }
     }
     catch (Exception)
     {
         return false;
     }
 }
        public bool DeleteCategory(int catId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    var delCat = context.Categories
                   .FirstOrDefault(p => p.Id == catId);

                    context.Categories.Remove(delCat);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public bool DeletePost(int postId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    var delPost = context.Posts
                   .FirstOrDefault(p => p.Id == postId);

                    context.Posts.Remove(delPost);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public bool UpdateCategory(Category cat)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    if (cat.Id == 0)
                    {
                        var newCat = cat;
                        context.Categories.Add(newCat);

                    }
                    else
                    {
                        var catFromDb = context.Categories
                            .Include(p => p.Posts)
                            .Single(p => p.Id == cat.Id);

                        context.Entry(catFromDb).CurrentValues.SetValues(cat);

                    }
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {

                    throw;
                    return false;
                }
            }
        }
 public int TotalPostsForSearch(string search)
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts.Count(p => p.Published && (p.Title.Contains(search) || p.Category.Name.Equals(search) || p.Tags.Any(t => t.UrlSlug.Equals(search))));
     }
 }
 public int TotalPostsForCategory(string category)
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts.Count(p => p.Published == true && p.Category.UrlSlug.Equals(category));
     }
 }
 public Category GetCategoryById(int catId)
 {
     using (var context = new EFDbContext())
     {
         try
         {
             return context.Categories
                 .Include(p => p.Posts)
                 .FirstOrDefault(p => p.Id == catId);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
 public int TotalPosts()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts.Count(p => p.Published == true);
     }
 }
 public int TotalPostForTag(string tag)
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts.Count(p => p.Published == true && p.Tags.Any(t => t.UrlSlug.Equals(tag)));
     }
 }
 public IList<Post> GetPostedPosts()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts
             .Where(p => p.Published)
             .OrderByDescending(p => p.PostedOn)
             .Include(p => p.Category)
             .Include(p => p.Tags)
             .ToList();
     }
 }
        public Post Post(int year, int month, string titleSlug)
        {
            using (EFDbContext context = new EFDbContext())
            {
                var post = context.Posts
                    .Include(i => i.Category)
                    .Include(i => i.Tags)
                    .Single(x => x.UrlSlug == titleSlug);

                return post;
            }
        }
        public bool UpdateTag(Tag tag)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    if (tag.Id == 0)
                    {
                        var newTag = tag;
                       context.Tags.Add(newTag);

                    }
                    else
                    {
                        var tagFromDb = context.Tags
                            .Include(p => p.Posts)
                            .Single(p => p.Id == tag.Id);

                        context.Entry(tagFromDb).CurrentValues.SetValues(tag);

                    }
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {

                    throw;
                    return false;
                }
            }
        }
 public Category GetCategoryByName(string catName)
 {
     using (var context = new EFDbContext())
     {
         try
         {
             return context.Categories
                 .Include(p => p.Posts)
                 .FirstOrDefault(p => p.Name == catName);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
 public IList<Post> LatestPosts()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts.OrderBy(p => p.Title).ToList();
     }
 }
 public Tag GetTagById(int tagId)
 {
     using (var context = new EFDbContext())
     {
         try
         {
             return context.Tags
                 .Include(p => p.Posts)
                 .FirstOrDefault(p => p.Id == tagId);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
        public bool UpdatePost(Post post, List<int> tagIds, string catId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    if (post.Id == 0)
                    {
                        var newPost = post;
                        var cats = Categories();
                        var tags = Tags();
                        List<Tag> tagsToAdd = (from tagid in tagIds select tags.Find(x => x.Id == tagid)).ToList();
                        context.Posts.Add(newPost);

                        context.Categories.Attach(cats.Find(m => m.Name.Equals(catId)));
                        newPost.Category = cats.Find(m => m.Name.Equals(catId));
                        newPost.Tags = new List<Tag>();
                        foreach (var tagid in tagIds)
                        {
                            Tag t = context.Tags.Find(tagid);
                            newPost.Tags.Add(t);
                        }

                    }
                    else
                    {
                        var postFromDb = context.Posts
                            .Include(p => p.Category)
                            .Include(p => p.Tags)
                            .Single(p => p.Id == post.Id);

                        context.Entry(postFromDb).CurrentValues.SetValues(post);

                        if (post.Category.Id != postFromDb.Category.Id)
                        {
                            context.Categories.Attach(post.Category);
                            postFromDb.Category = post.Category;
                        }

                        postFromDb.Tags.Clear();
                        foreach (var tagid in tagIds)
                        {
                            Tag t = context.Tags.Find(tagid);
                            postFromDb.Tags.Add(t);
                        }
                    }
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
 //Admin section
 public IList<Post> GetAllPosts()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts
             .OrderByDescending(p => p.Id)
             .Include(p => p.Category)
             .Include(p => p.Category.Posts)
             .Include(p => p.Tags)
             .ToList();
     }
 }
 public IList<Post> PostsForSearch(string search)
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts
                   .Where(p => p.Published && (p.Title.Contains(search) || p.Category.Name.Equals(search) || p.Tags.Any(t => t.UrlSlug.Equals(search))))
                   .OrderByDescending(p => p.PostedOn)
                   .Include(p => p.Category)
                   .Include(p => p.Tags)
                   .ToList();
     }
 }
 public Post GetPost(int postId)
 {
     using (var context = new EFDbContext())
     {
         //var context = new EFDbContext();
         try
         {
             return context.Posts
                 .Include(p => p.Tags)
                 .Include(p => p.Category)
                 .FirstOrDefault(p => p.Id == postId);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
 public IList<Post> PostsForTag(string tag)
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Posts
                   .Where(p => p.Published && p.Tags.Any(t => t.UrlSlug.Equals(tag)))
                   .OrderByDescending(p => p.PostedOn)
                   .Include(p => p.Category)
                   .Include(p => p.Tags)
                   .ToList();
     }
 }
 public List<Tag> Tags()
 {
     using (EFDbContext context = new EFDbContext())
     {
         return context.Tags
             .Include(x => x.Posts)
             .ToList();
     }
 }
        public bool DeleteTag(int tagId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    var delTag = context.Tags
                   .FirstOrDefault(p => p.Id == tagId);

                    context.Tags.Remove(delTag);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public bool ExcludePostFromCategory(Category chosenCat, int postId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    var postToDelete = context.Posts.First(x => x.Id == postId);

                    chosenCat.Posts
                        .Remove(postToDelete);

                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }