public ActionResult DetailBySlug(string slug) { if (string.IsNullOrWhiteSpace(slug)) { return(RedirectToAction(nameof(PostController.Index))); } var appUserId = User.Identity.GetUserId(); var isAdmin = User.IsInRole("Admin"); if (isAdmin) { postForDetail = DbContext.Posts.FirstOrDefault(p => p.Slug == slug); } else { postForDetail = DbContext.Posts.FirstOrDefault(p => p.Published && p.Slug == slug); } if (postForDetail == null) { return(RedirectToAction(nameof(PostController.Index))); } var model = new DetailPostViewModel(); model.Title = postForDetail.Title; model.Body = postForDetail.Body; model.MediaUrl = postForDetail.MediaUrl; model.Comments = postForDetail.Comments; return(View("Detail", model)); }
public ActionResult Detail(int?id) { if (!id.HasValue) { return(RedirectToAction(nameof(PostController.Index))); } var appUserId = User.Identity.GetUserId(); var isAdmin = User.IsInRole("Admin"); if (isAdmin) { postForDetail = DbContext.Posts.FirstOrDefault(p => p.Id == id.Value); } else { postForDetail = DbContext.Posts.FirstOrDefault(p => p.Published && p.Id == id.Value); } if (postForDetail == null) { return(RedirectToAction(nameof(PostController.Index))); } var model = new DetailPostViewModel(); model.Title = postForDetail.Title; model.Body = postForDetail.Body; model.MediaUrl = postForDetail.MediaUrl; return(View(model)); }
public IActionResult Details(int id) { var model = new DetailPostViewModel { Post = _postData.Get(id) }; if (model == null) { return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> Detail(int?id) { if (id == null) { return(RedirectToAction("Post")); } var post = await _postService.GetPost((int)id); var model = new DetailPostViewModel { Id = post.Id, Title = post.Title, BodyText = post.BodyText, Created = post.Created, UserName = post.User.UserName, }; return(View(model)); }
//[Route("Blog/{SlugName}")] //Display post Detail and all Comments public ActionResult DetailBySlugName(string slugName, int?CommentUpdateId) { if (string.IsNullOrWhiteSpace(slugName)) { return(RedirectToAction(nameof(HomeController.Index))); } var post = DbContext.Posts.FirstOrDefault(p => p.Slug == slugName); if (post == null) { return(RedirectToAction(nameof(HomeController.Index))); } var model = new DetailPostViewModel(); model.Id = post.Id; model.Title = post.Title; model.Body = post.Body; model.Published = post.Published; model.DateCreate = post.DateCreate; model.DateUpdate = post.DateUpdate; model.MediaUrl = post.MediaUrl; model.Slug = slugName; model.CommentUpdateId = CommentUpdateId; //Display user's commnet in the textBox when admin need to be updated if (CommentUpdateId != null) { var updatedComment = DbContext.Comments.FirstOrDefault(p => p.Id == CommentUpdateId); model.CommentUpdateId = CommentUpdateId; model.CommentMessage = updatedComment.CommentMessage; model.CommentUpdatedReason = updatedComment.CommentUpdatedReason; } // Get comment on this post var comments = DbContext.Comments.Where(p => p.Post.Id == post.Id).ToList(); model.Comments = comments; return(View("Detail", model)); }
public IActionResult Details(int id, DetailPostViewModel model) { if (!ModelState.IsValid) { RedirectToAction("Details", "Home", id); } try { var comment = new Comment { DescriptionOfComment = model.CommentText, NicknameOfCommenter = model.CommentNick }; comment = _commentData.Add(comment, id); return(RedirectToAction("Details", "Home", id)); } catch (Exception) { return(RedirectToAction("Index", "Home")); } }
// GET: Post/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var post = await _context.Posts //.Include(p => p.User) //.Include(p => p.Children) // .ThenInclude(p => p.) .SingleOrDefaultAsync(m => m.Id == id); if (post == null) { return(NotFound()); } await _context.Entry(post) .Collection(p => p.Children) .LoadAsync(); await _context.Entry(post) .Reference(p => p.User) .LoadAsync(); foreach (var item in post.Children) { await _context.Entry(item) .Reference(p => p.User) .LoadAsync(); } var postView = new DetailPostViewModel(post); return(View(postView)); }
public DetailPostViewModel GetDetail(int id) { // lay chi tiet post theo id var post = GetById(id); // lay user theo post var user = _userRepository.FindSingle(s => s.Id == post.UserId); // lay danh sach tag theo post id var tags = from t in _tagRepository.FindAll() join pt in _postTagRepository.FindAll(s => s.PostId == id) on t.Id equals pt.TagId //where pt.PostId == id select new TagViewModel { Id = t.Id, TagName = t.TagName }; // lay danh sach category theo postid var categories = from pc in _postCategoryRepository.FindAll(s => s.PostId == id) join c in _categoryRepository.FindAll() on pc.CategoryId equals c.Id //where pc.PostId == id select new CategoryViewModel { Id = c.Id, CategoryName = c.CategoryName }; // lay danh sach comment var comments = from cm in _commentRepository.FindAll() join p in _postRepository.FindAll() on cm.PostId equals p.Id join us in _userRepository.FindAll() on p.UserId equals us.Id where cm.PostId == id && cm.Status == true select new CommentInPostViewModel { UserName = us.UserName, CreateDate = cm.CreateDate, Content = cm.Content }; //get previous post var previousPost = _postRepository.FindAll(s => s.Id < id).ProjectTo <PostViewModel>().LastOrDefault(); //get next post var nextPost = _postRepository.FindAll(s => s.Id > id).ProjectTo <PostViewModel>().FirstOrDefault(); var postViewModel = new DetailPostViewModel { PostId = post.Id, Title = post.Title, Content = post.Content, Tags = tags, Categories = categories, Avatar = user.Avatar, Author = user.FirstName, Comments = comments.OrderByDescending(s => s.CreateDate), DateAgo = DateFormat.GetPrettyDateAgo(post.CreateDate), PreviousPost = previousPost, NextPost = nextPost }; return(postViewModel); }
public PartialDetailPostViewModel(int index, DetailPostViewModel item) { this.Index = index; this.Item = item; }
public ActionResult DetailBySlugName(string SlugName, CreateUpadteCommentViewModel formData) { var post = DbContext.Posts.FirstOrDefault(p => p.Slug == SlugName); var userId = User.Identity.GetUserId(); var userName = User.Identity.GetUserName(); //User Create new comment if (formData.CommentMessage != null && formData.CommentUpdateId == null) { var comment = new Comment(); comment.CommentMessage = formData.CommentMessage; comment.DateCreate = DateTime.Now; comment.UserId = userId; post.Comments.Add(comment); DbContext.SaveChanges(); ModelState.Clear(); } //Update comment when message and Reason are in the formData else if (formData.CommentMessage != null && formData.CommentUpdatedReason != null && formData.CommentUpdateId != null) { var updateComment = DbContext.Comments.FirstOrDefault(p => p.Id == formData.CommentUpdateId); updateComment.CommentMessage = formData.CommentMessage; updateComment.CommentUpdatedReason = formData.CommentUpdatedReason; updateComment.DateUpdate = DateTime.Now; DbContext.SaveChanges(); ModelState.Clear(); formData.CommentUpdateId = null; } //Delete comment if (formData.CommentDeleteId != null && (User.IsInRole("Admin") || User.IsInRole("Moderator"))) { var comment = DbContext.Comments.FirstOrDefault(p => p.Id == formData.CommentDeleteId); if (comment != null) { DbContext.Comments.Remove(comment); DbContext.SaveChanges(); } } //Create new model for detail view with comment var model = new DetailPostViewModel(); model.Id = post.Id; model.Title = post.Title; model.Body = post.Body; model.Published = post.Published; model.DateCreate = post.DateCreate; model.DateUpdate = post.DateUpdate; model.MediaUrl = post.MediaUrl; model.Slug = SlugName; //var comments = DbContext.Comments.Where(p => p.Post.Id == post.Id).Include(p => p.User).ToList(); var comments = DbContext.Comments.Where(p => p.Post.Id == post.Id).ToList(); model.Comments = comments; return(RedirectToAction("DetailBySlugName", "Home", new { SlugName, formData.CommentUpdateId })); }