public async Task <IActionResult> AddComment(ReadMoreViewModel model) { if (string.IsNullOrEmpty(model.Comment.text)) { _notification.AddWarningToastMessage("لطفا مقادیر را پر کنید"); return(RedirectToAction("PostText", "ReadMore", model.Comment.PostId)); } if (model.Comment.ParentID == null) { var comment = new Comment() { PostId = model.Comment.PostId, UserId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value, text = model.Comment.text, CreatedTime = DateTime.Now }; await _db.CommentRepository.InsertAsync(comment); } else { var comment = new Comment() { PostId = model.Comment.PostId, UserId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value, text = model.Comment.text, CreatedTime = DateTime.Now, ParentID = model.Comment.ParentID }; await _db.CommentRepository.InsertAsync(comment); } await _db.SaveChangeAsync(); _notification.AddSuccessToastMessage("نظر شما ثبت شد"); return(RedirectToAction("ReadMore", "PostText", new { id = model.Comment.PostId })); }
public async Task <IActionResult> ReadMore(string id) { if (string.IsNullOrEmpty(id)) { _notification.AddWarningToastMessage("پست مورد نطر یافت نشد دوباره امتحان کنید"); return(RedirectToAction("Home", "Index")); } var Post = await _db.PostTextRepository.GetByIdAsync(id); var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; ViewBag.UserId = userId; var savepost = _db.SavePostRepository.Where(p => p.UserId == userId).ToList(); ViewBag.UserCount = savepost.Count(); var ip = HttpContext.Connection.RemoteIpAddress.ToString(); Post.Groups = await _db.GroupRepository.GetByIdAsync(Post.GroupId); Post.Users = await _db.UserRepository.GetByIdAsync(Post.UserId); var GetVisit = _db.PostTextVisitRepository.Where(p => p.PostId == id && p.Ip == ip).ToList(); if (GetVisit.Count == 0) { var visit = new PostTextVisit() { Ip = ip, PostId = id }; await _db.PostTextVisitRepository.InsertAsync(visit); Post.Visit += 1; _db.PostTextRepository.Update(Post); _db.SaveChange(); } var Comments = _db.CommentRepository.Where(p => p.PostId == Post.Id).OrderByDescending(p => p.CreatedTime).ToList(); if (Comments != null) { foreach (var item in Comments) { item.Users = await _db.UserRepository.GetByIdAsync(item.UserId); } } var viewModel = new ReadMoreViewModel() { PostText = Post, Comments = Comments }; return(View(viewModel)); }