public ActionResult Create(string title, string director, DateTime? year,
            string leadingFemaleRole, string leadingMaleRole)
        {
            Actor femaleRole = db.Actors.FirstOrDefault(a => a.Name == leadingFemaleRole);
            Actor maleRole = db.Actors.FirstOrDefault(a => a.Name == leadingMaleRole);

            Movie newMovie = new Movie()
            {
                Title = title,
                Director = director,
                Year = (DateTime)year,
                LeadingFemaleRole = femaleRole,
                LeadingMaleRole = maleRole
            };

            db.Movies.Add(newMovie);
            db.SaveChanges();

            var actors = db.Actors;

            return RedirectToAction("Index");
        }
        private Movie GetNewMovie(MovieViewModel model)
        {
            var movie = new Movie
            {
                Title = model.Title,
                Director = model.Director,
                Year = model.Year
            };

            var maleRoleId = -1;
            var femaleRoleId = -1;

            if (this.movies.MaleRoleExist(model.LeadingMaleRole.Name, model.LeadingMaleRole.Age))
            {
                maleRoleId = this.movies.GetMaleRoleId(model.LeadingMaleRole.Name, model.LeadingMaleRole.Age);
            }

            if (this.movies.FemaleRoleExist(model.LeadingFemaleRole.Name, model.LeadingFemaleRole.Age))
            {
                femaleRoleId = this.movies.GetFemaleRoleId(model.LeadingFemaleRole.Name, model.LeadingFemaleRole.Age);
            }

            if (maleRoleId != -1)
            {
                movie.LeadingMaleRoleId = maleRoleId;
            }
            else
            {
                movie.LeadingMaleRole = new Actor()
                {
                    Age = model.LeadingMaleRole.Age,
                    Gender = GenderType.Male,
                    Name = model.LeadingMaleRole.Name,
                    Studio = new Studio()
                    {
                        Name = model.LeadingMaleRole.StudioName,
                        Address = model.LeadingMaleRole.StudioAddress
                    }
                };
            }

            if (femaleRoleId != -1)
            {
                movie.LeadingFemaleRoleId = femaleRoleId;
            }
            else
            {
                movie.LeadingFemaleRole = new Actor()
                {
                    Age = model.LeadingFemaleRole.Age,
                    Gender = GenderType.Female,
                    Name = model.LeadingFemaleRole.Name,
                    Studio = new Studio()
                    {
                        Name = model.LeadingFemaleRole.StudioName,
                        Address = model.LeadingFemaleRole.StudioAddress
                    }
                };
            }

            return movie;
        }
        private Movie UpdateModel(Movie modelForUpdate, MovieViewModel model)
        {
            modelForUpdate.Title = model.Title;
            modelForUpdate.Director = model.Director;
            modelForUpdate.Year = model.Year;

            modelForUpdate.LeadingMaleRole.Age = model.LeadingMaleRole.Age;
            modelForUpdate.LeadingMaleRole.Gender = model.LeadingMaleRole.GenderId == 0 ? GenderType.Male : GenderType.Female;
            modelForUpdate.LeadingMaleRole.Name = model.LeadingMaleRole.Name;
            modelForUpdate.LeadingMaleRole.Studio.Name = model.LeadingMaleRole.StudioName;
            modelForUpdate.LeadingMaleRole.Studio.Address = model.LeadingMaleRole.StudioAddress;

            modelForUpdate.LeadingFemaleRole.Age = model.LeadingFemaleRole.Age;
            modelForUpdate.LeadingFemaleRole.Gender = model.LeadingFemaleRole.GenderId == 0 ? GenderType.Male : GenderType.Female;
            modelForUpdate.LeadingFemaleRole.Name = model.LeadingFemaleRole.Name;
            modelForUpdate.LeadingFemaleRole.Studio.Name = model.LeadingFemaleRole.StudioName;
            modelForUpdate.LeadingFemaleRole.Studio.Address = model.LeadingFemaleRole.StudioAddress;

            return modelForUpdate;
        }
 public void Add(Movie movie)
 {
     this.movies.Add(movie);
     this.movies.SaveChanges();
 }
 public void Update(Movie movie)
 {
     this.movies.Update(movie);
     this.movies.SaveChanges();
 }