public IActionResult Details(int id) { var bd = blogUtil_.GetDetailDisplay(id); if (bd == null) { return(NotFound()); } var author = udb_.Users.SingleOrDefault(u => u.UserName == bd.blog.Author); User authorInfo = new User { UserName = bd.blog.Author, NickName = bd.blog.Author }; if (author != null) { authorInfo = Models.App.User.FromUserProfile(author, Url.Action("Show", "Avatar", new { name = authorInfo.UserName }, Request.Scheme)); } var comments = db_.Posts.Where(p => p.IdType == ItemType.Blog && p.ItemId == id) .GroupJoin(db_.BlogRatings, p => p.PostId, r => r.PostId, (p, r) => new { post = p, ratings = r }) .SelectMany(a => a.ratings.DefaultIfEmpty(), (p, r) => new { p.post, blograting = r, uc = p.post.Ratings.Count(ra => ra.Value == 1), dc = p.post.Ratings.Count(ra => ra.Value == -1), uv = User.Identity.IsAuthenticated ? p.post.Ratings.FirstOrDefault(ra => ra.Rater == User.Identity.Name) : null }) .OrderByDescending(p => p.post.Rating) .ThenByDescending(p => p.post.PostDate) .Take(5) .Select(c => new Comment { Author = c.post.Author, CommentId = c.post.PostId, Content = c.post.Content, ItemId = c.post.ItemId, UpvoteCount = c.uc, DownvoteCount = c.dc, IsUpvoted = c.uv == null ? new bool?() : c.uv.Value == 1, IsDownvoted = c.uv == null ? new bool?() : c.uv.Value == -1, CreateDate = c.post.PostDate, Type = Comment.CommentType.Blog, Rating = c.blograting == null ? new int?() : c.blograting.value }) .ToArray(); var rating = ratingUtil_.GetUsersRating(id); return(Json(GetBlogDetails(bd, authorInfo, comments, rating.Rating == null ? new int?() : rating.Rating.value))); }
public ActionResult AddReplyWithRate(int itemid, string addreplycontent, string Captcha, string Prefix, int?rating) { Post post; try { CheckPost(Captcha, Prefix, addreplycontent); UsersRating userrate = null; if (rating != null && RatingUtil.RatingValue.ContainsKey(rating.Value)) { userrate = _ratingUtil.GetUsersRating(itemid); if (userrate.Rating != null && (userrate.HasPost || userrate.Rating.value != RatingUtil.RatingValue[rating.Value])) { throw new BlogException(userrate.RateWithAccount ? "您已经评过分了" : "今天已经评过分了"); } } post = _blogUtil.AddBlogPost(itemid, User.Identity.Name, _sanitizerService.Sanitize(addreplycontent)); HttpContext.Session.SetDateTime("LastPostTime", DateTime.Now); if (userrate != null) { if (userrate.Rating == null) { var Rate = _ratingUtil.AddBlogRating(itemid, rating.Value, userrate.credential, post.PostId); if (Rate != null) { _ratingUtil.getRating(itemid, false); } } else { userrate.Rating.PostId = post.PostId; _db.SaveChanges(); } } TriggerAddPost(post); } catch (BlogException e) { return(Json(new { err = e.Message })); } string expmsg = HttpContext.Items["QuestMsg"] as string; return(Json(new { id = post.PostId, expmsg })); }
public ActionResult GetUsersRate(int blogid) { var rating = _ratingUtil.GetUsersRating(blogid); return(Json(new { rating = rating.Rating == null ? null : new { value = rating.Rating.value, HasPost = rating.HasPost } })); }
public async Task <ActionResult> Comment([FromBody] AddCommentRequest request) { if (string.IsNullOrWhiteSpace(BlogHelper.removeAllTags(request.Content))) { return(BadRequest(new { error = "内容不能为空或纯图片。" })); } Post post; switch (request.Type) { case Models.App.Comment.CommentType.Blog: var blog = await _db.Blogs.Include("option").SingleOrDefaultAsync(b => b.BlogID == request.ItemId); if (blog == null) { return(NotFound()); } if (blog.option != null && blog.option.NoComment) { return(Forbid()); } UsersRating userrate = null; if (request.Rating != null && RatingUtil.RatingValue.ContainsKey(request.Rating.Value)) { if (blog.option.NoRate) { return(Forbid()); } userrate = _ratingUtil.GetUsersRating(request.ItemId); if (userrate.Rating != null && (userrate.HasPost || userrate.Rating.value != RatingUtil.RatingValue[request.Rating.Value])) { return(BadRequest(new { error = userrate.RateWithAccount ? "您已经评过分了" : "今天已经评过分了" })); } } post = _blogUtil.AddBlogPost(request.ItemId, User.Identity.Name, _sanitizerService.Sanitize(request.Content)); if (userrate != null) { if (userrate.Rating == null) { var Rate = _ratingUtil.AddBlogRating(request.ItemId, request.Rating.Value, userrate.credential, post.PostId); if (Rate != null) { _ratingUtil.getRating(request.ItemId, false); } } else { userrate.Rating.PostId = post.PostId; await _db.SaveChangesAsync(); } } break; default: return(BadRequest()); } TriggerAddPost(post); string expmsg = HttpContext.Items["QuestMsg"] as string; return(Json(new AddReplyResponse { CommentId = post.PostId, Message = expmsg })); }