public Movie UpdateMovie(int id, UpdateMovieRequest movieRequest)
        {
            var movie = dbContext.Movies.Where(m => m.Id == id).FirstOrDefault();

            movie.EndAt = DateTime.Parse(movieRequest.EndAt);
            Coppier <UpdateMovieRequest, Movie> .Copy(movieRequest, movie);

            if (movieRequest.ScreenTypeIds != null)
            {
                var screenTypeToDelete = dbContext.MovieScreenTypes.Where(ms => ms.MovieId == id).ToList();
                if (screenTypeToDelete != null)
                {
                    dbContext.RemoveRange(screenTypeToDelete);
                }

                var screenTypes = dbContext.ScreenTypes.Where(s => movieRequest.ScreenTypeIds.Contains(s.Id)).ToList();

                foreach (var screenType in screenTypes)
                {
                    var movieScreenType = new MovieScreenType()
                    {
                        Movie      = movie,
                        ScreenType = screenType,
                    };
                    dbContext.Add(movieScreenType);
                }
            }

            dbContext.Update(movie);
            var isSuccess = Save();

            if (!isSuccess)
            {
                return(null);
            }
            return(movie);
        }
Beispiel #2
0
        public Movie CreateMovie(MovieRequest movieRequest)
        {
            MovieResponse response = MovieProxy.GetMovieByIMDB(movieRequest.Imdb);
            var           movie    = new Movie()
            {
                EndAt = DateTime.Parse(movieRequest.EndAt),
            };

            Coppier <MovieResponse, Movie> .Copy(response, movie);

            Coppier <MovieRequest, Movie> .Copy(movieRequest, movie);

            var rate = dbContext.Rates.Where(r => r.Name == response.RateName).FirstOrDefault();

            if (rate == null)
            {
                rate = new Rate()
                {
                    Name   = response.RateName,
                    MinAge = 0,
                };
                dbContext.Add(rate);
            }
            movie.Rate = rate;

            var screenTypes = dbContext.ScreenTypes.Where(s => movieRequest.ScreenTypeIds.Contains(s.Id)).ToList();

            List <string> actorNames = new List <string>();

            foreach (var actor in response.Actors)
            {
                actorNames.Add(actor.Name);
            }

            var actors = dbContext.Actors.Where(a => actorNames.Contains(a.Name)).ToList();


            var isNotExistActors = response.Actors.Where(a => !actors.Any(a2 => a2.Name == a.Name)).ToList();

            if (isNotExistActors != null)
            {
                foreach (var actor in isNotExistActors)
                {
                    dbContext.Add(actor);
                }
            }

            //genre
            List <string> genreNames = new List <string>();

            foreach (var genre in response.Genres)
            {
                genreNames.Add(genre.Name);
            }

            var genres = dbContext.Genres.Where(a => genreNames.Contains(a.Name)).ToList();


            var isNotExistGenres = response.Genres.Where(a => !genres.Any(a2 => a2.Name == a.Name)).ToList();

            if (isNotExistGenres != null)
            {
                foreach (var genre in isNotExistGenres)
                {
                    dbContext.Add(genre);
                }
            }

            foreach (var screenType in screenTypes)
            {
                var movieScreenType = new MovieScreenType()
                {
                    Movie      = movie,
                    ScreenType = screenType,
                };
                dbContext.Add(movieScreenType);
            }

            foreach (var actor in actors.Concat(isNotExistActors))
            {
                var movieActor = new MovieActor()
                {
                    Movie = movie,
                    Actor = actor,
                };
                dbContext.Add(movieActor);
            }

            foreach (var genre in genres.Concat(isNotExistGenres))
            {
                var movieGenre = new MovieGenre()
                {
                    Movie = movie,
                    Genre = genre,
                };
                dbContext.Add(movieGenre);
            }

            dbContext.Add(movie);
            var isSuccess = Save();

            if (!isSuccess)
            {
                return(null);
            }
            return(movie);
        }