public ActionResult CreateRating([Bind(Include = "Rating,UserName,PostId")] UserToPostRating uTPR) { db.UserToPostRatings.Add(uTPR); db.SaveChanges(); Post post = db.Posts.Where(d => d.PostID == uTPR.PostId).ToList().ElementAt(0); post.NumOfRating += 1; db.SaveChanges(); int sum = 0; foreach (var item in db.UserToPostRatings.Where(d => d.PostId == uTPR.PostId).ToList()) { sum += item.Rating; } post.Rating = sum / post.NumOfRating; db.SaveChanges(); string postCreator = db.ProfessionalPages.Where(d => d.ProfessionalPageID == post.ProfessionalPageID).ToList().ElementAt(0).UserName; Professional pro = db.Professionals.Where(d => d.UserName == postCreator).ToList().ElementAt(0); var listOfPP = db.ProfessionalPages.Where(d => d.UserName == pro.UserName).ToList(); double score = 0; int sumOfPosts = 0; foreach (var pp in listOfPP) { foreach (var p in pp.Posts.ToList()) { sumOfPosts++; score += p.Rating; } } pro.Score = score / sumOfPosts; db.SaveChanges(); return(Redirect("../Posts/Details/" + uTPR.PostId)); }
public ActionResult DeleteConfirmed(int id) { UserToPostRating userToPostRating = db.UserToPostRatings.Find(id); db.UserToPostRatings.Remove(userToPostRating); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "PostId,UserName,Rating")] UserToPostRating userToPostRating) { if (ModelState.IsValid) { db.Entry(userToPostRating).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.PostId = new SelectList(db.Posts, "PostID", "Title", userToPostRating.PostId); ViewBag.UserName = new SelectList(db.Users, "UserName", "FirstName", userToPostRating.UserName); return(View(userToPostRating)); }
// GET: UserToPostRatings/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } UserToPostRating userToPostRating = db.UserToPostRatings.Find(id); if (userToPostRating == null) { return(HttpNotFound()); } return(View(userToPostRating)); }
// GET: UserToPostRatings/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } UserToPostRating userToPostRating = db.UserToPostRatings.Find(id); if (userToPostRating == null) { return(HttpNotFound()); } ViewBag.PostId = new SelectList(db.Posts, "PostID", "Title", userToPostRating.PostId); ViewBag.UserName = new SelectList(db.Users, "UserName", "FirstName", userToPostRating.UserName); return(View(userToPostRating)); }
public ActionResult AddCommentOrRate([Bind(Include = "PostID,PostCommentContent,UserName")] PostComment pC, [Bind(Include = "Rating,UserName,PostId")] UserToPostRating uTPR) { MyDB db = new MyDB(); if (pC.PostCommentContent != null) { return(CreateComment(pC)); } else if ((uTPR.Rating.ToString() != null) && db.UserToPostRatings.Where(d => d.UserName == pC.UserName && d.PostId == pC.PostID).Count() == 0) { return(CreateRating(uTPR)); } else { return(Redirect("../Posts/Details/" + pC.PostID)); } }