Merge pull request #1957 from umbraco/temp-U4-9940
U4-9940 Make it easy to get the integer Id of an item from it's Udi
This commit is contained in:
@@ -95,6 +95,16 @@ namespace Umbraco.Core.Services
|
||||
return result.HasValue ? Attempt.Succeed(result.Value) : Attempt<int>.Fail();
|
||||
}
|
||||
|
||||
public Attempt<int> GetIdForUdi(Udi udi)
|
||||
{
|
||||
var guidUdi = udi as GuidUdi;
|
||||
if (guidUdi == null)
|
||||
return Attempt<int>.Fail();
|
||||
|
||||
var umbracoType = Constants.UdiEntityType.ToUmbracoObjectType(guidUdi.EntityType);
|
||||
return GetIdForKey(guidUdi.Guid, umbracoType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the GUID for a given integer id
|
||||
/// </summary>
|
||||
@@ -124,9 +134,9 @@ namespace Umbraco.Core.Services
|
||||
|
||||
private static Guid GetNodeObjectTypeGuid(UmbracoObjectTypes umbracoObjectType)
|
||||
{
|
||||
var guid = umbracoObjectType.GetGuid();
|
||||
var guid = umbracoObjectType.GetGuid();
|
||||
if (guid == Guid.Empty)
|
||||
throw new NotSupportedException("Unsupported object type (" + umbracoObjectType + ").");
|
||||
throw new NotSupportedException("Unsupported object type (" + umbracoObjectType + ").");
|
||||
|
||||
return guid;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,14 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="key"></param>
|
||||
/// <param name="umbracoObjectType"></param>
|
||||
/// <returns></returns>
|
||||
Attempt<int> GetIdForKey(Guid key, UmbracoObjectTypes umbracoObjectType);
|
||||
Attempt<int> GetIdForKey(Guid key, UmbracoObjectTypes umbracoObjectType);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the integer id for a given Udi
|
||||
/// </summary>
|
||||
/// <param name="udi"></param>
|
||||
/// <returns></returns>
|
||||
Attempt<int> GetIdForUdi(Udi udi);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the GUID for a given integer id
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Umbraco.Web.Extensions
|
||||
return umbracoHelper.TypedMember(memberAttempt.Result);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Umbraco.Core.Cache;
|
||||
|
||||
namespace Umbraco.Web
|
||||
@@ -38,6 +37,7 @@ namespace Umbraco.Web
|
||||
private MembershipHelper _membershipHelper;
|
||||
private TagQuery _tag;
|
||||
private IDataTypeService _dataTypeService;
|
||||
private IEntityService _entityService;
|
||||
private UrlProvider _urlProvider;
|
||||
private ICultureDictionary _cultureDictionary;
|
||||
|
||||
@@ -113,6 +113,14 @@ namespace Umbraco.Web
|
||||
get { return _dataTypeService ?? (_dataTypeService = UmbracoContext.Application.Services.DataTypeService); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lazy instantiates the IEntityService
|
||||
/// </summary>
|
||||
private IEntityService EntityService
|
||||
{
|
||||
get { return _entityService ?? (_entityService = UmbracoContext.Application.Services.EntityService); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lazy instantiates the IUmbracoComponentRenderer if not specified in the constructor
|
||||
/// </summary>
|
||||
@@ -1489,5 +1497,10 @@ namespace Umbraco.Web
|
||||
return surfaceRouteParams.EncryptWithMachineKey();
|
||||
}
|
||||
|
||||
public int GetIdForUdi(Udi udi)
|
||||
{
|
||||
var udiToIdAttempt = EntityService.GetIdForUdi(udi);
|
||||
return udiToIdAttempt.Success ? udiToIdAttempt.Result : -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user