From 677c05876016df9ac92316d02df0fbdff7c89411 Mon Sep 17 00:00:00 2001 From: davidt3050 Date: Thu, 24 Oct 2024 20:20:25 -0400 Subject: [PATCH] Added range limitations for ratings, defined by constants in the Review entity class. --- Controllers/ReviewController.cs | 14 ++++++++++++++ Entities/Review.cs | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/Controllers/ReviewController.cs b/Controllers/ReviewController.cs index 3604540..98e4869 100644 --- a/Controllers/ReviewController.cs +++ b/Controllers/ReviewController.cs @@ -81,6 +81,13 @@ public async Task> Get([FromQuery] bool mostRecent = true, [HttpPost] public async Task 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); } @@ -93,6 +100,13 @@ public async Task Post(Review review) [HttpPut] public async Task 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.Filter.Eq(x => x.Id, review.Id); await _reviews.ReplaceOneAsync(filter, review); return Ok(); diff --git a/Entities/Review.cs b/Entities/Review.cs index 06fb22f..02c290b 100644 --- a/Entities/Review.cs +++ b/Entities/Review.cs @@ -6,6 +6,11 @@ namespace SimpleWebAppReact.Entities; /// 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; }