/* Submits Note Rating
  * Returns false if fails or not allowed to rate
  * Return True if successfully submitted or updated
  * */
 public static bool SubmitNoteRating(RatingModel Rm, int UserID)
 {
     using (var context = new NotesMarketPlaceEntities())
     {
         var IsAllowedToRate = context.Downloads.FirstOrDefault(dwn => dwn.NoteID == Rm.NoteID && dwn.BuyerID == UserID && dwn.IsAllowed && dwn.IsDownloaded);
         if (IsAllowedToRate == null)
         {
             return(false); //Not allowed
         }
         else
         {
             NotesReview NR = context.NotesReviews.FirstOrDefault(nr => nr.NoteID == Rm.NoteID && nr.BuyerID == UserID);
             if (NR != null)
             {
                 NR.Comment      = Rm.Comment;
                 NR.Rating       = Rm.Stars;
                 NR.ModifiedBy   = UserID;
                 NR.ModifiedDate = System.DateTime.Now;
             }
             else
             {
                 context.NotesReviews.Add(new NotesReview()
                 {
                     NoteID       = Rm.NoteID,
                     BuyerID      = UserID,
                     Rating       = Rm.Stars,
                     Comment      = Rm.Comment,
                     CreatedBy    = UserID,
                     CreatedDate  = System.DateTime.Now,
                     ModifiedBy   = UserID,
                     ModifiedDate = System.DateTime.Now,
                     DownloadID   = IsAllowedToRate.DownloadID
                 });
             }
             try
             {
                 context.SaveChanges();
             }
             catch (Exception e)
             {
                 Debug.WriteLine(e);
                 return(false);
             }
             return(true);
         }
     }
 }
        public ActionResult AddReview(NotesReview 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 = dobj.Downloads.Where(x => x.ID == notereview.AgainstDownloadsID && x.IsAttachmentDownloaded == true).FirstOrDefault();

            // user can provide notereview after downloading the note
            if (notedownloaded != null)
            {
                var user = dobj.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault();

                var alreadyprovidereview = dobj.NotesReview.Where(x => x.AgainstDownloadsID == notereview.AgainstDownloadsID && x.IsActive == true).FirstOrDefault();

                // if user not provide notereview then add notereview
                if (alreadyprovidereview == null)
                {
                    NotesReview review = new NotesReview();

                    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;

                    dobj.NotesReview.Add(review);
                    dobj.SaveChanges();

                    return(RedirectToAction("MyDownloads"));
                }

                // edit previous notereview
                else
                {
                    alreadyprovidereview.Ratings      = notereview.Ratings;
                    alreadyprovidereview.Comments     = notereview.Comments;
                    alreadyprovidereview.ModifiedDate = DateTime.Now;
                    alreadyprovidereview.ModifiedBy   = user.ID;

                    // update and save notereview
                    dobj.Entry(alreadyprovidereview).State = EntityState.Modified;
                    dobj.SaveChanges();

                    return(RedirectToAction("MyDownloads"));
                }
            }
            return(RedirectToAction("MyDownloads"));
        }