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"); }