Esempio n. 1
0
        // GET: Movies/Create
        public ActionResult Create(Movie movie)
        {
            CreateMovieViewModel model = new CreateMovieViewModel();

            if (movie.Name != null)
            {
                Populate     n           = new Populate();
                Composite    pop         = n.Popul(movie);
                Producer     newproducer = new Producer();
                List <Actor> newactors   = new List <Actor>();

                newproducer = pop.Producer;
                newactors   = pop.Actors;

                string   pname    = newproducer.Name;
                Producer producer = _producerRepository.GetProducers().FirstOrDefault(a => a.Name == pname);
                if (producer == null)
                {
                    _producerRepository.InsertProducer(newproducer);
                    _producerRepository.Save();
                }

                List <int> selectedActorIDs = new List <int>();


                List <Actor> actors     = _actorRepository.GetActors().ToList();
                List <Actor> tempActors = new List <Actor>();

                foreach (var b in newactors)
                {
                    string aname = b.Name;
                    Actor  actor = actors.FirstOrDefault(a => a.Name == aname);
                    if (actor == null)
                    {
                        tempActors.Add(b);
                    }
                }

                foreach (var b in tempActors)
                {
                    _actorRepository.InsertActor(b);
                    _actorRepository.Save();
                }

                actors = _actorRepository.GetActors().ToList();
                //foreach (var b in newactors)
                //    selectedActorIDs.Add(actors.FirstOrDefault(a => a.tmdb_id == b.tmdb_id).ID);

                List <int> toadd = (from na in newactors
                                    join a in actors on na.TmdbId equals a.TmdbId
                                    select a.Id).ToList();

                selectedActorIDs.AddRange(toadd);

                SelectList      producerslist = new SelectList(_producerRepository.GetProducers().OrderBy(i => i.Name), "Id", "Name", _producerRepository.GetProducers().FirstOrDefault(a => a.TmdbId == newproducer.TmdbId).Id);
                MultiSelectList actorslist    = new MultiSelectList(_actorRepository.GetActors().OrderBy(i => i.Name), "Id", "Name", selectedActorIDs);
                model.Name      = movie.Name;
                model.Plot      = movie.Plot;
                model.Year      = movie.Year;
                model.Image     = movie.Image;
                model.Producers = producerslist;
                model.Actors    = actorslist;
                model.TmdbId    = movie.TmdbId;
            }
            else
            {
                SelectList      producerslist = new SelectList(_producerRepository.GetProducers().OrderBy(i => i.Name), "Id", "Name");
                MultiSelectList actorslist    = new MultiSelectList(_actorRepository.GetActors().OrderBy(i => i.Name), "Id", "Name");
                model.Producers = producerslist;
                model.Actors    = actorslist;
            }

            return(View(model));
        }