using System; using System.Configuration; using System.Net; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.UI; using AutoMapper; using DR.Common.RESTClient; using DR.FV2016.Service; using DR.FV2016.Service.Helpers; using DR.FV2016.Service.Interfaces; using DR.FV2016.Web.Helpers; using DR.FV2016.Web.Models; namespace DR.FV2016.Web.Controllers { public class CustomArticlesController : Controller { private readonly IArticleService _articleService; private readonly IHtmlResourceService _htmlResourceService; private readonly IRESTClient _restClient; private static readonly string EmbedUrl = ConfigurationManager.AppSettings["meningsmaaling-embed-url"]; private static readonly string EmbedUrlCandidateMovement = ConfigurationManager.AppSettings["vaelgervandring-embed-url"]; private readonly IConstituencyService _constituencyService; public CustomArticlesController(IArticleService articleService, IHtmlResourceService htmlResourceService, IRESTClient restClient, IConstituencyService constituencyService) { if (constituencyService == null) throw new ArgumentNullException("constituencyService", "constituencyService may not be null."); _constituencyService = constituencyService; _articleService = articleService; _htmlResourceService = htmlResourceService; _restClient = restClient; } // // GET: /CreateGovernment/ id=9mEVSD5MaK in query [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult CreateGovernment(string id) { var model = Mapper.Map(_articleService.GetWebCmsArticle("/min-regering")); ViewBag.Id = id; model.PageTitle = "Min Regering"; model.IsResponsive = true; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:min-regering"); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult CreateGovernmentMobile(string id) { var model = Mapper.Map(_articleService.GetDefaultArticleWrapper()); ViewBag.Id = id; model.PageTitle = "Min Regering"; model.IsResponsive = true; ViewBag.TopBar = _htmlResourceService.GetHtml(new Uri("http://www.dr.dk/drdktopbar/navigation/top")); ViewBag.Footer = _htmlResourceService.GetHtml(new Uri("http://www.dr.dk/drdktopbar/navigation/footer")); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult Meningsmaalinger() { var model = Mapper.Map(_articleService.GetWebCmsArticle("/meningsmaalinger")); model.PageTitle = "Meningsmåling"; ViewBag.Content = _htmlResourceService.GetHtml(new Uri(EmbedUrl)); model.IsResponsive = true; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:meningsmaalinger"); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult Vaelgervandringer() { var model = Mapper.Map(_articleService.GetWebCmsArticle("/vaelgervandringer")); model.PageTitle = "Vælgervandringer"; ViewBag.Content = _htmlResourceService.GetHtml(new Uri(EmbedUrlCandidateMovement)); model.IsResponsive = true; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:vaelgervandringer"); return View(model); } private bool ShowResultPages() { var now = TimeHelper.Now(); DateTime testStartTime; DateTime testEndTime; if (Request.Url != null && Request.Url.ToString().Contains("resultat-test")) { return DateTime.TryParse(ConfigurationManager.AppSettings["ResultTestStart"], out testStartTime) && now >= testStartTime && DateTime.TryParse(ConfigurationManager.AppSettings["ResultTestEnd"], out testEndTime) && now <= testEndTime; } else { return TimeHelper.ElectionDayHasStarted(); } } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 120)] public ActionResult ResultPage() { if (!ShowResultPages()) { return HttpNotFound(); } var model = Mapper.Map(_articleService.GetWebCmsArticle("/resultat")); model.PageTitle = "Resultat"; model.GreaterConstituencies = ConstituencyHelper.AllConstituencies(Mapper.Engine, _constituencyService); model.IsResponsive = true; model.CanonicalLink = string.Format("http://www.dr.dk{0}/{1}", HttpRuntime.AppDomainAppVirtualPath, ((Route)(RouteTable.Routes["ResultPage"])).Url); model.Page = "resultat"; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:alleresultater"); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 120)] public ActionResult ElectedPage() { if (!ShowResultPages()) { return HttpNotFound(); } var model = Mapper.Map(_articleService.GetWebCmsArticle("/resultat/valgte")); model.PageTitle = "Hvem er valgt"; model.GreaterConstituencies = ConstituencyHelper.AllConstituencies(Mapper.Engine, _constituencyService); model.IsResponsive = true; model.CanonicalLink = string.Format("http://www.dr.dk{0}/{1}", HttpRuntime.AppDomainAppVirtualPath, ((Route)(RouteTable.Routes["ElectedPage"])).Url); model.Page = "valgte"; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:valgt"); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 120)] public ActionResult ResultHistory() { if (!ShowResultPages()) { return HttpNotFound(); } var model = Mapper.Map(_articleService.GetDefaultArticleWrapper()); model.PageTitle = "Historisk Udvikling"; model.IsResponsive = true; model.CanonicalLink = string.Format("http://www.dr.dk{0}/{1}", HttpRuntime.AppDomainAppVirtualPath, ((Route)(RouteTable.Routes["ResultHistory"])).Url); model.Page = "historik"; SearchableHelper.EnsureUrn(model, "urn:dr:valg2015:historiskudvikling"); return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult Poster(string id) { var articleViewModel = Mapper.Map(_articleService.GetWebCmsArticle("/plakat")); articleViewModel.PageTitle = "Plakatgenerator"; //Fix canonical url var baseUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUrl"]; articleViewModel.CanonicalLink = baseUrl + "plakat" + (string.IsNullOrWhiteSpace(id) ? "" : "?id=" + id); articleViewModel.PosterUploadUrl = System.Configuration.ConfigurationManager.AppSettings["PosterGeneratorUrl"] + "uploadposter"; //Get posterinfo if (!string.IsNullOrWhiteSpace(id)) { var url = ""; try { url = System.Configuration.ConfigurationManager.AppSettings["PosterGeneratorUrl"] + "posterinfo/" + id; var result = _restClient.Get(url); if (result != null) { articleViewModel.Poster = result; } } catch (RESTClientException ex) { articleViewModel.Poster = new Poster(); if (ex.StatusCode == HttpStatusCode.NotFound) articleViewModel.Poster.ErrorMessage = "Could not find posterinfo for id " + id + ". Please make sure the id is valid. url=" + url; else articleViewModel.Poster.ErrorMessage = "Failed to load posterinfo: " + ex.StatusCode + " url=" +url ; } } SearchableHelper.EnsureUrn(articleViewModel, "urn:dr:valg2015:plakatgenerator"); return View(articleViewModel); } } }