public void ReportHelpful(string reviewId, bool isHelpful, string authorId, string authorName, string authorLocation) { // Validate Incoming parameters if (string.IsNullOrEmpty(reviewId)) throw new ArgumentNullException("reviewId"); if (string.IsNullOrEmpty(authorId)) throw new ArgumentNullException("authorId"); if (string.IsNullOrEmpty(authorName)) throw new ArgumentNullException("authorName"); if (string.IsNullOrEmpty(authorLocation)) throw new ArgumentNullException("authorLocation"); var review = FindReviewById(reviewId); if (review != null) { // Skip duplicated Helpful var helpfulElement = FindReportHelpfulByAuthor(reviewId, authorId); if (helpfulElement != null) { return; } // Post Helpfull var newHelpfulElement = new ReportHelpfulElement(); newHelpfulElement.ReviewId = reviewId; //newHelpfulElement.ReportHelpfulElementId = Guid.NewGuid().ToString(); newHelpfulElement.AuthorId = authorId; //newHelpfulElement.AuthorFN = authorName; //newHelpfulElement.AuthorLocation = authorLocation; //newHelpfulElement.Status = "Pending"; newHelpfulElement.IsHelpful = isHelpful; _repository.Add(newHelpfulElement); _repository.UnitOfWork.Commit(); } }
public HttpResponseMessage VoteProductReview(VoteParameters voteParams) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest); } dynamic entity = voteParams.IsReview ? (StorageEntity) _repository.Reviews.FirstOrDefault(r => r.ReviewId == voteParams.Id) : _repository.ReviewComments.FirstOrDefault(r => r.ReviewCommentId == voteParams.Id); if (entity == null) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent("Entity with given id not found".Localize()); return response; } if (entity.AuthorId == UserHelper.CustomerSession.CustomerId) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(string.Format("You cannot vote for your own {0}".Localize(), voteParams.IsReview ? "review" : "comment")); return response; } if (_repository.ReportHelpfulElements.Any(rh => rh.AuthorId == UserHelper.CustomerSession.CustomerId && (voteParams.IsReview && rh.ReviewId == voteParams.Id || rh.CommentId == voteParams.Id))) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(string.Format("You may only submit one vote per {0}".Localize(), voteParams.IsReview ? "review." : "comment.")); return response; } if (voteParams.Like) { entity.TotalPositiveFeedbackCount++; } else { entity.TotalNegativeFeedbackCount++; } try { var sEntity = (StorageEntity)entity; _repository.Update(sEntity); var helpful = new ReportHelpfulElement { AuthorId = UserHelper.CustomerSession.CustomerId, IsHelpful = voteParams.Like, ReviewId = voteParams.IsReview ? voteParams.Id : null, CommentId = voteParams.IsReview ? null : voteParams.Id }; _repository.Add(helpful); _repository.UnitOfWork.Commit(); } catch (Exception) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent("Error while saving data to database.".Localize()); return response; } return Request.CreateResponse(HttpStatusCode.OK); }
public IHttpActionResult VoteProductReview(VoteParameters voteParams) { if (!ModelState.IsValid) { return new InvalidModelStateResult(ModelState, this); } dynamic entity = voteParams.IsReview ? (StorageEntity) _repository.Reviews.FirstOrDefault(r => r.ReviewId == voteParams.Id) : _repository.ReviewComments.FirstOrDefault(r => r.ReviewCommentId == voteParams.Id); if (entity == null) { return NotFound(); } if (entity.AuthorId == UserHelper.CustomerSession.CustomerId) { return BadRequest(string.Format("You cannot vote for your own {0}".Localize(), voteParams.IsReview ? "review" : "comment")); } if (_repository.ReportHelpfulElements.Any(rh => rh.AuthorId == UserHelper.CustomerSession.CustomerId && (voteParams.IsReview && rh.ReviewId == voteParams.Id || rh.CommentId == voteParams.Id))) { return BadRequest(string.Format("You may only submit one vote per {0}".Localize(), voteParams.IsReview ? "review." : "comment.")); } if (voteParams.Like) { entity.TotalPositiveFeedbackCount++; } else { entity.TotalNegativeFeedbackCount++; } try { var sEntity = (StorageEntity)entity; _repository.Update(sEntity); var helpful = new ReportHelpfulElement { AuthorId = UserHelper.CustomerSession.CustomerId, IsHelpful = voteParams.Like, ReviewId = voteParams.IsReview ? voteParams.Id : null, CommentId = voteParams.IsReview ? null : voteParams.Id }; _repository.Add(helpful); _repository.UnitOfWork.Commit(); } catch { return BadRequest("Error while saving data to database.".Localize()); } return Ok("ok"); }