Esempio n. 1
0
        public ActionResult AddChannelReview(int id, [FromBody] Review review)
        {
            var user = review.User;

            _context.Users.Add(user);
            _context.SaveChanges();
            review.User = user; // UserID added with autoincrement

            var channel = _context.Channels.Single(channel => channel.ChannelID == review.ChannelID);

            channel.RatingAverage = (channel.RatingAverage * channel.ReviewCount + review.Rating) / (channel.ReviewCount + 1);
            channel.ReviewCount++;
            _context.Reviews.Add(review);
            _context.SaveChanges();
            _cache.AddReview(_context.Reviews.Include(review => review.Channel).Include(review => review.User).Single(r => r.ReviewID == review.ReviewID));
            return(CreatedAtAction("AddChannelReview", new { Id = review.ReviewID }, review));
        }