public async Task <IActionResult> HotestComments() { // 子查询可以有,但是只能查询一个scalar。outer join应当用正规的outer join var query = from p in Context.Comments select new { comment = p, agreeCount = Context.Attitudes.Where(o => o.Agree == true && o.CommentId == p.Id).Count(), } into k join u in Context.Users on k.comment.SenderId equals u.Id join w in Context.Works on k.comment.WorkId equals w.Id orderby k.comment.AgreeCount descending select new { comment = k.comment, agreeCount = k.agreeCount, work = w, user = u }; var comments = await query.Take(20).ToListAsync(); var qcomments = comments.Select( p => QComment.NormalView(p.comment, QUser.NormalView(p.user), null, QWork.NormalView(p.work))) .ToList(); return(Ok(qcomments)); }
public async Task <QComment> GetById(string id) { var commentId = XUtils.ParseId(id); if (commentId == null) { return(null); } var query = from p in Context.Comments.Where(r => r.Id == commentId) join q in Context.Users on p.SenderId equals q.Id select new { comment = p, user = q, myAtt = Context.Attitudes.Where(o => o.CommentId == p.Id && o.SenderId == AuthStore.UserId).FirstOrDefault() }; var data = await query.FirstOrDefaultAsync(); var user = QUser.NormalView(data.user); var commentQ = QComment.NormalView(data.comment, user, data.myAtt?.Agree); return(commentQ); }
public ActionResult DeleteConfirmed(int id) { QComment qComment = db.QComments.Find(id); db.QComments.Remove(qComment); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "Id,Description")] QComment qComment, int id) { qComment.UserId = this.User.Identity.GetUserId(); qComment.QuestionId = id; qComment.Comdate = DateTime.Now; db.QComments.Add(qComment); db.SaveChanges(); return(RedirectToAction("Details/", "Questions", new { id = id.ToString(), viewtype = "extended" })); }
public ActionResult Edit([Bind(Include = "Id,Description,Comdate,QuestionId,UserId")] QComment qComment) { if (ModelState.IsValid) { db.Entry(qComment).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.QuestionId = new SelectList(db.Questions, "Id", "Tilte", qComment.QuestionId); ViewBag.UserId = new SelectList(db.ApplicationUsers, "Id", "Email", qComment.UserId); return(View(qComment)); }
public async Task <IActionResult> GetByWork(string workId, OrderByType order, int page) { if (!Enum.IsDefined(typeof(OrderByType), order)) { return(new ApiError(MyErrorCode.ModelInvalid, "Invalid 'order'").Wrap()); } page = Math.Max(page, 0); const int pageSize = 20; var wid = XUtils.ParseId(workId); if (wid == null) { return(new ApiError(MyErrorCode.ModelInvalid, "wordId parse error").Wrap()); } var query = from p in Context.Comments where p.WorkId == wid join q in Context.Users on p.SenderId equals q.Id join o in Context.Attitudes.Where(z => z.SenderId == AuthStore.UserId) on p.Id equals o.CommentId into xx from x in xx.DefaultIfEmpty() select new { comment = p, user = q, myatt = x }; if (order == OrderByType.Hottest) { query = query.OrderByDescending(p => p.comment.AgreeCount); } else { query = query.OrderByDescending(p => p.comment.CreatedAt); } var ll = await query.Take(10).ToListAsync(); query = query.Skip(page * pageSize).Take(pageSize); var data = await query.ToListAsync(); var commentsQ = data.Select(p => QComment.NormalView(p.comment, QUser.NormalView(p.user), p.myatt?.Agree)).ToList(); return(Ok(commentsQ)); }
// GET: QComments/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } QComment qComment = db.QComments.Find(id); if (qComment == null) { return(HttpNotFound()); } return(View(qComment)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } QComment qComment = db.QComments.Find(id); if (qComment == null) { return(HttpNotFound()); } ViewBag.QuestionId = new SelectList(db.Questions, "Id", "Tilte", qComment.QuestionId); ViewBag.UserId = new SelectList(db.ApplicationUsers, "Id", "Email", qComment.UserId); return(View(qComment)); }