} // Get by ID

        /// <summary>
        /// Adds an Artist Rating
        /// </summary>
        /// <param name="artistRating"></param>
        /// <returns></returns>
        public IHttpActionResult Post(ArtistRatingCreate artistRating)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateArtistRatingService();

            if (!service.CreateArtistRating(artistRating))
            {
                return(InternalServerError());
            }

            return(Ok());
        } // Post
Ejemplo n.º 2
0
        public bool CreateArtistRating(ArtistRatingCreate model)
        {
            // format the new ArtistRating record
            var entity =
                new ArtistRating()
            {
                OwnerId  = _userId,
                ArtistId = model.ArtistId,
                ArtistIndividualRating = model.ArtistIndividualRating,
                Artist = model.Artist
            };

            // Add the new ArtistRating to the table
            using (var ctx = new ApplicationDbContext())
            {
                ctx.ArtistRatings.Add(entity);
                bool addedArtistRating = ctx.SaveChanges() == 1;
                if (!addedArtistRating)
                {
                    return(false);
                }
            }

            // Update the Artist record
            using (var ctx = new ApplicationDbContext())
            {
                // retrieve the ArtistRating record we just posted so we can follow the foreign key to the Artist record
                var newArtistRating =
                    ctx
                    .ArtistRatings
                    .Single(e => e.ArtistRatingId == entity.ArtistRatingId && e.OwnerId == _userId);

                // Update the fields in the Artist record at the other end of the foreign key
                newArtistRating.Artist.CulumativeRating += model.ArtistIndividualRating;
                newArtistRating.Artist.NumberOfRatings  += 1;

                newArtistRating.Artist.ArtistRating = newArtistRating.Artist.CulumativeRating / newArtistRating.Artist.NumberOfRatings;

                return(ctx.SaveChanges() == 1);
            }
        } // CreateArtistRating