public void DeleteMovieWhenExistingItemShouldDelete()
        {
            //Arrange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var movie = new Movie()
            {
                Id = 1,
                Length = 10,
                Ration = 10
            };

            //Act -> perform some logic
            repo.Add(movie);
            repo.SaveChanges();

            var movieFromDb = repo.GetById(movie.Id);

            repo.Delete(movieFromDb);
            repo.SaveChanges();

            var newMovieFromDb = repo.GetById(movie.Id);
            
            //Asssert -> expect and exception
            Assert.IsNull(newMovieFromDb);
        }
        public void AddMovieWhenMovieIsInvalidShouldThrowException()
        {
            //Arrange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var invalidMovie = new Movie()
            {
                Id = 1,
                Length = 20,
                Ration = 20
            };

            //Act -> perform some logic
            repo.Add(invalidMovie);
            repo.SaveChanges();

            //Asssert -> expect and exception
        }
        public void AddMovieWhenMovieIsAddedToDbShouldReturnMovie()
        {
            //Arange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var movie = new Movie()
            {
                Id = 1,
                Length = 1,
                Ration = 3
            };

            //Act -> perform some logic
            repo.Add(movie);
            repo.SaveChanges();

            //Assert -> validate the results
            var movieFromDb = repo.GetById(movie.Id);

            Assert.IsNotNull(movieFromDb);
            Assert.AreEqual(movie.Length, movieFromDb.Length);
            Assert.AreEqual(movie.Ration, movieFromDb.Ration);
            Assert.IsTrue(movieFromDb.Id != 0);
        }
        public IHttpActionResult AddMovie([FromBody] MovieBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var moive = new Movie()
            {
                Title = model.Title,
                Length = model.Length,
                Ration = model.Rotation
            };

            this.Data.Moveis.Add(moive);
            this.Data.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = moive.Id }, moive);
        }
        public void ModifyMovieWhenExistingItemShouldModify()
        {
            //Arrange -> prepare the objects
            var repo = new GenericRepository<Movie>(new MoviesGalleryContext());
            var movie = new Movie()
            {
                Id = 1,
                Length = 10,
                Ration = 10
            };

            //Act -> perform some logic
            repo.Add(movie);
            repo.SaveChanges();

            movie.Length = 2;
            movie.Ration = 7;

            repo.Update(movie);
            repo.SaveChanges();

            var movieFromDb = repo.GetById(movie.Id);

            Assert.IsNotNull(movieFromDb);
            Assert.AreEqual(2, movieFromDb.Length);
            Assert.AreEqual(7, movieFromDb.Ration);
            Assert.IsTrue(movieFromDb.Id != 0);
        }
        public IHttpActionResult PostMovie(AddMovieInputModel movieModel)
        {
            if (movieModel == null)
            {
                return this.BadRequest("Model cannot be null");
            }
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!this.MoviesGalleryData.Genres.All().Any(g => g.Id == movieModel.GenreId))
            {
                return this.BadRequest("Genre with such id does not exists");
            }

            if (movieModel.Ration > 10 || movieModel.Ration < 1)
            {
                return this.BadRequest("Ration must be in range [1..10]");
            }

            var movie = new Movie()
                        {
                            Title = movieModel.Title,
                            Length = movieModel.Length,
                            Ration = movieModel.Ration,
                            Country = movieModel.Country,
                            GenreId = movieModel.GenreId
                        };

            this.MoviesGalleryData.Movies.Add(movie);
            this.MoviesGalleryData.SaveChanges();
            var result = new { movie.Id, movie.Title };
            return this.CreatedAtRoute("DefaultApi", new { id = movie.Id }, result);
        }