Exemplo n.º 1
0
        public MovieDetailsGetData GetMovieDetails(Guid movie_id)
        {
            SqlConnection       sqlconnection = sqlsetup.SqlConnectionSetup();
            MovieDetailsGetData moviedetails  = new MovieDetailsGetData();

            try
            {
                sqlconnection.Open();

                List <Task> TableFetch = new List <Task>();

                MovieData MovieDBList = new MovieData();
                MovieProducerRelationData     MovieProducerMapping = new MovieProducerRelationData();
                List <ActorData>              ActorDBList          = new List <ActorData>();
                List <ProducerData>           ProducerList         = new List <ProducerData>();
                List <ActorMovieRelationData> MovieActorMapping    = new List <ActorMovieRelationData>();

                Dictionary <Guid, ActorData>    dicActorData = new Dictionary <Guid, ActorData>();
                Dictionary <Guid, ProducerData> dicProducer  = new Dictionary <Guid, ProducerData>();



                string ImageLocation = "/src/Images/";


                TableFetch.Add(Task.Run(() => {
                    MovieDBList = sqlconnection.Query <MovieData>("SELECT * FROM IMDB.dbo.Movie WHERE movie_id = @movie_id;", new { movie_id }).FirstOrDefault();
                }));
                TableFetch.Add(Task.Run(() => {
                    ActorDBList = sqlconnection.Query <ActorData>("SELECT * FROM IMDB.dbo.Actors;").ToList();
                    if (ActorDBList?.Count > 0)
                    {
                        dicActorData = ActorDBList.ToDictionary(x => x.actor_id);
                    }
                }));
                TableFetch.Add(Task.Run(() => {
                    ProducerList = sqlconnection.Query <ProducerData>("SELECT * FROM IMDB.dbo.Producers;").ToList();
                    if (ProducerList?.Count > 0)
                    {
                        dicProducer = ProducerList.ToDictionary(x => x.producer_id);
                    }
                }));
                TableFetch.Add(Task.Run(() => {
                    MovieProducerMapping = sqlconnection.Query <MovieProducerRelationData>("SELECT * FROM IMDB.dbo.Mapping_Table_Producers_and_Movies WHERE movie_id = @movie_id;", new { movie_id }).FirstOrDefault();
                }));
                TableFetch.Add(Task.Run(() => {
                    MovieActorMapping = sqlconnection.Query <ActorMovieRelationData>("SELECT * FROM IMDB.dbo.Mapping_Table_Actors_and_movies WHERE movie_id = @movie_id;", new { movie_id }).ToList();
                }));
                Task.WaitAll(TableFetch.ToArray());

                String Format = ".jpg";


                moviedetails.MovieDetail   = MovieDBList;
                moviedetails.ImageFilePath = moviedetails.MovieDetail.Poster != null? ImageLocation + MovieDBList.Name + Format:null;

                ProducerData producerBio = null;
                dicProducer.TryGetValue(MovieProducerMapping.producer_id, out producerBio);

                if (producerBio != null)
                {
                    moviedetails.producerList = producerBio;
                }
                object obj = new object();
                moviedetails.actorList = new List <ActorData>();
                Parallel.ForEach(MovieActorMapping, x =>
                {
                    ActorData actordata = null;
                    dicActorData.TryGetValue(x.actor_id, out actordata);
                    if (actordata != null)
                    {
                        lock (obj)
                        {
                            moviedetails.actorList.Add(actordata);
                        }
                    }
                });
            }
            catch (Exception e)
            {
            }
            finally
            {
                sqlconnection.Close();
            }
            return(moviedetails);
        }
        public IHttpActionResult getMovieDetails(Guid movie_id)
        {
            MovieDetailsGetData moviedetails = moviedata.GetMovieDetails(movie_id);

            return(Ok(moviedetails));
        }