public IHttpActionResult Postreview(review review) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //increment place rating and number of reviews int PlaceId = (int)review.PlaceId; place place = db.places.Find(PlaceId); if (place == null) { return BadRequest("Can't add review as no such place in database."); } else { //update place rating counts place.NumberReviews += 1; place.TotalRatings += (int)review.Rating; db.Entry(place).State = EntityState.Modified; db.reviews.Add(review); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = review.Id }, review); } }
public IHttpActionResult Putreview(int id, review review) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != review.Id) { return BadRequest(); } review SelectedReview = db.reviews.Find(id); place place = db.places.Find(review.PlaceId); int NewRating = (int)review.Rating; int OldRating = (int)SelectedReview.Rating; place.TotalRatings -= OldRating; place.TotalRatings += NewRating; SelectedReview.Headline = review.Headline; SelectedReview.Date = review.Date; SelectedReview.Rating = review.Rating; SelectedReview.ReviewText = review.ReviewText; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!reviewExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }