public ActionResult Details(int?id) { if (id == null) { return(new HttpNotFoundResult()); } else { var post = _db.Posts.FirstOrDefault(p => p.Id == id); var viewModel = new BlogPostDetailsViewModel() { Id = post.Id, Description = post.Description, ShortDescription = post.ShortDescription, Category = post.Category, Modified = post.Modified, PostedOn = post.PostedOn, Published = post.Published, Title = post.Title, Comments = post.Comments, //Could lead to possible bug if seed order doesn't stay the same. SelectedCategoryId = post.Category.Id }; return(View(viewModel)); } }
public ActionResult Details(string slug) { if (String.IsNullOrWhiteSpace(slug)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BlogPost blogPost = db.BlogPosts.FirstOrDefault(p => p.Slug == slug); if (blogPost == null) { return(HttpNotFound()); } //SqlParameter param1 = new SqlParameter("@blogpostid", blogPost.Id); //List<Comment> result = db.Database.SqlQuery<Comment>("exec SelectCommentsByBlogpost @blogpostid", param1).Where(x=>x.BlogPostId==blogPost.Id).Reverse().ToList(); var result = db.Comments.Where(x => x.BlogPostId == blogPost.Id).OrderBy(x => x.ParentID).ToList();//OrderByDescending(x => x.CreateDate) var root = new TreeNode <Comment>(new Comment { Id = 0 }); var currentNode = root; while (result.Any()) { for (int i = 0; i < result.Count; i++) { if (currentNode.Children.Any(x => x.Data.Id == result[i].ParentID)) { currentNode.FirstOrDefault(x => x.Data.Id == result[i].ParentID).AddChild(result[i]); //currentNode.FirstOrDefault(x => x.Data.Id == result[i].ParentID).Children = currentNode.FirstOrDefault(x => x.Data.Id == result[i].ParentID).OrderByDescending(x => x.Data.CreateDate).ToList(); result.Remove(result[i]); } else if (currentNode.Data.Id == result[i].ParentID) { currentNode.AddChild(result[i]); //currentNode.Children = currentNode.Children.OrderByDescending(x => x.Data.CreateDate).ToList(); result.Remove(result[i]); } } } var viewmodel = new BlogPostDetailsViewModel { BlogPost = blogPost, CommentTree = root, CommentCount = result.Count }; blogPost.ViewCount++; db.SaveChanges(); return(View(viewmodel)); }
// GET: BlogPosts/Details/5 public ActionResult Details(int?id) { Person thisPerson = GeneralLogic.getLoggedInUser(db); if (id == null) { return(RedirectToAction("Index", "Home")); } BlogPost blogPost = db.BlogPosts.Find(id); BlogPostDetailsViewModel details = new BlogPostDetailsViewModel(); details.author = blogPost.person; details.blogID = id.Value; details.comments = blogPost.comments; details.content = blogPost.content; details.date = blogPost.dateCreated; details.tags = blogPost.tags; details.title = blogPost.title; details.ratings = blogPost.ratings; details.reports = blogPost.report; if (details.reports == null) { details.reports = new Collection <Report>(); } if (details.ratings == null) { details.ratings = new Collection <Rating>(); } if (details.comments == null) { details.comments = new Collection <Comment>(); blogPost.comments = new Collection <Comment>(); } if (blogPost == null) { return(HttpNotFound()); } double avgrating = 0.0;; foreach (Rating r in blogPost.ratings) { avgrating += r.ratingNumber; } avgrating = avgrating / blogPost.ratings.Count; ViewBag.avgRating = avgrating; foreach (Report r in details.reports) { if (thisPerson != null && r.reporterID == thisPerson.Id) { ViewBag.isReported = true; } } //return View(blogPost); if (thisPerson != null) { ViewBag.userID = thisPerson.Id; } else { ViewBag.userID = null; } //ViewBag.userID = thisPerson.Id; return(View(details)); }
public ActionResult Details(BlogPostDetailsViewModel model) { Person currUser = GeneralLogic.getLoggedInUser(db); BlogPost blogPost = (from b in db.BlogPosts where (model.blogID == b.Id) select b).FirstOrDefault(); if (model.newRating != null) { using (db) { Rating r = new Rating(); r.blogPost = blogPost; r.BlogPostId = blogPost.Id; if (model.newRating > 5) { model.newRating = 5; } else if (model.newRating < 0) { model.newRating = 0; } r.ratingNumber = (int)model.newRating; r.username = User.Identity.Name; blogPost.ratings.Add(r); db.SaveChanges(); } } if (model.newreport != null) { using (db) { Report r = new Report(); r.reporterID = (int)model.newreport; blogPost.report.Add(r); db.SaveChanges(); } } if (model.newComment != null) { Comment c = new Comment(); c.theAuthorID = currUser.Id; c.Author = currUser.FirstName + " " + currUser.LastName; c.blogPost = blogPost; c.contents = model.newComment; c.dateCreated = DateTime.Now; if (blogPost.comments == null) { blogPost.comments = new Collection <Comment>(); blogPost.comments.Add(c); } else { blogPost.comments.Add(c); } db.SaveChanges(); } return(RedirectToAction("Details", "Blog", model.blogID)); }
public ActionResult CreateSearch([Bind(Include = "title,date,newRating")] BlogPostDetailsViewModel blogPost) { return(RedirectToAction("Search", "Blog", new { s = blogPost.title.ToString(), date = blogPost.date, rating = blogPost.newRating })); }