public static bool Authorize(SlickBlogAppContext _db, Blog blog)
 {
     Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
     UserInfo userInfo = _db.UserInfo.Single(ui => ui.UserGuid == userGuid);
     if (blog.Owner.Equals(userInfo))
     {
         return true;
     }
     return false;
 }
        private bool IsBlogOwner(string username, string address)
        {
            // TODO: query the backend to perform the necessary verifications
            SlickBlogAppContext _db = new SlickBlogAppContext();
            try
            {
                Blog blog = _db.Blogs.Single(b => b.Address == address);
                if (blog.Owner.Username.Equals(username))
                {
                    return true;
                }
            }
            catch (InvalidOperationException)
            {
                return false;
            }

            return false;
        }
        public ActionResult SavePost(EditPost p)
        {
            Blog blog = _db.Blogs.Find(p.BlogId);

            if (ModelState.IsValid)
            {
                //anti xss
                //p.Content = Sanitizer.GetSafeHtmlFragment(p.Content);
                byte[] fileData = null;
                string contentType = null;
                string fileName = null;

                if (p.PostId != 0)
                {
                    Post pst = _db.Posts.Find(p.PostId);
                    fileData = pst.File;
                    contentType = pst.FileContentType;
                    fileName = pst.FileName;
                    pst = null;
                    _db = new SlickBlogAppContext();
                }
                Mapper.CreateMap<EditPost, Post>().ForMember(f => f.File, opt => opt.Ignore());
                Post post = Mapper.Map<EditPost, Post>(p);
                Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
                UserInfo userInfo = _db.UserInfo.Single(ui => ui.UserGuid == userGuid);
                if (p.file != null)
                {
                    if (p.file.ContentLength > 0)
                    {
                        Stream s = p.file.InputStream;
                        byte[] appData = new byte[p.file.ContentLength + 1];
                        s.Read(appData, 0, p.file.ContentLength);
                        post.File = appData;
                        post.FileName = p.file.FileName;
                        post.FileContentType = p.file.ContentType;
                    }
                }
                else if (p.PostId != 0 && p.FileName!=null)
                {
                    if (p.FileName.Equals(fileName))
                    {
                        post.File = fileData;
                        post.FileName = fileName;
                        post.FileContentType = contentType;
                    }
                }
                if (p.Tags != null)
                {
                    List<Tag> tagList = new List<Tag>();
                    post.Tags = tagList;
                    String[] tags = Regex.Split(p.Tags, ",");
                    foreach (var tag in tags)
                    {
                        Tag blogTag;
                        try
                        {
                            blogTag = _db.Tags.Single(t => t.TagName.Equals(tag));
                        }
                        catch (InvalidOperationException)
                        {
                            blogTag = new Tag();
                            blogTag.TagName = tag;
                            _db.Tags.Add(blogTag);

                        }
                        post.Tags.Add(blogTag);
                    }
                }
                post.Author = userInfo;
                post.Blog = _db.Blogs.Find(p.BlogId);
                post.Published = false;
                post.PostDate = DateTime.Now;
                if (p.PostId != 0)
                {
                    _db.Entry(post).State = EntityState.Modified;
                }
                else
                {
                    _db.Posts.Add(post);
                }
                _db.SaveChanges();
                //Mapper.CreateMap<Post, EditPost>();
                //EditPost ep = Mapper.Map<Post, EditPost>(post);
                //ep.PostId = post.PostId;
                //ep.BlogId = p.BlogId;
                //ep.Address = p.Address;
                return Content(post.PostId.ToString());
            }
            return Content("0");
        }
 public static MvcHtmlString TotalCommentCount(this HtmlHelper helper, int blogid)
 {
     SlickBlogAppContext _db = new SlickBlogAppContext();
     //Blog blog = _db.Blogs.Find(blogid);
     _db.Comments.Where(c=>c.Post.Blog.BlogId==blogid).Count();
     int count = 0;
     //if (blog.Posts != null)
     //{
     count = _db.Comments.Where(c => c.Post.Blog.BlogId == blogid).Count();
     //}
     return MvcHtmlString.Create(count.ToString());
 }
 public static MvcHtmlString PostCount(this HtmlHelper helper, int blogid)
 {
     SlickBlogAppContext _db = new SlickBlogAppContext();
     Blog blog = _db.Blogs.Find(blogid);
     int count = 0;
     if (blog.Posts != null)
     {
         count = blog.Posts.Count();
     }
     return MvcHtmlString.Create(count.ToString());
 }
 public static IEnumerable<Comment> getPagedComments(Blog blog, int skip, int take)
 {
     if (skip != 0)
     {
         skip--;
     }
     SlickBlogAppContext _db = new SlickBlogAppContext();
     IEnumerable<Comment> trl =  _db.Comments.Include("Author").Include("Post").Where(c => c.Post.Blog.Address.Equals(blog.Address)).OrderByDescending(c => c.PostDate).Skip(skip * take).Take(take);
     return trl;
 }