private async Task <FullPostViewModel> GetFullPostVMFromPost(Post post) { FullPostViewModel viewModel = new FullPostViewModel(); viewModel.Post = post; List <Comment> comments = await _context.Comments .Where(m => m.ParentPost == post).ToListAsync(); viewModel.Comments = comments; viewModel.OwnerUserName = post.OwnerUsername; return(viewModel); }
public FullPostViewModel Create(FullPostViewModel page) { var result = new FullPostViewModel(); var newEntity = GenericMapper.MapObject <Post>(page); var createdEntity = _postsService.Create(newEntity, HttpContext.User.Identity.Name); if (createdEntity != null) { result = GenericMapper.MapObject <FullPostViewModel>(createdEntity); } return(result); }
public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } Post post = await _context.Posts .FirstOrDefaultAsync(m => m.PostId == id); if (post == null) { return(NotFound()); } FullPostViewModel viewModel = await GetFullPostVMFromPost(post); return(View(viewModel)); }
public async Task <IActionResult> FullPost(int?id) { if (id != null) { var post = await _db.BlogPosts.AsNoTracking(). FirstOrDefaultAsync(x => x.Id == id); if (post == null) { return(NotFound()); } FullPostViewModel fpvw = new FullPostViewModel { Posts = post }; return(PartialView(fpvw)); } return(NotFound()); }
public ActionResult LikePost(int idPost) { if (!User.Identity.IsAuthenticated) { return(PartialView("LikeError")); } var user = _userRepos.Get.FirstOrDefault(p => p.Login == User.Identity.Name); if (user != null) { _likeRep.AddOrRemove(Convert.ToInt32(idPost), user.Id); } var model = new FullPostViewModel() { CountLike = _likeRep.Get.Count(p => p.IdPost == idPost) }; return(Request.IsAjaxRequest() ? PartialView("LikePartial", model) : PartialView("LikeError")); }
public ActionResult AllPost(int?idPost) { const char separator = '#'; var model = new FullPostViewModel(); var post = _repo.Get.FirstOrDefault(p => p.Id == idPost); if (post == null) { return(RedirectToAction("Error404")); } model.Id = Convert.ToInt32(idPost); model.Title = post.Title; model.User = _userRepos.Get.First(u => u.Id == post.IdUser); model.Content = post.Content; model.CreateTime = post.CreateTime; model.UpdateTime = post.UpdateTime; model.Tags = post.Tags.Trim().Split(separator); model.Comments = _repository.Get.Where(p => p.IdPost == idPost); model.CountLike = _likeRep.Get.Count(p => p.IdPost == idPost); return(View(model)); }
public async Task <IActionResult> Details([Bind("PostID,Comment")] FullPostViewModel vm) { FullPostViewModel viewModel = null; if (ModelState.IsValid) { Comment newComment = new Comment(); var Owner = await _userManager.FindByNameAsync(LoggedInUsername); newComment.CommentText = vm.Comment; newComment.DatePosted = DateTime.Now; newComment.OwnerId = Owner.Id; newComment.OwnerUserName = Owner.UserName; Post post = await _context.Posts .FirstOrDefaultAsync(m => m.PostId == vm.PostID); if (post == null) { return(NotFound()); } newComment.ParentPost = post; post.NoOfComments++; _context.Add(newComment); await _context.SaveChangesAsync(); viewModel = await GetFullPostVMFromPost(post); } else { return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }
public void Update(int id, FullPostViewModel page) { var updatedEntity = GenericMapper.MapObject <Post>(page); _postsService.Update(id, updatedEntity); }