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 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 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 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 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;
                }
            }
        }
        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 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;
                }
            }
        }