public ReviewComment(model.ReviewComment item = null) : base(item) { if (item != null) { this.InjectFrom(item); Body = item.Comment; Title = item.Review.Title; ItemName = item.Review.ItemId; ReviewType = ReviewType.Comment; } }
public HttpResponseMessage AddReviewComment(AddCommentParameters commentParams) { if (ModelState.IsValid) { var productreview = _repository.Reviews.FirstOrDefault(r => r.ReviewId == commentParams.Id); if (productreview == null) { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent("Review with given id not found".Localize()); return response; } try { var reviewComment = new ReviewComment { AuthorId = UserHelper.CustomerSession.CustomerId, AuthorLocation = commentParams.AuthorLocation, AuthorName = commentParams.AuthorName, Comment = commentParams.Comment, ReviewId = productreview.ReviewId, ReviewCommentId = Guid.NewGuid().ToString(), Status = (int)ReviewStatus.Pending }; productreview.ReviewComments.Add(reviewComment); _repository.Update(productreview); _repository.UnitOfWork.Commit(); return Request.CreateResponse(HttpStatusCode.OK, new MReviewComment { Comment = reviewComment.Comment, CreatedDateTime = reviewComment.Created, Id = reviewComment.ReviewCommentId, Reviewer = new MReviewer { Address = reviewComment.AuthorLocation, Id = reviewComment.AuthorId, NickName = reviewComment.AuthorName } }); } catch { var response = Request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent("Error while saving data to database.".Localize()); return response; } } return Request.CreateResponse(HttpStatusCode.BadRequest); }
public void Can_save_review_comments() { var client = GetRepository(); var reviewId = Guid.NewGuid().ToString(); var customerId = Guid.NewGuid().ToString(); Review dbReviewreview = new Review() { ReviewId = reviewId, AuthorId = customerId, AuthorLocation = "Los Angeles", AuthorName = "NickName", ItemId = "someitem", ItemUrl = "url", OverallRating = 5, Title = "comment", IsVerifiedBuyer = true, Status = (int)ReviewStatus.Pending }; client.Add(dbReviewreview); client.UnitOfWork.Commit(); //RefreshClient RefreshRepository(ref client); var loadedReview = client.Reviews.Where(r => r.ReviewId == reviewId).SingleOrDefault(); //client.Attach(loadedReview); ReviewComment reviewComment = new ReviewComment(); reviewComment.AuthorId = customerId; reviewComment.AuthorLocation = "Los Angeles"; reviewComment.AuthorName = "Author"; reviewComment.Comment = "Comment"; reviewComment.ReviewCommentId = Guid.NewGuid().ToString(); reviewComment.Status = (int)ReviewStatus.Pending; loadedReview.ReviewComments.Add(reviewComment); //client.Update(loadedReview); client.UnitOfWork.Commit(); RefreshRepository(ref client); loadedReview = (from r in client.Reviews where r.ReviewId == reviewId select r).ExpandAll().SingleOrDefault(); // test delete from collection, object should be removed from db as well loadedReview.ReviewComments.Remove(loadedReview.ReviewComments[0]); client.UnitOfWork.Commit(); RefreshRepository(ref client); loadedReview = (from r in client.Reviews where r.ReviewId == reviewId select r).ExpandAll().SingleOrDefault(); Assert.True(loadedReview.ReviewComments.Count == 0); // test saving just a comment by itself var reviewComment2 = new ReviewComment(); reviewComment2.AuthorId = customerId; reviewComment2.AuthorLocation = "Los Angeles"; reviewComment2.AuthorName = "Author"; reviewComment2.ReviewId = reviewId; reviewComment2.Comment = "Comment"; reviewComment2.ReviewCommentId = Guid.NewGuid().ToString(); reviewComment2.Status = (int)ReviewStatus.Pending; client.Add(reviewComment2); client.UnitOfWork.Commit(); var comment2 = (from r in client.ReviewComments where r.ReviewId == reviewId && r.ReviewCommentId == reviewComment2.ReviewCommentId select r).SingleOrDefault(); Assert.True(comment2 != null); // now modify it reviewComment2.AuthorName = "Author2"; client.UnitOfWork.Commit(); comment2 = (from r in client.ReviewComments where r.ReviewId == reviewId && r.ReviewCommentId == reviewComment2.ReviewCommentId select r).SingleOrDefault(); Assert.True(comment2 != null); }