Exemple #1
0
        public async Task <IActionResult> Index(string searchString, string searchYear, int?pageNumber)
        {
            var model = new SearchResults();

            if (!string.IsNullOrWhiteSpace(searchString))
            {
                ViewData["SearchString"] = searchString;
                ViewData["SearchYear"]   = searchYear;

                TempData["SearchString"] = searchString;
                TempData["SearchYear"]   = searchYear;
                TempData["pageNumber"]   = pageNumber ?? 1;

                var searchService = new SearchService(_apiKey);

                var moviesResponse = await searchService.SearchMovies(searchString, searchYear, pageNumber ?? 1);

                if (moviesResponse.Response)
                {
                    model.Results     = moviesResponse.Search;
                    model.TotalItems  = moviesResponse.TotalResults;
                    model.TotalPages  = moviesResponse.TotalResults / 10;
                    model.CurrentPage = pageNumber ?? 1;

                    var structuredMovies = moviesResponse.Search.Select((t, i) => t.ToStructuredData(i)).ToList();

                    var structuredList = new Schema.NET.ItemList
                    {
                        ItemListElement = new Values <IListItem, string, IThing>(structuredMovies)
                    };

                    ViewBag.JsonLd = structuredList.ToString();
                }
            }

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Index(string s, string y)
        {
            string apiUrl = configuration.GetValue <string>("api:url");
            string apiKey = configuration.GetValue <string>("api:key");

            UriBuilder baseUri = new UriBuilder(apiUrl + "?apiKey=" + apiKey);

            var model = new SearchResults();

            if (s != null && s.Length > 0)
            {
                string queryToAppend = "s=" + s;
                if (baseUri.Query != null && baseUri.Query.Length > 1)
                {
                    baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
                }
                else
                {
                    baseUri.Query = queryToAppend;
                }
            }

            bool checkYear = int.TryParse(y, out int year);

            if (y != null && y.Length == 4 && checkYear)
            {
                string queryToAppend = "y=" + y;
                if (baseUri.Query != null && baseUri.Query.Length > 1)
                {
                    baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
                }
                else
                {
                    baseUri.Query = queryToAppend;
                }
            }

            if (s != null && s.Length > 0 || (y != null && y.Length == 4 && checkYear))
            {
                string url = baseUri.ToString();

                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync(url))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        JObject o = JObject.Parse(apiResponse);
                        if ((string)o["Response"] == "True")
                        {
                            JArray arrMovies          = (JArray)o["Search"];
                            IList <SearchResult> list = arrMovies.ToObject <IList <SearchResult> >();
                            model.Results = list;

                            int position = 1;
                            List <IListItem> itemElements = new List <IListItem>();
                            foreach (SearchResult sResult in list)
                            {
                                var i = new Schema.NET.ListItem()
                                {
                                    Position = position,
                                    Item     = new Schema.NET.Movie()
                                    {
                                        Name  = sResult.Title,
                                        Image = new Uri(sResult.Poster),
                                        Url   = new Uri(sResult.Poster)
                                    }
                                };
                                itemElements.Add(i);
                                position++;
                            }

                            var itemListSchema = new Schema.NET.ItemList()
                            {
                                ItemListElement = itemElements
                            };
                            ViewBag.JsonLd = itemListSchema.ToString();
                        }
                    }
                }
            }
            return(View(model));
        }