예제 #1
0
        // GET: Movies/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var movie = await _context.Movie.FindAsync(id);

            if (movie == null)
            {
                return(NotFound());
            }

            MovieEditModel newMovie = new MovieEditModel
            {
                Title       = movie.Title,
                ReleaseDate = movie.ReleaseDate,
                Genre       = movie.Genre,
                Price       = movie.Price,
                Rating      = movie.Rating,
                ImagePath   = movie.Image,
                Image       = null
            };

            return(View(newMovie));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, string actorSearchName = "")
        {
            //Avoid Null Exception when no search filter passed.
            if (string.IsNullOrEmpty(actorSearchName))
            {
                actorSearchName = "";
            }

            Movie foundMovie = await _movies.GetById(id);


            if (foundMovie != null)
            {
                MovieEditModel viewModel = new MovieEditModel
                {
                    Title       = foundMovie.Title,
                    Id          = foundMovie.Id,
                    Year        = foundMovie.Year,
                    Revenue     = foundMovie.Revenue,
                    Runtime     = foundMovie.Runtime,
                    Description = foundMovie.Description,
                    Director    = foundMovie.Director
                };

                //Build List of Actors
                ICollection <Actor> linkedActors = foundMovie.FilmCasts.Select(filmCast => filmCast.Actor).ToList();
                ICollection <Actor> allActors    = await _actors.GetAllLight();

                List <AssignedEntity> assignedActors = allActors.OrderBy(actor => actor.Name).Where(actor => actor.Name.Contains(actorSearchName))
                                                       .Select(actor => new AssignedEntity
                {
                    Id       = actor.Id,
                    Name     = actor.Name,
                    Assigned = linkedActors.Contains(actor)
                }).ToList();


                viewModel.AssignedActors = assignedActors;


                //Build List of Genres
                ICollection <Genre> linkedGenres = foundMovie.MovieGenres.Select(movieGenre => movieGenre.Genre).ToList();
                ICollection <Genre> allGenres    = await _genres.GetAllLight();

                viewModel.AssignedGenres = allGenres
                                           .Select(genre => new AssignedEntity
                {
                    Id       = genre.Id,
                    Name     = genre.Name,
                    Assigned = linkedGenres.Contains(genre)
                }).ToList();


                await PopulateDirectorDropDownList(foundMovie.Director.Id);

                return(View(viewModel));
            }

            return(NotFound());
        }
예제 #3
0
        public async Task <ActionResult> Edit(int id)
        {
            var movie = await context.Movies
                        .Include(m => m.User)
                        .Where(m => m.Id == id)
                        .FirstOrDefaultAsync();

            if (movie != null && User.Identity.Name == movie.User.UserName)
            {
                var model = new MovieEditModel()
                {
                    Id             = movie.Id.ToString(),
                    Name           = movie.Name,
                    ProductionYear = movie.ProductionYear,
                    Director       = movie.Director,
                    Description    = movie.Description
                };

                ViewData["Title"]     = "Редактирование фильма";
                ViewData["BannerBtn"] = "Заменить постер";
                ViewData["Action"]    = "Edit";
                return(PartialView("_EditPartial", model));
            }
            else
            {
                return(NotFound());
            }
        }
예제 #4
0
        public async Task <IActionResult> Delete(MovieEditModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.moviesService.Delete(input);

            return(this.Redirect($"/Movies/MoviesIndex"));
        }
예제 #5
0
        public async Task Delete(MovieEditModel input)
        {
            var movie = this.moviesRepository
                        .All()
                        .FirstOrDefault(x => x.Id == input.Id);

            movie.IsDeleted = true;
            movie.DeletedOn = DateTime.UtcNow;

            this.moviesRepository.Update(movie);
            await this.moviesRepository.SaveChangesAsync();
        }
예제 #6
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,ReleaseDate,Genre,Price,Rating,Image,ImagePath")] MovieEditModel movie)
        {
            if (id != movie.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string uniqueFileName = movie.ImagePath;

                    if (movie.Image != null)
                    {
                        // ;
                        string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "img");
                        uniqueFileName = Guid.NewGuid().ToString() + "_" + movie.Image.FileName;
                        string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                        movie.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                    }

                    Movie newMovie = new Movie
                    {
                        Id          = id,
                        Title       = movie.Title,
                        ReleaseDate = movie.ReleaseDate,
                        Genre       = movie.Genre,
                        Price       = movie.Price,
                        Rating      = movie.Rating,
                        Image       = uniqueFileName
                    };
                    _context.Update(newMovie);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MovieExists(movie.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(movie));
        }
