U4-8788 ContentService.GetByIds() returns items in different order
U4-8937 ContentController.PostSort is not honouring the passed sorted ids.
This commit is contained in:
@@ -342,11 +342,23 @@ namespace Umbraco.Core.Services
|
||||
/// <returns><see cref="IContent"/></returns>
|
||||
public IEnumerable<IContent> GetByIds(IEnumerable<int> ids)
|
||||
{
|
||||
if (ids.Any() == false) return Enumerable.Empty<IContent>();
|
||||
var idsArray = ids.ToArray();
|
||||
if (idsArray.Length == 0) return Enumerable.Empty<IContent>();
|
||||
|
||||
using (var repository = RepositoryFactory.CreateContentRepository(UowProvider.GetUnitOfWork()))
|
||||
{
|
||||
return repository.GetAll(ids.ToArray());
|
||||
//ensure that the result has the order based on the ids passed in
|
||||
var result = repository.GetAll(idsArray);
|
||||
|
||||
var content = result.ToDictionary(x => x.Id, x => x);
|
||||
|
||||
var sortedResult = idsArray.Select(x =>
|
||||
{
|
||||
IContent c;
|
||||
return content.TryGetValue(x, out c) ? c : null;
|
||||
}).WhereNotNull();
|
||||
|
||||
return sortedResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,27 @@ namespace Umbraco.Tests.Services
|
||||
Assert.IsTrue(contentService.PublishWithStatus(content).Success);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Get_By_Ids_Sorted()
|
||||
{
|
||||
// Arrange
|
||||
var contentService = ServiceContext.ContentService;
|
||||
|
||||
// Act
|
||||
var results = new List<IContent>();
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
results.Add(contentService.CreateContentWithIdentity("Test", -1, "umbTextpage", 0));
|
||||
}
|
||||
|
||||
var sortedGet = contentService.GetByIds(new[] {results[10].Id, results[5].Id, results[12].Id}).ToArray();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(sortedGet[0].Id, results[10].Id);
|
||||
Assert.AreEqual(sortedGet[1].Id, results[5].Id);
|
||||
Assert.AreEqual(sortedGet[2].Id, results[12].Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Count_All()
|
||||
{
|
||||
|
||||
@@ -489,20 +489,11 @@ namespace Umbraco.Web.Editors
|
||||
{
|
||||
var contentService = Services.ContentService;
|
||||
|
||||
// content service GetByIds does *not* order the content items
|
||||
// so we need get them in the proper order here - no need to sort!
|
||||
var content = contentService
|
||||
.GetByIds(sorted.IdSortOrder)
|
||||
.ToDictionary(x => x.Id, x => x);
|
||||
|
||||
var sortedContent = sorted.IdSortOrder.Select(x =>
|
||||
{
|
||||
IContent c;
|
||||
return content.TryGetValue(x, out c) ? c : null;
|
||||
}).WhereNotNull();
|
||||
// content service GetByIds does order the content items based on the order of Ids passed in
|
||||
var content = contentService.GetByIds(sorted.IdSortOrder);
|
||||
|
||||
// Save content with new sort order and update content xml in db accordingly
|
||||
if (contentService.Sort(sortedContent) == false)
|
||||
if (contentService.Sort(content) == false)
|
||||
{
|
||||
LogHelper.Warn<ContentController>("Content sorting failed, this was probably caused by an event being cancelled");
|
||||
return Request.CreateValidationErrorResponse("Content sorting failed, this was probably caused by an event being cancelled");
|
||||
|
||||
Reference in New Issue
Block a user