using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web.Mvc; using System.Web.UI; using AutoMapper; using DR.AcCommonUI.Helpers.LivePage; using DR.FV2016.Service.Helpers; using DR.FV2016.Service.Interfaces; using DR.FV2016.Service.Models; using DR.FV2016.Web.Models; using DR.FV2016.Web.Models.Candidate; using DR.WCMS.Web.Mvc.IO.Contract; using DR.Web.UI.Model; using DRCMSRedesign.Contracts; namespace DR.FV2016.Web.Controllers { public class ArticleController : Controller { private readonly IArticleService _articleService; private readonly ILivePageService _livePageService; private readonly IPartialArticleWrapperService _partialArticleWrapperService; private readonly ICompositeCandidateService _compositeCandidateService; public ArticleController(IArticleService articleService, IPartialArticleWrapperService partialArticleWrapperService, ILivePageService livePageService, ICompositeCandidateService compositeCandidateService) { _articleService = articleService; _livePageService = livePageService; _partialArticleWrapperService = partialArticleWrapperService; _compositeCandidateService = compositeCandidateService; } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 120)] public ActionResult Index(ArticleWrapper articleWrapper) { var model = Mapper.Map(articleWrapper); //Candidates in article var candidatesInArticle = new List(); var candidateRegExMatches = new List(); var regex = new Regex(@"\/kandidater\/(?.*?)\/(?[0-9]*)"); foreach (var paragraphElement in articleWrapper.Body.OfType()) { var matches = regex.Matches(paragraphElement.Text); candidateRegExMatches.AddRange(matches.Cast().Select(m => m.Groups["id"].Value)); } foreach (var candidateId in candidateRegExMatches) { int id; if (int.TryParse(candidateId, out id)) { BasicCandidate candidate = _compositeCandidateService.GetCandidate(id); candidatesInArticle.Add(Mapper.Map(candidate)); } } model.CandidatesInArticle = new CandidateArticleList { Items = candidatesInArticle }; return View(model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public ActionResult Video(ArticleWrapper articleWrapper) { var articleViewModel = Mapper.Map(articleWrapper); //try //{ // articleViewModel.DebugOutputString = JsonConvert.SerializeObject(articleViewModel); //} //catch //{ //} RemoveSelfInList(articleViewModel.RelatedVideoArticleList1, articleViewModel.PostingGuid, 4); RemoveSelfInList(articleViewModel.RelatedVideoArticleList2, articleViewModel.PostingGuid, 4); return View(articleViewModel); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public ActionResult Live() { var articleWrapper = Mapper.Map(_articleService.GetWebCmsArticle("/live")); var model = Mapper.Map(articleWrapper); model.PostingGuid = model.PostingGuid; model.LivePageViewModel = LivePageHelper.GetLivePageViewModel(model, Mapper.Map>(articleWrapper.Images), _livePageService); //model.LivePageViewModel.RelatedArticles = model.RelatedArticles; model.IsResponsive = true; if(TimeHelper.ElectionDayHasStarted() || Request.QueryString["test"] == "true") // this is ok in DRxml { model.Article.isBackdrop = true; return View("live-valg", model); } return View(model); } private void RemoveSelfInList(ArticleList list, string postingGuid, int count) { var items = list.Items.ToList(); if (list.Items.Any(i => i.PostingGuid == postingGuid)) { items.Remove(list.Items.First(i => i.PostingGuid == postingGuid)); } list.Items = items.Take(count); } } }