示例#1
0
        public IActionResult Create(int star, string reviewBody, int reservationId, int apartmentId)
        {
            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            Review newReview = new Review {
                ApartmentId = apartmentId, GuestId = userId, Rating = star, ReviewBody = reviewBody, Date = DateTime.Now
            };

            this._db.Reviews.Add(newReview);
            this._db.SaveChanges();

            Apartment apartment = this._db.Apartments.Include(ap => ap.Reviews).FirstOrDefault(ap => ap.Id == apartmentId);

            apartment.CalculateRating(star);

            this._db.Entry(apartment).State = EntityState.Modified;
            this._db.SaveChanges();

            return(RedirectToAction("Details", "Apartment", new { id = apartmentId }));
        }