public MovieDetails GetMovieData(MovieName movieName)
        {
            String URLString = URLStringFirst + movieName.Name + URLStringSecond;   // makes the URL

            try
            {
                XDocument ob = XDocument.Load(URLString);

                var movies = ob.Root.Descendants("review")                          // Saves the data into an object
                           .Where(x => x.Element("display_title").Value.Contains(movieName.Name))
                           .Select(x => new
                           {
                               Title = x.Element("display_title").Value,
                               Author = x.Element("byline").Value,
                               Date = x.Element("publication_date").Value,
                               Review = x.Element("headline").Value,
                               Description = x.Element("capsule_review").Value,
                               Summary = x.Element("summary_short").Value
                           }).FirstOrDefault();

                if (movies == null) return null; // if no movie was found

                movieDetails = new MovieDetails(movies.Title, movies.Author, movies.Date, movies.Review, movies.Description, movies.Summary);
            }

            catch (WebException e)
            {
                throw new MovieDataServiceException("Internet connection error", e);
            }

            return movieDetails;
        }
        public void GetMovieDataTestNullName()
        {
            // Arrange
            IMovieDataService serviceTest = MovieDataServiceFactory.getMovieDataService(MovieDataServiceFactory.NEW_YORK_TIMES);
            MovieName keyWord = new MovieName("");
            MovieDetails result = new MovieDetails();
            bool _null = true;

            // Act
            result = serviceTest.GetMovieData(keyWord);
            if (result == null) _null = false;

            // Assert
            Assert.IsTrue(_null);
        }
 private NYTimes()
 {
     movieDetails = new MovieDetails();
     URLStringFirst = "http://api.nytimes.com/svc/movies/v2/reviews/search.xml?query=";
     URLStringSecond = "&api-key=cd7311c24acf548adf1f836ea1bcb115:14:72677084";
 }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            string nameString;
            char select;
            bool goOn = true;
            IMovieDataService service = MovieDataServiceFactory.getMovieDataService(MovieDataServiceFactory.NEW_YORK_TIMES);
            // The factory will decide, by the const we are sending to the function, which object to create

            Console.WriteLine("\n~~~~~~~ Movies Reviews ~~~~~~~");

            while (goOn)
            {
                MovieName keyWord = new MovieName();
                MovieDetails result = new MovieDetails();

                try
                {
                    Console.WriteLine("\nPlease enter the movie's name: ");
                    nameString = Console.ReadLine();

                    if (nameString == "")
                    {
                        throw new MovieDataServiceException("Input string can't be null, please try again\n");
                    }

                    keyWord.Name = keyWord.NameToUpper(nameString);
                    result = service.GetMovieData(keyWord);

                    if (result == null)
                    {
                        throw new MovieDataServiceException("We didn't find the movie you were searching for");
                    }

                    result.PrintDetails();  // if a movie was found, prints its details
                }

                catch (NullReferenceException e)
                {
                    throw new MovieDataServiceException("Null Reference Exception", e);
                }
                catch (MovieDataServiceException e)
                {
                    Console.WriteLine(e.ToString());
                }

                finally
                {
                    try
                    {
                        Console.WriteLine("\nDo you want to have another search?[Y/N]:");
                        select = char.Parse(Console.ReadLine());

                        if ((select == 'Y') || (select == 'y'))
                            goOn = true;
                        else goOn = false;

                    }
                    catch (FormatException e)  // when we call the NameToUpper function with no argument
                    {
                        throw new MovieDataServiceException("You didn't enter a char", e);
                    }

                }
            } // end of the while loop

            Console.WriteLine("Have a great day!\n");
        }