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:
Morten Christensen
2012-11-13 19:07:43 -01:00
parent f7b7b90bed
commit 5540959e71
5 changed files with 124 additions and 4 deletions

View File

@@ -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>

View File

@@ -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.
}

View File

@@ -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>