示例#1
0
        public ActionResult Edit(FeatureFilmViewModel viewModel, HttpPostedFileBase image,
                                 IList <string> actor, IList <string> director,
                                 IList <string> category, IList <string> producer,
                                 IList <string> resolution, IList <string> tag)
        {
            Movie oldMovie = _movieService.Find(viewModel.MoviesViewModel.Id);

            var movie = AutoMapper.Mapper.Map <Movie>(viewModel.MoviesViewModel);
            var film  = AutoMapper.Mapper.Map <Film>(viewModel.FilmViewModel);

            if (image != null &&
                image.FileName != null &&
                CheckImageUploadExtension.CheckImagePath(image.FileName) == true)
            {
                var path = Path.Combine(Server.MapPath("~/Images/Upload"), image.FileName);
                image.SaveAs(path);
                movie.Thumbnail = VariableUtils.UrlUpLoadImage + image.FileName;
            }
            else
            {
                if (oldMovie.Thumbnail != null)
                {
                    movie.Thumbnail = oldMovie.Thumbnail;
                }
            }
            film.MovieId = movie.Id;
            _movieService.Update(movie, movie.Id);
            _filmService.Update(film, film.Id);

            IList <string> _actor = new List <string>();

            if (actor != null)
            {
                foreach (var item in actor)
                {
                    _actor.Add(item);
                }
            }
            var listActorMovie = _actorMovieService.GetIdActorByMovieId(movie.Id);

            foreach (var item in listActorMovie)
            {
                if (!_actor.Contains(item.ToString()))
                {
                    var model = _actorMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _actorMovieService.Delete(model);
                    }
                }
                else
                {
                    _actor.Remove(item.ToString());
                }
            }

            IList <string> _director = new List <string>();

            if (director != null)
            {
                foreach (var item in director)
                {
                    _director.Add(item);
                }
            }
            var listDirectorMovie = _directorMovieService.GetIdDirectorByMovieId(movie.Id);

            foreach (var item in listDirectorMovie)
            {
                if (!_director.Contains(item.ToString()))
                {
                    var model = _directorMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _directorMovieService.Delete(model);
                    }
                }
                else
                {
                    _director.Remove(item.ToString());
                }
            }

            IList <string> _category = new List <string>();

            if (category != null)
            {
                foreach (var item in category)
                {
                    _category.Add(item);
                }
            }
            var listCategoryMovie = _categoryMovieService.GetIdCategoryByMovieId(movie.Id);

            foreach (var item in listCategoryMovie)
            {
                if (!_category.Contains(item.ToString()))
                {
                    var model = _categoryMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _categoryMovieService.Delete(model);
                    }
                }
                else
                {
                    _category.Remove(item.ToString());
                }
            }

            IList <string> _producer = new List <string>();

            if (producer != null)
            {
                foreach (var item in producer)
                {
                    _producer.Add(item);
                }
            }
            var listProducerMovie = _producerMovieService.GetIdProducerByMovieId(movie.Id);

            foreach (var item in listProducerMovie)
            {
                if (!_producer.Contains(item.ToString()))
                {
                    var model = _producerMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _producerMovieService.Delete(model);
                    }
                }
                else
                {
                    _producer.Remove(item.ToString());
                }
            }

            IList <string> _resolution = new List <string>();

            if (resolution != null)
            {
                foreach (var item in resolution)
                {
                    _resolution.Add(item);
                }
            }
            var listResolutionMovie = _resolutionMovieService.GetIdResolutionByMovieId(movie.Id);

            foreach (var item in listResolutionMovie)
            {
                if (!_resolution.Contains(item.ToString()))
                {
                    var model = _resolutionMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _resolutionMovieService.Delete(model);
                    }
                }
                else
                {
                    _resolution.Remove(item.ToString());
                }
            }

            IList <string> _tag = new List <string>();

            if (tag != null)
            {
                foreach (var item in tag)
                {
                    _tag.Add(item);
                }
            }
            var listTagMovie = _tagMovieService.GetIdTagByMovieId(movie.Id);

            foreach (var item in listTagMovie)
            {
                if (!_tag.Contains(item.ToString()))
                {
                    var model = _tagMovieService.FindBy2Id(movie.Id, item);
                    if (model != null)
                    {
                        _tagMovieService.Delete(model);
                    }
                }
                else
                {
                    _tag.Remove(item.ToString());
                }
            }


            if (_actor != null)
            {
                foreach (var item in _actor)
                {
                    _actorMovieService.Create(new ActorMovie()
                    {
                        ActorId = Guid.Parse(item),
                        MovieId = movie.Id
                    });
                }
            }
            if (_director != null)
            {
                foreach (var item in _director)
                {
                    _directorMovieService.Create(new DirectorMovie()
                    {
                        DirectorId = Guid.Parse(item),
                        MovieId    = movie.Id
                    });
                }
            }
            if (_category != null)
            {
                foreach (var item in _category)
                {
                    _categoryMovieService.Create(new CategoryMovie()
                    {
                        CategoryId = Guid.Parse(item),
                        MovieId    = movie.Id
                    });
                }
            }
            if (_producer != null)
            {
                foreach (var item in _producer)
                {
                    _producerMovieService.Create(new ProducerMovie()
                    {
                        ProducerId = Guid.Parse(item),
                        MovieId    = movie.Id
                    });
                }
            }
            if (_resolution != null)
            {
                foreach (var item in _resolution)
                {
                    _resolutionMovieService.Create(new MovieResolution()
                    {
                        ResolutionId = Guid.Parse(item),
                        MovieId      = movie.Id
                    });
                }
            }
            if (_tag != null)
            {
                foreach (var item in _tag)
                {
                    _tagMovieService.Create(new TagMovie()
                    {
                        TagId   = Guid.Parse(item),
                        MovieId = movie.Id
                    });
                }
            }
            return(RedirectToAction("Index"));
        }