Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added range limitations for ratings, defined by constants in the Revi… #38

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Controllers/ReviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public async Task<ActionResult<Review>> Get([FromQuery] bool mostRecent = true,
[HttpPost]
public async Task<ActionResult> Post(Review review)
{
// check validity of raing value
if (review.Rating < Review.MIN_RATING || review.Rating > Review.MAX_RATING) {
string error = "Error. Ensure rating is between " + Review.MIN_RATING + " and " + Review.MAX_RATING + ".";
return BadRequest(error);
}

// add review to database
await _reviews.InsertOneAsync(review);
return CreatedAtAction(nameof(GetById), new { id = review.Id }, review);
}
Expand All @@ -93,6 +100,13 @@ public async Task<ActionResult> Post(Review review)
[HttpPut]
public async Task<ActionResult> Update(Review review)
{
// check validity of raing value
if (review.Rating < Review.MIN_RATING || review.Rating > Review.MAX_RATING) {
string error = "Error. Ensure rating is between " + Review.MIN_RATING + " and " + Review.MAX_RATING + ".";
return BadRequest(error);
}

// update review
var filter = Builders<Review>.Filter.Eq(x => x.Id, review.Id);
await _reviews.ReplaceOneAsync(filter, review);
return Ok();
Expand Down
5 changes: 5 additions & 0 deletions Entities/Review.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace SimpleWebAppReact.Entities;
/// </summary>
public class Review
{
// static fields storing definitions of max/min rating values
public const short MIN_RATING = 1;
public const short MAX_RATING = 10;

// database elements
[BsonId]
[BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
Expand Down
Loading