Fixing homepage service

Fixes #6263
Fixes #6273
This commit is contained in:
Sebastien Ros 2016-01-14 17:35:15 -08:00
parent b91d001010
commit ec0b4b1a4b
2 changed files with 37 additions and 8 deletions

View File

@ -83,11 +83,24 @@ namespace Orchard.Alias.Implementation.Storage {
}
public IDictionary<string, string> Get(string path) {
return _aliasRepository
.Fetch(r => r.Path == path, o => o.Asc(r => r.Id), 0, 1)
.Select(ToDictionary)
.Select(item => item.Item3)
.SingleOrDefault();
// All aliases are already in memory, and updated. We don't need to query the
// database to lookup an alias by path.
AliasInfo alias;
// Optimized code path on Contents as it's the main provider of aliases
if (_aliasHolder.GetMap("Contents").TryGetAlias(path, out alias)) {
return alias.RouteValues;
}
foreach (var map in _aliasHolder.GetMaps()) {
if(map.TryGetAlias(path, out alias)) {
return alias.RouteValues;
}
}
return null;
}
public void Remove(string path) {

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using Orchard.Alias;
@ -14,6 +15,8 @@ namespace Orchard.Autoroute.Services {
private const string AliasSource = "Autoroute:Home";
private const string HomeAlias = "";
private RouteValueDictionary _homeAliasRoute;
public HomeAliasService(IAliasService aliasService, IAliasHolder aliasHolder, IContentManager contentManager) {
_aliasService = aliasService;
_aliasHolder = aliasHolder;
@ -21,7 +24,11 @@ namespace Orchard.Autoroute.Services {
}
public RouteValueDictionary GetHomeRoute() {
return _aliasService.Get(HomeAlias);
if(_homeAliasRoute == null) {
_homeAliasRoute = _aliasService.Get(HomeAlias);
}
return _homeAliasRoute;
}
public int? GetHomePageId(VersionOptions version = null) {
@ -40,7 +47,7 @@ namespace Orchard.Autoroute.Services {
if (alias == null)
return null;
var homePage = _contentManager.Query<AutoroutePart, AutoroutePartRecord>(version).Where(x => x.DisplayAlias == alias).Slice(0, 1).SingleOrDefault();
var homePage = _contentManager.Query<AutoroutePart, AutoroutePartRecord>(version).Where(x => x.DisplayAlias == alias).Slice(0, 1).FirstOrDefault();
return homePage;
}
@ -60,6 +67,7 @@ namespace Orchard.Autoroute.Services {
}
public void PublishHomeAlias(RouteValueDictionary route) {
_homeAliasRoute = route;
_aliasService.DeleteBySource(AliasSource);
_aliasService.Set(HomeAlias, route, AliasSource);
}
@ -74,8 +82,16 @@ namespace Orchard.Autoroute.Services {
if (map == null)
return null;
var alias = map.GetAliases().FirstOrDefault(x => !String.IsNullOrWhiteSpace(x.Path));
var alias = map.GetAliases().FirstOrDefault(x => IsSameRoute(x.RouteValues, routeValues) && !String.IsNullOrWhiteSpace(x.Path));
return alias != null ? alias.Path : null;
}
private bool IsSameRoute(IDictionary<string,string> a, RouteValueDictionary b) {
if(a.Count != b.Count) {
return false;
}
return a.Keys.All(x => a[x] == b[x].ToString());
}
}
}