Example #1
0
        public ActionResult Edit(Movie updatedMovie, int id, IFormCollection collection)
        {
            try
            {
                _movieRepo.Edit(updatedMovie);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Example #2
0
        public async Task CanEditMovie()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                              .UseSqlite(connection)
                              .Options;

                using (var context = new ApplicationDbContext(options))
                {
                    context.Database.EnsureCreated();
                }

                var movie = new Movie
                {
                    Id       = Guid.NewGuid(),
                    Director = "Shelby Mansker",
                    Name     = "Life Sucks"
                };

                var newMovieName = "Life is Awesome";

                using (var context = new ApplicationDbContext(options))
                {
                    context.Movies.Add(movie);
                    context.SaveChanges();
                }

                using (var context = new ApplicationDbContext(options))
                {
                    movie.Name = newMovieName;

                    var repo = new MovieRepository(context);
                    await repo.Edit(movie);
                }

                using (var context = new ApplicationDbContext(options))
                {
                    Assert.AreEqual(1, context.Movies.Count());
                    Assert.AreEqual(newMovieName, context.Movies.Single().Name);
                }
            }
            finally
            {
                connection.Close();
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            // get the configuration from file
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            // now i can access the connection string like this
            // this will be null if there isn't an appsettings with this connection string
            Console.WriteLine(configuration.GetConnectionString("MoviesDB"));

            // ORM: object-relational mapper
            // our ORM in .NET is: Entity Framework
            // we will use database-first approach to EF
            Console.WriteLine("Hello World!");

            // provide the connection string to the dbcontext
            var optionsBuilder = new DbContextOptionsBuilder <MoviesDBContext>();

            optionsBuilder.UseSqlServer(configuration.GetConnectionString("MoviesDB"));

            var repo = new MovieRepository(new MoviesDBContext(optionsBuilder.Options));

            var movies = repo.GetMoviesWithGenres();

            foreach (var item in movies)
            {
                Console.WriteLine($"Name {item.Name}," +
                                  $" genre {item.Genre.Name}");
            }

            //edit a movie
            var aMovie = movies.First();

            aMovie.Name = "A New Hope";
            repo.Edit(aMovie);
            repo.AddMovie("Die Hard", DateTime.Now, "action");
            repo.DeleteById(aMovie.Id);
            repo.SaveChanges();

            movies = repo.GetMoviesWithGenres();
            Console.WriteLine();
            foreach (var item in movies)
            {
                Console.WriteLine($"Name {item.Name}," +
                                  $" genre {item.Genre.Name}");
            }
        }
Example #4
0
        public IHttpActionResult Update(UpdateMovieRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Movie movie = MovieRepository.Get(request.MovieId);

            if (movie == null)
            {
                return(NotFound());
            }

            movie.Title  = request.Title;
            movie.Rating = request.Rating;

            MovieRepository.Edit(movie);
            return(Ok(movie));
        }
Example #5
0
        public ActionResult Edit(Movie movie)
        {
            movieRepo.Edit(movie);

            return(RedirectToAction("Index", "Home"));
        }