コード例 #1
0
        public ActionResult Create(Movie currMovie)
        {
            try
            {
                var context = new MovieEntities();

                using (context)
                {
                    var director = GetPeople(currMovie, context, Kind.Director);
                    var maleActor = GetPeople(currMovie, context, Kind.MaleActor);
                    var femaleActor = GetPeople(currMovie, context, Kind.FemaleActor);

                    var movieToAdd = new Movies()
                    {
                        title = currMovie.Title,
                        year = currMovie.Year,
                        directorId = director.id,
                        leadingMaleRoleId = maleActor.id,
                        leadingFemaleRoleId = femaleActor.id
                    };

                    context.Movies.Add(movieToAdd);
                    context.SaveChanges();
                }

                return View();
            }
            catch
            {
                return View("Error");
            }
        }
コード例 #2
0
        private Movie GenerateMovieModel(Movies movie)
        {
            var movieModel = new Movie()
            {
                Id = movie.id,
                Title = movie.title,
                Year = movie.year,
                Director = new MoviesCrud.ViewModels.People()
                {
                    FirstName = movie.Director.firstName,
                    LastName = movie.Director.lastName,
                    Age = movie.Director.age,
                    Studio = new MoviesCrud.ViewModels.Studio()
                    {
                        Address = movie.Director.Studio.address,
                        Name = movie.Director.Studio.name
                    }
                },
                LeadingFemale = new MoviesCrud.ViewModels.People()
                {
                    FirstName = movie.LeadingFemale.firstName,
                    LastName = movie.LeadingFemale.lastName,
                    Age = movie.LeadingFemale.age,
                    Studio = new MoviesCrud.ViewModels.Studio()
                    {
                        Address = movie.LeadingFemale.Studio.address,
                        Name = movie.LeadingFemale.Studio.name
                    }
                },
                LeadingMale = new MoviesCrud.ViewModels.People()
                {
                    FirstName = movie.LeadingMale.firstName,
                    LastName = movie.LeadingMale.lastName,
                    Age = movie.LeadingMale.age,
                    Studio = new MoviesCrud.ViewModels.Studio()
                    {
                        Address = movie.LeadingMale.Studio.address,
                        Name = movie.LeadingMale.Studio.name
                    }
                }
            };

            return movieModel;
        }
コード例 #3
0
        private Models.People AddPeople(Movie currMovie, MovieEntities context, Models.People people, Kind kind)
        {
            ViewModels.People currPeople = GetCurrPeople(currMovie, kind);

            var currStudio = context.Studio.FirstOrDefault(x => x.name == currPeople.Studio.Name);

            if (currStudio == null)
            {
                currStudio = AddStudio(currPeople.Studio, context);
            }

            people = new Models.People()
            {
                firstName = currMovie.Director.FirstName,
                lastName = currMovie.Director.LastName,
                age = currMovie.Director.Age,
                studioId = currStudio.id,
                kindId = (int)kind
            };

            context.People.Add(people);
            context.SaveChanges();

            return people;
        }
コード例 #4
0
        private Models.People GetPeople(Movie currMovie, MovieEntities context, Kind kind)
        {
            ViewModels.People currPeople = GetCurrPeople(currMovie, kind);

            var kindName = Enum.GetName(typeof(Kind), kind);

            var people = context.People.Include("Kinds")
                            .FirstOrDefault(x => x.Kinds.name == kindName &&
                                            x.firstName == currPeople.FirstName &&
                                            x.lastName == currPeople.LastName);

            if (people == null)
            {
                people = this.AddPeople(currMovie, context, people, Kind.Director);
            }

            return people;
        }
コード例 #5
0
        private ViewModels.People GetCurrPeople(Movie currMovie, Kind kind)
        {
            ViewModels.People currPeople = new ViewModels.People();

            switch (kind)
            {
                case Kind.Director:
                    currPeople = currMovie.Director;
                    break;
                case Kind.MaleActor:
                    currPeople = currMovie.LeadingMale;
                    break;
                default:
                    currPeople = currMovie.LeadingFemale;
                    break;
            }

            return currPeople;
        }
コード例 #6
0
        public ActionResult Delete(int id, Movie movie)
        {
            try
            {
                var context = new MovieEntities();
                using (context)
                {
                    var movieToRemove = context.Movies.Find(id);
                    if (movieToRemove != null)
                    {
                        context.Movies.Remove(movieToRemove);
                        context.SaveChanges();
                    }
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
コード例 #7
0
        public ActionResult Edit(int id, Movie currMovie)
        {
            try
            {
                var context = new MovieEntities();
                using (context)
                {
                    var movieToUpdate = context.Movies.Find(id);

                    if (movieToUpdate != null)
                    {
                        var director = GetPeople(currMovie, context, Kind.Director);
                        var maleActor = GetPeople(currMovie, context, Kind.MaleActor);
                        var femaleActor = GetPeople(currMovie, context, Kind.FemaleActor);

                        movieToUpdate.title = currMovie.Title;
                        movieToUpdate.year = currMovie.Year;
                        movieToUpdate.leadingMaleRoleId = maleActor.id;
                        movieToUpdate.leadingFemaleRoleId = femaleActor.id;
                        movieToUpdate.directorId = director.id;

                        context.SaveChanges();
                    }
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }