Esempio n. 1
0
        public void Should_AddNewRatingToDatabase()
        {
            //arrange
            var rating = new MovieRating {
                MovieId = 1, Rating = 2, UserId = 3
            };

            //act
            GetRepository().SaveRatingAsync(rating);

            var ratingCount = 0;
            var firstRating = new Repo.MovieRating();

            using (var context = GetContext())
            {
                ratingCount = context.MovieRatingDbSet.Count();
                firstRating = context.MovieRatingDbSet.First();
            }

            //assert
            Assert.AreEqual(1, ratingCount);

            Assert.IsNotNull(firstRating);
            Assert.AreEqual(rating.MovieId, firstRating.MovieId);
            Assert.AreEqual(rating.Rating, firstRating.Rating);
            Assert.AreEqual(rating.UserId, firstRating.UserId);
        }
Esempio n. 2
0
        public void Should_UpdateExistingRatingInDatabase()
        {
            //arrange
            var existingRating = new Repo.MovieRating {
                MovieId = 1, UserId = 2, Rating = 3
            };
            var newRating = new MovieRating {
                MovieId = 1, UserId = 2, Rating = 4
            };

            InsertRating(existingRating);

            //act
            GetRepository().SaveRatingAsync(newRating);

            var ratingCount = 0;
            var firstRating = new Repo.MovieRating();

            using (var context = GetContext())
            {
                ratingCount = context.MovieRatingDbSet.Count();
                firstRating = context.MovieRatingDbSet.First();
            }

            //assert
            Assert.AreEqual(1, ratingCount);

            Assert.IsNotNull(firstRating);
            Assert.AreEqual(existingRating.MovieId, firstRating.MovieId);
            Assert.AreEqual(newRating.Rating, firstRating.Rating);
            Assert.AreEqual(existingRating.UserId, firstRating.UserId);
        }
Esempio n. 3
0
        public async Task <bool> SaveRatingAsync(MovieRating movieRating)
        {
            var rating = _context.MovieRatingDbSet.FirstOrDefault(mr => mr.MovieId == movieRating.MovieId && mr.UserId == movieRating.UserId);

            if (rating != null)
            {
                rating.Rating = movieRating.Rating;
            }
            else
            {
                rating = new Repo.MovieRating
                {
                    MovieId = movieRating.MovieId,
                    UserId  = movieRating.UserId,
                    Rating  = movieRating.Rating
                };

                _context.MovieRatingDbSet.Add(rating);
            }

            return(await _context.SaveChangesAsync() > 0);
        }