コード例 #1
0
        /// <summary>
        /// Responsible for persisting movie information after fetching information from TMDB endpoint.
        /// </summary>
        /// <param name="movieId">The Imdb movie id for the movie needs to be persisted.</param>
        public void PersistMoive(string movieId)
        {
            IContainer continer = IocContainerSingleton.Instance.Container;

            IMovieRepository movieRepository = continer.Resolve <IMovieRepository>();

            if (movieRepository.CheckMovieExist(movieId))
            {
                return;
            }

            IActorRepository    actorRepository    = continer.Resolve <IActorRepository>();
            IDirectorRepository directorRepository = continer.Resolve <IDirectorRepository>();
            IUnitOfWork         unitOfWork         = continer.Resolve <IUnitOfWork>();

            TmdbMovieInformatonDto movieFromApi     = movieInfoFetchService.GetMovieAsync(movieId);
            TmdbMovieCastInfoDto   movieCastFromApi = movieInfoFetchService.GetMovieCreditsAsync(movieId);

            Movie movie = mapper.GetMapper().Map <Movie>(movieFromApi);

            movieRepository.PersistMovie(movie);

            foreach (var castedActor in movieCastFromApi.Cast.Take(8))
            {
                Actor actor = null;

                if (!actorRepository.CheckExistById(castedActor.Id))
                {
                    actor = mapper.GetMapper().Map <Actor>(castedActor);
                    actor.Movies.Add(movie);
                    actorRepository.PersistActor(actor);
                }
                else
                {
                    actor = actorRepository.GetActorById(castedActor.Id);
                    actor.Movies.Add(movie);
                }

                movie.Actors.Add(actor);
            }

            List <TmdbCrewDto> directorInfoFromApi = movieCastFromApi.Crew
                                                     .Where(crew => crew.Job == "Director").ToList();

            foreach (TmdbCrewDto directorDtoFromApi in directorInfoFromApi)
            {
                Director director = null;

                if (!directorRepository.CheckExistById(directorDtoFromApi.Id))
                {
                    director = mapper.GetMapper().Map <Director>(directorDtoFromApi);
                    director.Movies.Add(movie);
                    directorRepository.PersistDirector(director);
                }
                else
                {
                    director = directorRepository.GetDirectorById(directorDtoFromApi.Id);
                    director.Movies.Add(movie);
                }

                movie.Directors.Add(director);
            }

            unitOfWork.Complete();
        }