Skip to content

Commit

Permalink
Merge pull request #192 from vtex-apps/fix/REVIEWS-96_validations-bod…
Browse files Browse the repository at this point in the history
…y-fields-rest-api

[REVIEWS-96] Added body fields validations for post methods rest api.
  • Loading branch information
Arturo777 authored Jun 1, 2022
2 parents b05e0e7 + cb70e76 commit 5fcaed0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Added body fields validations for post methods rest api.

## [3.8.14] - 2022-05-24

### Added
Expand Down
57 changes: 57 additions & 0 deletions dotnet/Controllers/RoutesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public async Task<IActionResult> ProcessReviewApiAction(string requestedAction,
}

Review newReview = JsonConvert.DeserializeObject<Review>(bodyAsText);

if(newReview.ProductId == null)
{
return BadRequest("ProductId is missing.");
}
if(newReview.Rating == null)
{
return BadRequest("Rating is missing.");
}
if(newReview.Title == null)
{
return BadRequest("Title is missing.");
}
if(newReview.Text == null)
{
return BadRequest("Text is missing.");
}
if(newReview.ReviewerName == null)
{
return BadRequest("ReviewerName is missing.");
}
if(newReview.Approved == null)
{
return BadRequest("Approved is missing.");
}

bool hasShopperReviewed = await _productReviewsService.HasShopperReviewed(validatedUser.User, newReview.ProductId);
if (hasShopperReviewed)
{
Expand Down Expand Up @@ -114,6 +140,37 @@ public async Task<IActionResult> ProcessReviewApiAction(string requestedAction,
IList<Review> reviews = JsonConvert.DeserializeObject<IList<Review>>(bodyAsText);
List<string> ids = new List<string>();
foreach (Review review in reviews)
{
if(review.VerifiedPurchaser == null)
{
review.VerifiedPurchaser = false;
}
if(review.ProductId == null)
{
return BadRequest("ProductId is missing for one or more reviews.");
}
if(review.Rating == null)
{
return BadRequest("Rating is missing for one or more reviews.");
}
if(review.Title == null)
{
return BadRequest("Title is missing for one or more reviews.");
}
if(review.Text == null)
{
return BadRequest("Text is missing for one or more reviews.");
}
if(review.ReviewerName == null)
{
return BadRequest("ReviewerName is missing for one or more reviews.");
}
if(review.Approved == null)
{
return BadRequest("Approved is missing for one or more reviews.");
}
}
foreach (Review review in reviews)
{
var reviewsResponse = await this._productReviewsService.NewReview(review, false);
ids.Add(reviewsResponse.Id);
Expand Down

0 comments on commit 5fcaed0

Please sign in to comment.