public ActionResult NoteReview(FormCollection form) { var user = db.Users.Where(x => x.Email == User.Identity.Name).FirstOrDefault(); SellerNotesReviews reviews = new SellerNotesReviews(); int NoteID = Convert.ToInt32(form["noteid"]); if (!db.SellerNotesReviews.Any(x => x.NoteID == NoteID && x.ReviewedByID == user.ID)) { reviews.NoteID = Convert.ToInt32(form["noteid"]); reviews.AgainstDownloadsID = Convert.ToInt32(form["downloadid"]); reviews.ReviewedByID = user.ID; reviews.Ratings = Convert.ToDecimal(form["rate"]); reviews.Comments = form["review"]; reviews.CreatedDate = DateTime.Now; reviews.CreatedBy = user.ID; reviews.IsActive = true; db.SellerNotesReviews.Add(reviews); db.SaveChanges(); } else { var review = db.SellerNotesReviews.FirstOrDefault(x => x.NoteID == NoteID && x.ReviewedByID == user.ID); review.AgainstDownloadsID = Convert.ToInt32(form["downloadid"]); review.Ratings = Convert.ToDecimal(form["rate"]); review.Comments = form["review"]; review.ModifiedDate = DateTime.Now; review.ModifiedBy = user.ID; db.SaveChanges(); } return(RedirectToAction("MyDownloads")); }
public ActionResult AddReview(int nid, int rate, string Comments) { var emailid = User.Identity.Name.ToString(); Users obj = dbobj.Users.Where(x => x.EmailID == emailid).FirstOrDefault(); var oldreview = dbobj.SellerNotesReviews.Where(x => x.NoteID == nid && x.ReviewedByID == obj.ID).FirstOrDefault(); if (oldreview == null) //New Review { SellerNotesReviews review = new SellerNotesReviews(); review.NoteID = nid; review.ReviewedByID = obj.ID; review.Ratings = rate; review.Comments = Comments; review.CreatedDate = DateTime.Now; dbobj.SellerNotesReviews.Add(review); dbobj.SaveChanges(); // Adding Ratings in note table var book = dbobj.SellerNotes.Where(x => x.ID == nid).FirstOrDefault(); int total_reviews = dbobj.SellerNotesReviews.Where(x => x.NoteID == nid).Count(); decimal total_stars = dbobj.SellerNotesReviews.Where(x => x.NoteID == nid).Select(x => x.Ratings).Sum(); book.TotalReviews = total_reviews; book.Rating = ((double)total_stars / total_reviews) * 20; dbobj.Entry(book).State = System.Data.Entity.EntityState.Modified; dbobj.SaveChanges(); // ------------------------------------------------------------ return(RedirectToAction("MyDownloads")); } else //Update Review { oldreview.Ratings = rate; oldreview.Comments = Comments; dbobj.Entry(oldreview).State = System.Data.Entity.EntityState.Modified; dbobj.SaveChanges(); // Adding Ratings in note table var book = dbobj.SellerNotes.Where(x => x.ID == nid).FirstOrDefault(); int total_reviews = dbobj.SellerNotesReviews.Where(x => x.NoteID == nid).Count(); decimal total_stars = dbobj.SellerNotesReviews.Where(x => x.NoteID == nid).Select(x => x.Ratings).Sum(); book.TotalReviews = total_reviews; book.Rating = ((double)total_stars / total_reviews) * 20; dbobj.Entry(book).State = System.Data.Entity.EntityState.Modified; dbobj.SaveChanges(); // ------------------------------------------------------------ return(RedirectToAction("MyDownloads")); } }
public ActionResult AddReview(SellerNotesReviews notereview) { // check if comment is null or not if (String.IsNullOrEmpty(notereview.Comments)) { return(RedirectToAction("MyDownloads")); } // check if rating is between 1 to 5 if (notereview.Ratings < 1 || notereview.Ratings > 5) { return(RedirectToAction("MyDownloads")); } // get Download object for check if user is downloaded note or not var notedownloaded = context.Downloads.Where(x => x.ID == notereview.AgainstDownloadsID && x.IsAttachmentDownloaded == true).FirstOrDefault(); // user can provide review after downloading the note if (notedownloaded != null) { //get logged in user var user = context.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // check if user already provided review or not var alreadyprovidereview = context.SellerNotesReviews.Where(x => x.AgainstDownloadsID == notereview.AgainstDownloadsID && x.IsActive == true).FirstOrDefault(); // if user not provide review then add review if (alreadyprovidereview == null) { // create a sellernotesreview object and initialize it SellerNotesReviews review = new SellerNotesReviews(); review.NoteID = notereview.NoteID; review.AgainstDownloadsID = notereview.AgainstDownloadsID; review.ReviewedByID = user.ID; review.Ratings = notereview.Ratings; review.Comments = notereview.Comments; review.CreatedDate = DateTime.Now; review.CreatedBy = user.ID; review.IsActive = true; // save sellernotesreview into database context.SellerNotesReviews.Add(review); context.SaveChanges(); return(RedirectToAction("MyDownloads")); } // if user is already provided review then edit it else { alreadyprovidereview.Ratings = notereview.Ratings; alreadyprovidereview.Comments = notereview.Comments; alreadyprovidereview.ModifiedDate = DateTime.Now; alreadyprovidereview.ModifiedBy = user.ID; // update review and save in database context.Entry(alreadyprovidereview).State = EntityState.Modified; context.SaveChanges(); return(RedirectToAction("MyDownloads")); } } return(RedirectToAction("MyDownloads")); }