Beispiel #1
0
 public void Update(Post post)
 {
     post.UpdatedDate           = DateTime.Now;
     _context.Entry(post).State = EntityState.Modified;
 }
        public ActionResult Post(int id, string slug, string category, string tag, int?cur, string archive, string searchTerm)
        {
            var viewModel = new BlogViewModel();

            if (!string.IsNullOrWhiteSpace(category))
            {
                viewModel.Category = category;
            }

            if (!string.IsNullOrWhiteSpace(archive))
            {
                viewModel.Archive = archive;
            }

            if (!string.IsNullOrWhiteSpace(slug))
            {
                viewModel.UrlSlug = slug;
            }

            if (!string.IsNullOrWhiteSpace(tag))
            {
                viewModel.Tag = tag;
            }

            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                viewModel.SearchTerm = searchTerm;
            }

            if (cur.HasValue)
            {
                viewModel.CurrPage = cur;
            }

            var post = _db.Posts.Find(id);

            if (post == null)
            {
                throw new HttpException(404, "The blog post you are looking for is not available.");
            }

            _db.Entry(post).Reference("Category").Load();
            _db.Entry(post).Collection("Tags").Load();
            _db.Entry(post).Collection("Comments").Load();

            var prev = _db.Posts.Where(p => p.PostedOn < post.PostedOn).OrderByDescending(o => o.PostedOn).FirstOrDefault();
            var next = _db.Posts.Where(p => p.PostedOn > post.PostedOn).OrderBy(o => o.PostedOn).FirstOrDefault();

            if (prev != null)
            {
                viewModel.PrevId   = prev.Id;
                viewModel.PrevSlug = prev.UrlSlug;
            }

            if (next != null)
            {
                viewModel.NextId   = next.Id;
                viewModel.NextSlug = next.UrlSlug;
            }

            viewModel.SelectedPost = post;

            if (post.Comments != null && post.Comments.Count > ConfigManager.Get <int>(Constants.ConfigKey.Blog_Posts_MaxComments))
            {
                viewModel.DisableComments = true;
            }

            return(View(viewModel));
        }