示例#1
0
        public async Task <IActionResult> Create([Bind("FilmId,PersonId")] MovieActor movieActor)
        {
            if (ModelState.IsValid)
            {
                _context.Add(movieActor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["FilmId"]   = new SelectList(_context.Movies, "Id", "Summary", movieActor.FilmId);
            ViewData["PersonId"] = new SelectList(_context.Persons, "Id", "Biography", movieActor.PersonId);
            return(View(movieActor));
        }
示例#2
0
        public ActionResult Create([Bind(Include = "MovieId,ActorId")] MovieActor movieActor)
        {
            if (ModelState.IsValid)
            {
                db.MovieActor.Add(movieActor);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ActorId = new SelectList(db.Actor, "ActorId", "ActorName", movieActor.ActorId);
            ViewBag.MovieId = new SelectList(db.Movie, "MovieId", "MovieTitle", movieActor.MovieId);
            return(View(movieActor));
        }
示例#3
0
        public async Task <bool> AddMovieActorAsync(MovieActor movieActor)
        {
            var movActor = new MovieActor
            {
                MovieId = movieActor.MovieId,
                ActorId = movieActor.ActorId
            };

            _db.MovieActors.Add(movActor);
            await _db.SaveChangesAsync();

            return(true);
        }
示例#4
0
        public IActionResult PostMovie([FromBody] MovieCreationDto movieCreationDto)
        {
            Movie movie = new Movie();

            movie.Name        = movieCreationDto.Name;
            movie.ReleaseYear = movieCreationDto.ReleaseYear;
            movie.Runtime     = movieCreationDto.Runtime;
            movie.Summary     = movieCreationDto.Summary;
            movie.ImageUrl    = movieCreationDto.ImageUrl;

            Movie createdMovie = movieDbRepository.AddMovie(movie);

            try
            {
                if (movieCreationDto.Genres != null)
                {
                    foreach (string id in movieCreationDto.Genres.Split(','))
                    {
                        MovieGenre movieGenre = new MovieGenre()
                        {
                            GenreId = int.Parse(id), MovieId = createdMovie.MovieId
                        };
                        movieDbRepository.AddGenreToMovie(movieGenre);
                    }
                }
            }
            catch (Exception)
            {
            }

            try
            {
                if (movieCreationDto.Actors != null)
                {
                    foreach (string id in movieCreationDto.Actors.Split(','))
                    {
                        MovieActor movieActor = new MovieActor()
                        {
                            ActorId = int.Parse(id), MovieId = createdMovie.MovieId
                        };
                        movieDbRepository.AddActorToMovie(movieActor);
                    }
                }
            }
            catch (Exception)
            {
            }


            return(Ok());
        }
示例#5
0
        public IHttpActionResult DeleteMovieActor(int id)
        {
            MovieActor movieActor = db.MovieActors.Find(id);

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

            db.MovieActors.Remove(movieActor);
            db.SaveChanges();

            return(Ok(movieActor));
        }
        public async Task <MovieActorDTO> DeleteMovieActor(int id, MovieActorsDeleteRequest request)
        {
            foreach (Actor actor in request.DeletingActors)
            {
                var movieActor = await _context.MovieActor.Where(el => el.ActorId == actor.Id && el.MovieId == id).FirstOrDefaultAsync();

                _context.MovieActor.Remove(movieActor);
            }
            await _context.SaveChangesAsync();

            MovieActor lastReturningValue = new MovieActor(request.DeletingActors[request.DeletingActors.Count - 1].Id, id);

            return(Mapper.Map <MovieActor, MovieActorDTO>(lastReturningValue));
        }
示例#7
0
        /// <summary>
        /// Method for recording movies from OMDb API in the DB async.
        /// </summary>
        /// <param name="movieIds">Movie Ids.</param>
        public async Task UploadAsync(string[] movieIds)
        {
            OMDbService omdbService = new OMDbService(configuration.GetValue <string>("OMDbApiKey"));

            using (var transaction = await context.Database.BeginTransactionAsync())
            {
                foreach (var movieId in movieIds)
                {
                    if (!context.Movies.Any(m => m.ImdbId == movieId))
                    {
                        Movie movie = await omdbService.GetMovieFromJsonAsync(movieId);

                        await context.Movies.AddAsync(movie);

                        await context.SaveChangesAsync();
                    }
                    else
                    {
                        transaction.Rollback();
                    }

                    foreach (var actorName in await omdbService.GetMovieActorsNamesFromJsonAsync(movieId))
                    {
                        var actor = context.Actors.FirstOrDefault(a => a.Name == actorName);

                        if (actor == null)
                        {
                            actor = new Actor
                            {
                                Name = actorName.TrimStart(' ')
                            };

                            await context.Actors.AddAsync(actor);
                        }

                        var movieActor = new MovieActor
                        {
                            ActorId = actor.Id,
                            MovieId = context.Movies.Last().Id
                        };

                        await context.MovieActors.AddAsync(movieActor);
                    }

                    await context.SaveChangesAsync();

                    transaction.Commit();
                }
            }
        }
示例#8
0
        // GET: MovieActors/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            MovieActor movieActor = db.MovieActors.Find(id);

            if (movieActor == null)
            {
                return(HttpNotFound());
            }
            return(View(movieActor));
        }
示例#9
0
        public async Task <IActionResult> EditMovieActor(MovieActor movieActor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var result = await _repo.EditMovieActorAsync(movieActor);

            if (result)
            {
                return(Ok());
            }
            return(BadRequest());
        }
示例#10
0
        public bool CreateActor(Actor actor, List <int> movieIds)
        {
            var movies = _db.Movies.Where(m => movieIds.Contains(m.Id)).ToList();

            foreach (var movie in movies)
            {
                var movieActor = new MovieActor()
                {
                    Actor = actor,
                    Movie = movie
                };
                _db.Add(movieActor);
            }
            _db.Actors.Add(actor);
            return(Save());
        }
示例#11
0
        // GET: MovieActors/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            MovieActor movieActor = db.MovieActors.Find(id);

            if (movieActor == null)
            {
                return(HttpNotFound());
            }
            ViewBag.MovieId = new SelectList(db.Movies, "MovieId", "Name", movieActor.MovieId);
            ViewBag.ActorId = new SelectList(db.Actors, "ActorId", "Name", movieActor.ActorId);
            return(View(movieActor));
        }
示例#12
0
        public async Task <ActionResult> AddActorOnMovie(int id)
        {
            var movieActor = new MovieActor()
            {
                MovieId = id
            };

            var actors = (await _actorService.GetActors()).Select(x => new SelectListItem()
            {
                Text = x.Name, Value = x.Id.ToString()
            }).ToList();

            ViewBag.ActorOptions = actors;

            return(View(movieActor));
        }
示例#13
0
        public IActionResult Post(int id, [FromBody] Actor actor) // id is the movieId
        {
            _db.Actors.Add(actor);
            _db.SaveChanges();

            var movieActorToCreate = new MovieActor
            {
                MovieId = id,
                ActorId = actor.Id
            };

            _db.MovieActors.Add(movieActorToCreate);
            _db.SaveChanges();

            return(Ok());
        }
示例#14
0
        public bool CreateMovieModel([FromBody] MovieModel movie, [FromQuery] List <int> actorIds)
        {
            var actors = _db.Actors.Where(a => actorIds.Contains(a.Id)).ToList();

            foreach (var actor in actors)
            {
                var movieActor = new MovieActor()
                {
                    Actor = actor,
                    Movie = movie
                };
                _db.Add(movieActor);
            }
            _db.Movies.Add(movie);
            return(Save());
        }
示例#15
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            MovieActor = await context.MovieActors.FindAsync(id);

            if (MovieActor != null)
            {
                context.MovieActors.Remove(MovieActor);
                await context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
示例#16
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            MovieActor = await context.MovieActors.Include(m => m.Actor)
                         .Include(m => m.Movie)
                         .FirstOrDefaultAsync(m => m.MovieId == id);

            if (MovieActor == null)
            {
                return(NotFound());
            }
            return(Page());
        }
示例#17
0
        public MovieDto UpdateActors(string movieId, List <string> actorsIds)
        {
            var movieFromDb = db.Movies.Find(movieId);

            //Check if movie Id exists
            if (movieFromDb == null)
            {
                return(null);
            }

            //Verify if all actors exists and build the actors to insert
            var movieActorsToInsert = new List <MovieActor>();

            foreach (var actorId in actorsIds)
            {
                var actor = db.Actors.Find(actorId);
                if (actor == null)
                {
                    //If an actor does not exist, return
                    return(null);
                }
                var movieActor = new MovieActor()
                {
                    MovieId = movieFromDb.Id,
                    ActorId = actor.Id
                };
                movieActorsToInsert.Add(movieActor);
            }

            //Delete all records in MovieActors for this movieId
            var movieActorsToRemove = db.MovieActors.Where(movieActor => movieActor.MovieId == movieId).ToList();

            db.MovieActors.RemoveRange(movieActorsToRemove);
            db.SaveChanges();

            //Save the new relationships
            db.MovieActors.AddRange(movieActorsToInsert);
            db.SaveChanges();

            //Evict this entry from cache
            memoryCache.Remove(MemoryCacheKeyGenerator.Generate(MemoryCacheKey.MOVIE_BY_ID, movieId));

            //Return a refreshed instance of Movie
            return(GetMovie(movieId));
        }
示例#18
0
        public async Task <bool> EditMovieActorAsync(MovieActor movieActor)
        {
            var mov = await _db.MovieActors.FirstOrDefaultAsync(x => x.Id == movieActor.Id);

            if (mov == null)
            {
                return(false);
            }

            _db.Attach(mov);
            mov.ActorId = movieActor.ActorId;
            mov.MovieId = movieActor.MovieId;
            _db.Entry(mov).Property(x => x.MovieId).IsModified = true;
            _db.Entry(mov).Property(x => x.ActorId).IsModified = true;
            await _db.SaveChangesAsync();

            return(true);
        }
示例#19
0
        public async Task <ActionResult <AddMovieResponse> > PostMovie(AddMovieRequest addMovieRequest)
        {
            Movie movie = MovieMapper.mapFormAddMovieRequestToMovie(addMovieRequest);

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

            foreach (var actor in addMovieRequest.Actors)
            {
                MovieActor movieActor = new MovieActor();
                movieActor.ActorId = actor.Id;
                movieActor.MovieId = movie.Id;
                _context.MovieActors.Add(movieActor);
            }
            await _context.SaveChangesAsync();

            return(Ok(MovieMapper.mapFromMovieToAddMovieResponse(movie)));
        }
示例#20
0
        public ActionResult Create([Bind(Include = "Name,HitMovie,ActorIds")] Movie model, string[] ActorIds)
        {
            if (ModelState.IsValid)
            {
                Movie checkmodel = db.Movies.SingleOrDefault(x => x.Name == model.Name && x.HitMovie == model.HitMovie);

                if (checkmodel == null)
                {
                    //model.MovieId = Guid.NewGuid().ToString();
                    //model.CreateDate = DateTime.Now;
                    //model.EditDate = model.CreateDate;
                    db.Movies.Add(model);
                    db.SaveChanges();

                    if (ActorIds != null)
                    {
                        foreach (string actorId in ActorIds)
                        {
                            MovieActor movieActor = new MovieActor();

                            //movieActor.MovieActorId = Guid.NewGuid().ToString();
                            //movieActor.CreateDate = DateTime.Now;
                            //movieActor.EditDate = movieActor.CreateDate;

                            movieActor.MovieId = model.MovieId;
                            movieActor.ActorId = actorId;
                            model.Actors.Add(movieActor);
                        }
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Duplicated Movie detected.");
                }
            }

            ViewBag.Actors = new MultiSelectList(db.Actors.ToList(), "ActorId", "Name", ActorIds);

            return(View(model));
        }
示例#21
0
 public void TestAddMovieActor()
 {
     using (movieContext = new MovieContext())
     {
         int   countMovieActore = movieContext.MoviesActors.Count();
         Movie movie            = new Movie("AAAA", 2000, 133, "US", 1, "ASDFD");
         bc.Add(movie);
         Actor actor = new Actor("Zak", "Efron", "male");
         bc.Add(actor);
         MovieActor movieActor = new MovieActor(movie.Id, actor.Id);
         bc.Add(movieActor);
         int countMovieActore1 = movieContext.MoviesActors.Count();
         movieContext.Movies.Remove(movie);
         movieContext.Actors.Remove(actor);
         movieContext.MoviesActors.Remove(movieActor);
         movieContext.SaveChanges();
         Assert.AreNotEqual(countMovieActore, countMovieActore1, "Not added value in MovieActor");
     }
 }
示例#22
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            MovieActor = await context.MovieActors.Include(m => m.Actor)
                         .Include(m => m.Movie)
                         .FirstOrDefaultAsync(m => m.MovieId == id);

            if (MovieActor == null)
            {
                return(NotFound());
            }
            ViewData["ActorId"] = new SelectList(context.Actors, "Id", "FirstName");
            ViewData["MovieId"] = new SelectList(context.Movies, "Id", "Description");
            return(Page());
        }
示例#23
0
        public void EditMovies(string movies, int actorId)                                                                          // ADD or REMOVE movies from actor
        {
            var oldMovies = _context.MovieActor.Include(ma => ma.Movie).Where(ma => ma.ActorId == actorId).ToList();                // Get the actors related to movie
            var actor     = _context.Actor.Include(m => m.MovieActor).Where(a => a.Id == actorId).FirstOrDefault();
            var newMovies = movies.Split(",");                                                                                      // Split the selected movies to array

            foreach (string movie in newMovies)                                                                                     // Check if there is any new movies to add the actor
            {
                if (!oldMovies.Any(mg => mg.Movie.Name == movie))                                                                   // Check if the selected actors already connected to the movie
                {
                    var        newMovie   = _context.Movie.Include(g => g.MovieActor).Where(m => m.Name == movie).FirstOrDefault(); // Find the missing actor and add to a list
                    MovieActor movieGenre = new MovieActor()
                    {
                        MovieId = newMovie.Id,
                        Movie   = newMovie,
                        ActorId = actor.Id,
                        Actor   = actor,
                    };
                    if (newMovie.MovieActor == null)
                    {
                        newMovie.MovieActor = new List <MovieActor>();
                    }
                    newMovie.MovieActor.Add(movieGenre);
                    if (actor.MovieActor == null)
                    {
                        actor.MovieActor = new List <MovieActor>();
                    }
                    actor.MovieActor.Add(movieGenre);
                    _context.Add(movieGenre);
                }
            }
            foreach (MovieActor movie in oldMovies)   // Check if there is any old actors to removed from the movie actors
            {
                if (!newMovies.Any(mg => mg == movie.Movie.Name))
                {
                    var removedMovie = _context.Movie.Include(g => g.MovieActor).Where(g => g.Name == movie.Movie.Name).FirstOrDefault();     // Find the missing actor and add to a list
                    removedMovie.MovieActor.Remove(movie);
                    actor.MovieActor.Remove(movie);
                    _context.MovieActor.Remove(movie);
                }
            }
            _context.SaveChanges();
        }
        public async Task <MovieViewModel> ManageActorAsync(string movieName, string actorName)
        {
            var movie = await this.context.Movies
                        .Include(m => m.MovieActor)
                        .ThenInclude(a => a.Actor)
                        .FirstOrDefaultAsync(m => m.Name == movieName);

            if (movie == null)
            {
                throw new EntityInvalidException($"Movie with title `{movieName}` has not been found!");
            }

            var actor = await this.context.Actors.FirstOrDefaultAsync(a => a.Name == actorName);

            if (actor == null)
            {
                throw new EntityInvalidException($"Actor with name `{actorName}` has not been found!");
            }

            // Checks whether this actor is already assigned to this movie or not.
            var isActorAlreadyAssigned = movie.MovieActor.Any(x => x.Actor == actor && x.Movie == movie);

            if (isActorAlreadyAssigned)
            {
                MovieActor actorToRemove = movie.MovieActor.FirstOrDefault(x => x.Actor?.Name == actorName);
                movie.MovieActor.Remove(actorToRemove);
            }
            else
            {
                MovieActor movieActor = new MovieActor()
                {
                    ActorId = actor.Id,
                };
                movie.MovieActor.Add(movieActor);
            }

            await this.context.SaveChangesAsync();

            var returnMovie = this.mappingProvider.MapTo <MovieViewModel>(movie);

            return(returnMovie);
        }
        private static Collection <MovieActor> Read(SqlDataReader reader)
        {
            Collection <MovieActor> movieActors = new Collection <MovieActor>();

            while (reader.Read())
            {
                int        i          = -1;
                MovieActor movieActor = new MovieActor();
                movieActor.Movie_id = reader.GetInt32(++i);
                movieActor.Actor_id = reader.GetInt32(++i);
                movieActor.Role     = reader.GetString(++i);
                if (!reader.IsDBNull(++i))
                {
                    movieActor.Fee = reader.GetInt32(i);
                }

                movieActors.Add(movieActor);
            }
            return(movieActors);
        }
示例#26
0
        public void EditActors(string actors, int movieId)                                                                          // Store the Movie Genres suggestions for Guest
        {
            var oldActors = _context.MovieActor.Include(ma => ma.Actor).Where(ma => ma.MovieId == movieId).ToList();                // Get the actors related to movie
            var movie     = _context.Movie.Include(m => m.MovieGenre).Where(m => m.Id == movieId).FirstOrDefault();
            var newActors = actors.Split(",");                                                                                      // Split the selected genres to array

            foreach (string actor in newActors)                                                                                     // Check if there is any new actors to add the movie
            {
                if (!oldActors.Any(mg => mg.Actor.Name == actor))                                                                   // Check if the selected actors already connected to the movie
                {
                    var        newactor   = _context.Actor.Include(g => g.MovieActor).Where(g => g.Name == actor).FirstOrDefault(); // Find the missing actor and add to a list
                    MovieActor movieActor = new MovieActor();
                    movieActor.ActorId = newactor.Id;
                    movieActor.Actor   = newactor;
                    movieActor.MovieId = movie.Id;
                    movieActor.Movie   = movie;
                    if (newactor.MovieActor == null)
                    {
                        newactor.MovieActor = new List <MovieActor>();
                    }
                    newactor.MovieActor.Add(movieActor);
                    if (movie.MovieActor == null)
                    {
                        movie.MovieActor = new List <MovieActor>();
                    }
                    movie.MovieActor.Add(movieActor);
                    _context.Add(movieActor);
                }
            }
            foreach (MovieActor actor in oldActors)   // Check if there is any old actors to removed from the movie actors
            {
                if (!newActors.Any(mg => mg == actor.Actor.Name))
                {
                    var removedActor = _context.Actor.Include(g => g.MovieActor).Where(g => g.Name == actor.Actor.Name).FirstOrDefault();     // Find the missing actor and add to a list
                    removedActor.MovieActor.Remove(actor);
                    movie.MovieActor.Remove(actor);
                    _context.MovieActor.Remove(actor);
                }
            }
            _context.SaveChanges();
        }
        public async Task <Unit> Handle(UpdateMovieCommand request, CancellationToken cancellationToken)
        {
            var movie = await _context.Movies
                        .Include(x => x.MovieActors)
                        .SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (movie == null)
            {
                throw new NotFoundException(nameof(Movie), request.Id);
            }

            movie.Title      = request.Title;
            movie.Year       = request.Year;
            movie.DirectorId = request.DirectorId;

            foreach (var movieActor in movie.MovieActors.ToList())
            {
                if (!request.ActorIds.Contains(movieActor.ActorId))
                {
                    movie.MovieActors.Remove(movieActor);
                }
            }

            foreach (var actorId in request.ActorIds)
            {
                if (!movie.MovieActors.Any(r => r.ActorId == actorId))
                {
                    var newMovieActor = new MovieActor {
                        ActorId = actorId
                    };
                    _context.MovieActors.Attach(newMovieActor);
                    movie.MovieActors.Add(newMovieActor);
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            //todo: could also publish a mediatr event for "things" that need to respond to this update?? Need an example

            return(Unit.Value);
        }
示例#28
0
        public MovieActor Put(int id, MovieActorDTO value)
        {
            MovieActor model = IMovieActorRepository.Get(id);

            if (value.MovieId != 0)
            {
                model.MovieId = value.MovieId;
            }

            if (value.ActorId != 0)
            {
                model.ActorId = value.ActorId;
            }

            if (value.CharacterName != null)
            {
                model.CharacterName = value.CharacterName;
            }

            return(IMovieActorRepository.Update(model));
        }
示例#29
0
        public async Task <MovieActor> AddActorAsync(Guid movieId, Guid actorId)
        {
            var movie = await GetAsync(movieId);

            var actor = await _actorStore.GetAsync(actorId);

            if (movie == null || actor == null)
            {
                return(null);
            }

            var relation = new MovieActor(movieId, actorId);

            actor.PlayedMovies.Add(relation);
            movie.Actors.Add(relation);

            Save();
            _actorStore.Save();

            return(relation);
        }
        public void AddActorToMovie(int movieId, ActorDTO actor)
        {
            Actor dbActor = new Actor()
            {
                Id        = actor.Id,
                FirstName = actor.FirstName,
                LastName  = actor.LastName
            };

            _aRepo.AddActor(dbActor);

            MovieActor dbMa = new MovieActor
            {
                ActorId = dbActor.Id,
                MovieId = movieId
                          //MovieId = _mRepo.GetMovieById(movieId).First().Id
            };


            _maRepo.AddNewMovieActor(dbMa);
        }