Correcting ability to set guid Key, as it was overwritten when saving.
Adding unit tests to verify get/set Key. Adding unit tests to verify GetById with guid Key. Adding overload to GetById with guid Key to supplement the int Id.
This commit is contained in:
@@ -327,7 +327,9 @@ namespace Umbraco.Core.Models
|
||||
internal override void AddingEntity()
|
||||
{
|
||||
base.AddingEntity();
|
||||
Key = Guid.NewGuid();
|
||||
|
||||
if(Key == Guid.Empty)
|
||||
Key = Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -77,6 +77,20 @@ namespace Umbraco.Core.Services
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IContent"/> object by its 'UniqueId'
|
||||
/// </summary>
|
||||
/// <param name="key">Guid key of the Content to retrieve</param>
|
||||
/// <returns><see cref="IContent"/></returns>
|
||||
public IContent GetById(Guid key)
|
||||
{
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
||||
var query = Query<IContent>.Builder.Where(x => x.Key == key);
|
||||
var contents = repository.GetByQuery(query);
|
||||
return contents.SingleOrDefault();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
@@ -740,7 +754,7 @@ namespace Umbraco.Core.Services
|
||||
//If a user id was passed in we use that
|
||||
content.CreatorId = userId;
|
||||
}
|
||||
else if(_userService != null)
|
||||
else if (UserServiceOrContext())
|
||||
{
|
||||
var profile = _httpContext == null
|
||||
? _userService.GetCurrentBackOfficeUser()
|
||||
@@ -766,7 +780,7 @@ namespace Umbraco.Core.Services
|
||||
//If a user id was passed in we use that
|
||||
content.WriterId = userId;
|
||||
}
|
||||
else if (_userService != null)
|
||||
else if (UserServiceOrContext())
|
||||
{
|
||||
var profile = _httpContext == null
|
||||
? _userService.GetCurrentBackOfficeUser()
|
||||
@@ -780,6 +794,11 @@ namespace Umbraco.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
private bool UserServiceOrContext()
|
||||
{
|
||||
return _userService != null && (HttpContext.Current != null || _httpContext != null);
|
||||
}
|
||||
|
||||
//TODO Add method to remove versions from Content
|
||||
//TODO Add method to remove versions from all Content - parameters to select date-interval, ea. remove versions older then.
|
||||
}
|
||||
|
||||
@@ -31,6 +31,13 @@ namespace Umbraco.Core.Services
|
||||
/// <returns><see cref="IContent"/></returns>
|
||||
IContent GetById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IContent"/> object by its 'UniqueId'
|
||||
/// </summary>
|
||||
/// <param name="key">Guid key of the Content to retrieve</param>
|
||||
/// <returns><see cref="IContent"/></returns>
|
||||
IContent GetById(Guid key);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user