Request information about movie from IMDB.
using MovieNavigator.Core;
using MovieNavigator.Web.Models;
using MovieNavigator.Web.ViewModels;
using System.Collections.Generic;
using System.Web.Mvc;
namespace MovieNavigator.Web.Controllers
{
public class HomeController : Controller
{
private MovieService MovieService = new MovieService();
private readonly SearchStatisticsService SearchStatisticsService = new SearchStatisticsService();
public ActionResult Index()
{
var searchQuery = Helpers.GetUrlVarStr("find");
var pageNumber = Helpers.GetUrlVar("page");
var model = new HomeViewModel();
model.Movies = new List<MovieModel>();
model.DataPagination = new DataPagination
{
PageNumber = pageNumber
};
if (!string.IsNullOrEmpty(searchQuery))
{
model.DataPagination.BaseQueryString = $"?find={searchQuery}";
model.SearchTitle = searchQuery;
var moviesResponse = MovieService.GetMoviesList(searchQuery, pageNumber);
model.SearchResultsCount = moviesResponse.TotalResults;
if (moviesResponse.Response == "True")
{
foreach (var movie in moviesResponse.Items)
{
model.Movies.Add(new MovieModel
{
Title = movie.Title,
Year = movie.Year,
imdbID = movie.imdbID
});
}
}
else if (moviesResponse.Response == "Error")
{
model.SearchResult = moviesResponse.ResponseMessage;
}
else
{
model.SearchResult = $"By request - {searchQuery} - found 0 results.";
}
model.DataPagination.TotalItems = moviesResponse.TotalResults;
model.DataPagination.ProcessData();
// update search statistics
if (!SearchStatisticsService.IsSearchExists(searchQuery))
SearchStatisticsService.CreateSearchStatistics(searchQuery, moviesResponse.TotalResults);
else
SearchStatisticsService.UpdateSearchStatistics(searchQuery, moviesResponse.TotalResults);
}
return View(model);
}
[ChildActionOnly]
public ActionResult RecentSearch()
{
var model = new RecentSearchViewModel();
model.Items = new List<RecentSearchItem>();
foreach (var result in SearchStatisticsService.GetRecentSearchList())
{
model.Items.Add(new RecentSearchItem { SearchQuery = result.Query, FoundItems = result.FoundItems });
}
return PartialView("_RecentSearch", model);
}
[HttpPost]
public ActionResult SearchMovie()
{
return RedirectToAction("Index", new { find = Request.Form["SearchTitle"] });
}
public ActionResult About()
{
return View();
}
}
}