using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.UI; using AutoMapper; using DR.FV2016.Service.Interfaces; using DR.FV2016.Service.Models; using DR.FV2016.Web.Helpers; using DR.FV2016.Web.Models.Candidate; using DR.FV2016.Web.Models.CandidateList; namespace DR.FV2016.Web.Controllers { public class CandidateListController : Controller { private readonly ICompositeCandidateService _compositeCandidateService; private readonly IPartyService _partyService; private readonly IArticleService _articleService; public CandidateListController(ICompositeCandidateService compositeCandidateService, IPartyService partyService, IArticleService articleService) { if (compositeCandidateService == null) { throw new ArgumentNullException("compositeCandidateService"); } if (partyService == null) { throw new ArgumentNullException("partyService"); } if (articleService == null) { throw new ArgumentException("articleService"); } _compositeCandidateService = compositeCandidateService; _partyService = partyService; _articleService = articleService; } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 180)] public ActionResult Index(string constituencySlug) { if (string.IsNullOrEmpty(constituencySlug)) throw new ArgumentNullException("constituencySlug", "constituencySlug cannot be null or empty!!!"); var viewModel = Mapper.Map(_articleService.GetWebCmsArticle("/se-din-stemmeseddel")); viewModel.CanonicalLink += "/" + constituencySlug; var parties = Mapper.Map>(_partyService.GetParties().OrderBy(p => p.Letter)).ToList(); //Find out if outside parties party is in list index 0 and move to end int n; var orderedParties = new List(); if (int.TryParse(parties[0].Letter, out n)) { var outsideParties = parties[0]; parties.RemoveAt(0); orderedParties = parties.OrderBy(p => p.Letter).ToList(); orderedParties.Add(outsideParties); } try { foreach (var party in orderedParties) { var mappedParty = Mapper.Map(party); mappedParty.Items = Mapper.Map>(_compositeCandidateService.GetCandidatesByConstituency(constituencySlug, party.Id)); if (!mappedParty.Items.Any()) continue; viewModel.PartiesWithCandidates.Add(mappedParty); } viewModel.IsResponsive = true; SearchableHelper.EnsureUrn(viewModel, "urn:dr:valg2015:se-din-stemmeseddel:" + constituencySlug); viewModel.CanonicalLink = string.Format("http://www.dr.dk{0}/{1}/{2}", HttpRuntime.AppDomainAppVirtualPath,((Route) (RouteTable.Routes["Se din stemmeseddel"])).Url, constituencySlug); return View(viewModel); } catch (KeyNotFoundException) { return new HttpNotFoundResult(); } } } }