예제 #7
0
        public async Task <IActionResult> Edit(MovieEditModel input)
        {
            if (input == null)
            {
                return(this.NotFound());
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.moviesService.Edit(input);

            return(this.RedirectToAction(nameof(this.MovieView), new { title = input.Title }));
        }
예제 #8
0
        public async Task <ActionResult> Edit(MovieEditModel model)
        {
            if (ModelState.IsValid)
            {
                var movie = await context.Movies
                            .Include(m => m.User)
                            .Where(m => m.Id == Int32.Parse(model.Id))
                            .FirstOrDefaultAsync();

                if (movie != null && User.Identity.Name == movie.User.UserName)
                {
                    movie.Name           = model.Name;
                    movie.ProductionYear = model.ProductionYear ?? 0;
                    movie.Director       = model.Director;
                    movie.Description    = model.Description;

                    if (model.BannerImage != null)
                    {
                        using (var stream = new MemoryStream())
                        {
                            model.BannerImage.CopyTo(stream);
                            movie.Banner = stream.ToArray();
                        }
                    }

                    context.Entry(movie).State = EntityState.Modified;
                    await context.SaveChangesAsync();

                    return(Ok());
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                ViewData["Title"]     = "Редактирование фильма";
                ViewData["BannerBtn"] = "Заменить постер";
                ViewData["Action"]    = "Edit";
                return(PartialView("_EditPartial", model));
            }
        }
예제 #9
0
        public async Task Edit_WithValidInput_ShouldReturnValidResult()
        {
            var dbContext = ApplicationDbContextCreatorInMemory.InitializeContext();

            await this.SeedData(dbContext);

            var moviesRepository = new EfDeletableEntityRepository <Movie>(dbContext);
            var genresRepository = new EfDeletableEntityRepository <Genre>(dbContext);
            var service          = new MoviesService(moviesRepository, genresRepository);

            var viewModel = new MovieEditModel()
            {
                Title = "Edited",
            };

            var result = service.Edit(viewModel);

            Assert.Equal("Edited", viewModel.Title);
        }
예제 #10
0
        public async Task Edit(MovieEditModel input)
        {
            var movie = this.moviesRepository
                        .All()
                        .FirstOrDefault(x => x.Id == input.Id);

            movie.Title        = input.Title;
            movie.Year         = input.Year;
            movie.ImageUrl     = input.ImageUrl;
            movie.Description  = input.Description;
            movie.HomePageLink = input.HomePageLink;
            movie.IMDBLink     = input.IMDBLink;
            movie.TrailerLink  = input.TrailerLink;
            movie.FacebookLink = input.FacebookLink;
            movie.Creater      = input.Creater;
            movie.Producer     = input.Producer;
            movie.Country      = input.Country;

            this.moviesRepository.Update(movie);
            await this.moviesRepository.SaveChangesAsync();
        }
예제 #11
0
        // GET: Movies/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var movie = await _context.Movie.FindAsync(id);

            if (movie == null)
            {
                return(NotFound());
            }
            var model = new MovieEditModel {
                Id          = movie.Id,
                Title       = movie.Title,
                Genre       = movie.Genre,
                ReleaseDate = movie.ReleaseDate,
                Price       = movie.Price,
                Genres      = await _context.Movie.Select(m => m.Genre).Distinct().ToListAsync()
            };

            return(View(model));
        }
예제 #12
0
        public async Task <ActionResult> Create(MovieEditModel model)
        {
            if (ModelState.IsValid)
            {
                var movie = new Movie()
                {
                    Name           = model.Name,
                    ProductionYear = model.ProductionYear ?? 0,
                    Director       = model.Director,
                    Description    = model.Description
                };

                movie.User = await _userManager.FindByNameAsync(User.Identity.Name);

                if (model.BannerImage != null)
                {
                    using (var stream = new MemoryStream())
                    {
                        model.BannerImage.CopyTo(stream);
                        movie.Banner = stream.ToArray();
                    }
                }

                context.Movies.Add(movie);
                await context.SaveChangesAsync();

                return(Ok());
            }
            else
            {
                ViewData["Title"]     = "Добавление нового фильма";
                ViewData["BannerBtn"] = "Добавить постер";
                ViewData["Action"]    = "Create";
                return(PartialView("_EditPartial", model));
            }
        }
예제 #13
0
        public async Task <IActionResult> Edit(MovieEditModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Movie foundMovie = await _movies.GetById(viewModel.Id);

                if (foundMovie != null)
                {
                    Director foundDirector = await _directors.GetById(viewModel.DirectorId);

                    if (foundDirector != null)
                    {
                        foundMovie.Director = foundDirector;
                    }

                    if (!string.IsNullOrEmpty(viewModel.Title))
                    {
                        foundMovie.Title = viewModel.Title;
                    }
                    if (!string.IsNullOrEmpty(viewModel.Description))
                    {
                        foundMovie.Description = viewModel.Description;
                    }
                    if (viewModel.Year > 0)
                    {
                        foundMovie.Year = viewModel.Year;
                    }
                    if (viewModel.Runtime > 0)
                    {
                        foundMovie.Runtime = viewModel.Runtime;
                    }
                    if (viewModel.Revenue > 0)
                    {
                        foundMovie.Revenue = viewModel.Revenue;
                    }

                    foreach (AssignedEntity assignedActor in viewModel.AssignedActors)
                    {
                        if (assignedActor.Assigned)
                        {
                            _movies.AddActor(foundMovie, assignedActor.Id);
                        }

                        else if (foundMovie.FilmCasts.Select(filmCast => filmCast.ActorId).ToList().Contains(assignedActor.Id))
                        {
                            _movies.RemoveActor(foundMovie, assignedActor.Id);
                        }
                    }

                    foreach (AssignedEntity assignedGenre in viewModel.AssignedGenres)
                    {
                        if (assignedGenre.Assigned)
                        {
                            _movies.AddGenre(foundMovie, assignedGenre.Id);
                        }

                        else if (foundMovie.MovieGenres.Select(movieGenre => movieGenre.GenreId).ToList().Contains(assignedGenre.Id))
                        {
                            _movies.RemoveGenre(foundMovie, assignedGenre.Id);
                        }
                    }

                    _movies.Save();
                    ModelState.Clear();
                    return(RedirectToAction($"Detail/{foundMovie.Id}"));
                }
            }
            return(View());
        }