From bd4b3baff5c652658557a241acefa837f6b3b440 Mon Sep 17 00:00:00 2001 From: Thomas Hounsell Date: Sun, 21 Aug 2016 15:17:56 +0100 Subject: [PATCH] File Renaming; Fix #28 (Output Caching) --- BuildFeed.Model/BuildFeed.Model.csproj | 10 +++---- ...uild-Group.cs => BuildRepository-Group.cs} | 0 .../{Build-Lab.cs => BuildRepository-Lab.cs} | 0 ...ld-Source.cs => BuildRepository-Source.cs} | 0 ...-Version.cs => BuildRepository-Version.cs} | 0 ...{Build-Year.cs => BuildRepository-Year.cs} | 0 BuildFeed/Code/Options/Theme.cs | 15 +++++++++++ BuildFeed/Controllers/BaseController.cs | 9 +------ BuildFeed/Controllers/frontController.cs | 26 +++++++++---------- BuildFeed/Global.asax.cs | 26 ++++++++++++++----- 10 files changed, 54 insertions(+), 32 deletions(-) rename BuildFeed.Model/{Build-Group.cs => BuildRepository-Group.cs} (100%) rename BuildFeed.Model/{Build-Lab.cs => BuildRepository-Lab.cs} (100%) rename BuildFeed.Model/{Build-Source.cs => BuildRepository-Source.cs} (100%) rename BuildFeed.Model/{Build-Version.cs => BuildRepository-Version.cs} (100%) rename BuildFeed.Model/{Build-Year.cs => BuildRepository-Year.cs} (100%) diff --git a/BuildFeed.Model/BuildFeed.Model.csproj b/BuildFeed.Model/BuildFeed.Model.csproj index db04956..48f59ce 100644 --- a/BuildFeed.Model/BuildFeed.Model.csproj +++ b/BuildFeed.Model/BuildFeed.Model.csproj @@ -90,11 +90,11 @@ - - - - - + + + + + diff --git a/BuildFeed.Model/Build-Group.cs b/BuildFeed.Model/BuildRepository-Group.cs similarity index 100% rename from BuildFeed.Model/Build-Group.cs rename to BuildFeed.Model/BuildRepository-Group.cs diff --git a/BuildFeed.Model/Build-Lab.cs b/BuildFeed.Model/BuildRepository-Lab.cs similarity index 100% rename from BuildFeed.Model/Build-Lab.cs rename to BuildFeed.Model/BuildRepository-Lab.cs diff --git a/BuildFeed.Model/Build-Source.cs b/BuildFeed.Model/BuildRepository-Source.cs similarity index 100% rename from BuildFeed.Model/Build-Source.cs rename to BuildFeed.Model/BuildRepository-Source.cs diff --git a/BuildFeed.Model/Build-Version.cs b/BuildFeed.Model/BuildRepository-Version.cs similarity index 100% rename from BuildFeed.Model/Build-Version.cs rename to BuildFeed.Model/BuildRepository-Version.cs diff --git a/BuildFeed.Model/Build-Year.cs b/BuildFeed.Model/BuildRepository-Year.cs similarity index 100% rename from BuildFeed.Model/Build-Year.cs rename to BuildFeed.Model/BuildRepository-Year.cs diff --git a/BuildFeed/Code/Options/Theme.cs b/BuildFeed/Code/Options/Theme.cs index e01f0d5..812bf58 100644 --- a/BuildFeed/Code/Options/Theme.cs +++ b/BuildFeed/Code/Options/Theme.cs @@ -1,12 +1,15 @@ using System; using System.ComponentModel.DataAnnotations; using System.Linq; +using System.Web; using BuildFeed.Local; namespace BuildFeed.Code.Options { public class Theme { + private const string THEME_COOKIE_NAME = "bf_theme"; + public static Theme[] AvailableThemes = (from st in Enum.GetValues(typeof(SiteTheme)).Cast() select new Theme(st)).ToArray(); @@ -17,6 +20,18 @@ namespace BuildFeed.Code.Options public string DisplayName => MvcExtensions.GetDisplayTextForEnum(_siteTheme); public Theme(SiteTheme st) { _siteTheme = st; } + + public static SiteTheme DetectTheme(HttpContextBase context) + { + string themeCookie = context.Request.Cookies[THEME_COOKIE_NAME]?.Value; + SiteTheme theme = SiteTheme.Dark; + if (!string.IsNullOrEmpty(themeCookie)) + { + Enum.TryParse(themeCookie, out theme); + } + + return theme; + } } public enum SiteTheme diff --git a/BuildFeed/Controllers/BaseController.cs b/BuildFeed/Controllers/BaseController.cs index 48dca07..71c2d6d 100644 --- a/BuildFeed/Controllers/BaseController.cs +++ b/BuildFeed/Controllers/BaseController.cs @@ -10,7 +10,6 @@ namespace BuildFeed.Controllers public class BaseController : Controller { private const string LANG_COOKIE_NAME = "bf_lang"; - private const string THEME_COOKIE_NAME = "bf_theme"; protected override void Initialize(RequestContext requestContext) { @@ -36,13 +35,7 @@ namespace BuildFeed.Controllers catch (CultureNotFoundException) { } } - string themeCookie = requestContext.HttpContext.Request.Cookies[THEME_COOKIE_NAME]?.Value; - SiteTheme theme = SiteTheme.Dark; - if (!string.IsNullOrEmpty(themeCookie)) - { - Enum.TryParse(themeCookie, out theme); - } - ViewBag.Theme = new Theme(theme); + ViewBag.Theme = new Theme(Theme.DetectTheme(requestContext.HttpContext)); base.Initialize(requestContext); } diff --git a/BuildFeed/Controllers/frontController.cs b/BuildFeed/Controllers/frontController.cs index 1664e44..1f4257c 100644 --- a/BuildFeed/Controllers/frontController.cs +++ b/BuildFeed/Controllers/frontController.cs @@ -29,7 +29,7 @@ namespace BuildFeed.Controllers [Route("", Order = 1)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task Index() { @@ -39,7 +39,7 @@ namespace BuildFeed.Controllers [Route("page-{page:int:min(1)}/", Order = 0)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] #endif public async Task IndexPage(int page) { @@ -59,7 +59,7 @@ namespace BuildFeed.Controllers [Route("group/{major}.{minor}.{number}.{revision}/", Order = 1)] [Route("group/{major}.{minor}.{number}/", Order = 5)] // for when there is no revision #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewGroup(uint major, uint minor, uint number, uint? revision = null) { @@ -73,7 +73,7 @@ namespace BuildFeed.Controllers List builds = await _bModel.SelectGroup(bg); - return builds.Count() == 1 + return builds.Count == 1 ? RedirectToAction(nameof(ViewBuild), new { @@ -84,7 +84,7 @@ namespace BuildFeed.Controllers [Route("build/{id:guid}/", Name = "Build")] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewBuild(Guid id) { @@ -184,7 +184,7 @@ namespace BuildFeed.Controllers [Route("lab/{lab}/", Order = 1, Name = "Lab Root")] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewLab(string lab) { @@ -193,7 +193,7 @@ namespace BuildFeed.Controllers [Route("lab/{lab}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewLabPage(string lab, int page) { @@ -219,7 +219,7 @@ namespace BuildFeed.Controllers [Route("source/{source}/", Order = 1, Name = "Source Root")] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewSource(TypeOfSource source) { @@ -228,7 +228,7 @@ namespace BuildFeed.Controllers [Route("source/{source}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewSourcePage(TypeOfSource source, int page) { @@ -254,7 +254,7 @@ namespace BuildFeed.Controllers [Route("year/{year}/", Order = 1, Name = "Year Root")] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewYear(int year) { @@ -263,7 +263,7 @@ namespace BuildFeed.Controllers [Route("year/{year}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewYearPage(int year, int page) { @@ -289,7 +289,7 @@ namespace BuildFeed.Controllers [Route("version/{major}.{minor}/", Order = 1, Name = "Version Root")] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewVersion(uint major, uint minor) { @@ -298,7 +298,7 @@ namespace BuildFeed.Controllers [Route("version/{major}.{minor}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - // [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] #endif public async Task ViewVersionPage(uint major, uint minor, int page) { diff --git a/BuildFeed/Global.asax.cs b/BuildFeed/Global.asax.cs index 57e9b34..3c421ff 100644 --- a/BuildFeed/Global.asax.cs +++ b/BuildFeed/Global.asax.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; +using BuildFeed.Code.Options; using BuildFeed.Model; namespace BuildFeed @@ -32,15 +35,26 @@ namespace BuildFeed public override string GetVaryByCustomString(HttpContext context, string custom) { - switch (custom) + string[] parts = custom.Split(';'); + List varyParts = new List(); + + foreach (string part in parts) { - case "userName": - return context.User.Identity.Name.ToLower(); - case "lang": - return context.Request.Cookies["lang"]?.Value ?? CultureInfo.CurrentUICulture.IetfLanguageTag; + switch (part) + { + case "userName": + varyParts.Add($"user:{context.User.Identity.Name}"); + break; + case "lang": + varyParts.Add($"lang:{CultureInfo.CurrentUICulture.LCID}"); + break; + case "theme": + varyParts.Add($"theme:{Theme.DetectTheme(new HttpContextWrapper(context))}"); + break; + } } - return ""; + return string.Join(";", varyParts.OrderBy(s => s)); } } } \ No newline at end of file