示例#1
0
        public void EditMovie(EditMovieBindingModel model)
        {
            this.CheckModelForNull(model);
            this.CheckMovieForDuplication(model);

            var genresId = model.GenreIds.Select(int.Parse);
            var genres   = this.Data.Genres.All().Where(g => genresId.Any(id => id == g.Id)).ToList();
            var actorsId = model.ActorIds.Select(int.Parse);
            var actors   = this.Data.Actors.All().Where(a => actorsId.Any(id => id == a.Id)).ToList();

            var movie = this.GetMovieById(model.Id);

            movie.Name = model.Name;
            movie.Year = model.Year;
            movie.DurationInMinutes = model.DurationInMinutes;
            movie.Size        = model.Size;
            movie.Price       = model.Price;
            movie.Poster      = model.Poster;
            movie.Trailer     = model.Trailer;
            movie.Description = model.Description;
            movie.Country     = model.Country;

            var genresToDelete = new List <Genre>();

            if (genresId.Count() < movie.Genres.Count)
            {
                genresToDelete.AddRange(movie.Genres.Where(genre => !genresId.Any(id => id == genre.Id)));
            }
            else
            {
                movie.Genres = genres;
            }

            var actorsToDelete = new List <Actor>();

            if (actorsId.Count() < movie.Actors.Count)
            {
                actorsToDelete.AddRange(movie.Actors.Where(actor => !actorsId.Any(id => id == actor.Id)));
            }
            else
            {
                movie.Actors = actors;
            }

            foreach (var act in actorsToDelete)
            {
                movie.Actors.Remove(act);
            }

            foreach (var genre in genresToDelete)
            {
                movie.Genres.Remove(genre);
            }

            this.Data.SaveChanges();
        }
示例#2
0
        private void CheckMovieForDuplication(EditMovieBindingModel model)
        {
            var doesItExist = this.Data.Movies.All()
                              .Any(m => m.Name == model.Name && m.Year == model.Year && m.Id != model.Id);

            if (doesItExist)
            {
                throw new InvalidOperationException($"There is already movie with name {model.Name} and year ${model.Year}");
            }
        }
        public ActionResult Edit(EditMovieBindingModel model)
        {
            var movie = this.movieService.LoadEditMovieData(model.Id);

            if (this.ModelState.IsValid)
            {
                try
                {
                    this.movieService.EditMovie(model);
                    this.AddNotification("Edited successfully", NotificationType.SUCCESS);
                    return(RedirectToAction("Index", "Movies", new { Area = "" }));
                }
                catch (Exception ex)
                {
                    this.AddNotification(ex.Message, NotificationType.ERROR);
                    return(this.View(movie));
                }
            }

            return(View(movie));
        }