Last active
November 17, 2016 19:29
-
-
Save emmanueltissera/fa1193d1180fad01cec655eddd6361c0 to your computer and use it in GitHub Desktop.
SuzieT.Kentico.WebApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Runtime.Caching; | |
using System.Web; | |
using SuzieT.Kentico.WebApp.Interfaces; | |
namespace SuzieT.Kentico.WebApp.Helpers | |
{ | |
public class ContentCache | |
{ | |
private static readonly MemoryCache Cache = new MemoryCache("ContentCache"); | |
private static readonly int CacheExpiryInSeconds = GetCacheExpiryValue(600); | |
public static T AddOrGetExisting<T>(string key, Func<T> valueFactory) | |
{ | |
var newValue = new Lazy<T>(valueFactory); | |
var cacheItemPolicy = new CacheItemPolicy() | |
{ | |
AbsoluteExpiration = new DateTimeOffset(DateTime.UtcNow.AddSeconds(CacheExpiryInSeconds)) | |
}; | |
var oldValue = Cache.AddOrGetExisting(key, newValue, cacheItemPolicy) as Lazy<T>; | |
try | |
{ | |
return (oldValue ?? newValue).Value; | |
} | |
catch | |
{ | |
// Handle cached lazy exception by evicting from cache. | |
Cache.Remove(key); | |
return newValue.Value; | |
} | |
} | |
private static int GetCacheExpiryValue(int defaultValue) | |
{ | |
int numberOfSeconds; | |
if (int.TryParse(ConfigurationManager.AppSettings["DeliveryContentCacheTimeSeconds"], out numberOfSeconds)) | |
{ | |
return numberOfSeconds; | |
} | |
return defaultValue; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using KenticoCloud.Deliver; | |
using SuzieT.Kentico.WebApp.Factories; | |
using SuzieT.Kentico.WebApp.Interfaces; | |
using SuzieT.Kentico.WebApp.Models; | |
using System.Collections.Generic; | |
using System.Text; | |
using Microsoft.Ajax.Utilities; | |
namespace SuzieT.Kentico.WebApp.Helpers | |
{ | |
public static class ContentDeliveryHelper | |
{ | |
public static SettingsItemModel GetGeneralSettings() | |
{ | |
var model = DeliverClientFactory<SettingsItemModel>.GetItem(SettingsItemModel.ItemCodeName); | |
return model; | |
} | |
public static List<T> GetListOfModularContent<T>(this IEnumerable<ContentItem> items) where T : IKenticoDeliverModel, new() | |
{ | |
var modularList = new List<T>(); | |
foreach (var module in items) | |
{ | |
var model = new T(); | |
model.MapContent(module); | |
modularList.Add(model); | |
} | |
return modularList; | |
} | |
public static KeyValuePair<string, string> GetSelectedTaxonomy(this ContentItem content, string elementCode) | |
{ | |
var element = content.Elements[elementCode]; | |
return element != null && element.value != null ? new KeyValuePair<string, string>(element.value[0].codename.ToString(), element.value[0].name.ToString()) : new KeyValuePair<string, string>(); | |
} | |
public static string StringfyFilter(this IEnumerable<IFilter> parameters) | |
{ | |
if (parameters == null) | |
return "no-params"; | |
var filterBuilder = new StringBuilder(); | |
foreach (var filter in parameters) | |
{ | |
filterBuilder.Append($"{filter.GetQueryStringParameter()}-"); | |
} | |
return filterBuilder.ToString().ToLower(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using KenticoCloud.Deliver; | |
using SuzieT.Kentico.WebApp.Helpers; | |
using SuzieT.Kentico.WebApp.Interfaces; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Threading.Tasks; | |
namespace SuzieT.Kentico.WebApp.Factories | |
{ | |
public class DeliverClientFactory<T> where T : IKenticoDeliverModel, new() | |
{ | |
private static readonly DeliverClient client = new DeliverClient(ConfigurationManager.AppSettings["ProjectId"]); | |
public static T GetItem(string itemCodename, IEnumerable<IFilter> parameters = null) | |
{ | |
return Task.Run(() => GetCachedItemAsync(itemCodename, parameters)).Result; | |
} | |
public static async Task<T> GetItemAsync(string itemCodename, IEnumerable<IFilter> parameters = null) | |
{ | |
return await GetCachedItemAsync(itemCodename, parameters); | |
} | |
public static async Task<T> GetItemByIdAsync(string itemId) | |
{ | |
itemId = UrlHelper.GetKenticoIdFromUrl(itemId); | |
return await GetItemAsync(itemId); | |
} | |
public static async Task<T> GetItemsAsync(IEnumerable<IFilter> parameters = null) | |
{ | |
return await GetCachedItemsAsync(parameters); | |
} | |
private static async Task<T> GetCachedItemAsync(string itemCodename, | |
IEnumerable<IFilter> parameters = null) | |
{ | |
var cacheKey = $"dcf-cache-{itemCodename ?? string.Empty}-{parameters.StringfyFilter()}"; | |
return await ContentCache.AddOrGetExisting<Task<T>>(cacheKey, () => GetItemAsyncInternal(itemCodename, parameters)); | |
} | |
private static async Task<T> GetCachedItemsAsync(IEnumerable<IFilter> parameters = null) | |
{ | |
var cacheKey = $"dcf-cache-items-{parameters.StringfyFilter()}"; | |
return await ContentCache.AddOrGetExisting<Task<T>>(cacheKey, () => GetItemsAsyncInternal(parameters)); | |
} | |
private static async Task<T> GetItemAsyncInternal(string itemCodename, | |
IEnumerable<IFilter> parameters = null) | |
{ | |
var response = await client.GetItemAsync(itemCodename, parameters); | |
return GetItem(response.Item); | |
} | |
private static async Task<T> GetItemsAsyncInternal(IEnumerable<IFilter> parameters = null) | |
{ | |
var response = await client.GetItemsAsync(parameters); | |
return GetItems(response.Items); | |
} | |
private static T GetItem(ContentItem content) | |
{ | |
var item = new T(); | |
item.MapContent(content); | |
return item; | |
} | |
private static T GetItems(List<ContentItem> contentList) | |
{ | |
var item = new T(); | |
item.MapContentList(contentList); | |
return item; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace SuzieT.Kentico.WebApp | |
{ | |
public class RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapMvcAttributeRoutes(); | |
routes.MapRoute( | |
name: "HelpPageIndex", | |
url: "help", | |
defaults: new { controller = "Content", action = "Detail", id = "help" } | |
); | |
routes.MapRoute( | |
name: "HelpPages", | |
url: "help/{id}", | |
defaults: new {controller = "Content", action = "Detail"} | |
); | |
routes.MapRoute( | |
name: "ContactPageIndex", | |
url: "contact", | |
defaults: new { controller = "Content", action = "Detail", id = "contact" } | |
); | |
routes.MapRoute( | |
name: "ContactPages", | |
url: "contact/{id}", | |
defaults: new { controller = "Content", action = "Detail" } | |
); | |
routes.MapRoute( | |
name: "404Page", | |
url: "404", | |
defaults: new { controller = "Content", action = "Detail", id = "404" } | |
); | |
routes.MapRoute( | |
name: "500Page", | |
url: "500", | |
defaults: new { controller = "Content", action = "Detail", id = "500" } | |
); | |
routes.MapRoute( | |
name: "Default", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SuzieT.Kentico.WebApp.Helpers | |
{ | |
public static class UrlHelper | |
{ | |
public static string GetFriendlyUrl(KenticoCloud.Deliver.System system) | |
{ | |
var url = string.Empty; | |
if (system == null) | |
{ | |
return url; | |
} | |
if (system.SitemapLocations.Count > 0) | |
{ | |
url = $"/{system.SitemapLocations[0]}"; | |
} | |
if (url == "/root" & system.Codename == "home") | |
{ | |
return "/"; | |
} | |
if (url == "/root") | |
{ | |
url = string.Empty; | |
} | |
var codeName = system.Codename.Replace("_", "-"); | |
if (codeName.StartsWith("n") && char.IsNumber(codeName, 1)) | |
{ | |
codeName = codeName.TrimStart(new[] {'n'}); | |
} | |
url = $"{url}/{codeName}/"; | |
return url; | |
} | |
public static string GetKenticoIdFromUrl(string url) | |
{ | |
if (string.IsNullOrEmpty(url)) | |
{ | |
return string.Empty; | |
} | |
if (char.IsNumber(url, 0)) | |
{ | |
url = $"n{url}"; | |
} | |
return url.Replace("-", "_"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment