Skip to content

Commit

Permalink
Implement ILocalizationService.GetDictionaryItemKeyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan committed Sep 4, 2017
1 parent 44102df commit 9a06c3a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ private IEnumerable<IDictionaryItem> GetRootDictionaryItems()
return GetByQuery(query);
}

public Dictionary<string, Guid> GetDictionaryItemKeyMap()
{
var columns = new[] { "key", "id" }.Select(x => (object) SqlSyntax.GetQuotedColumnName(x)).ToArray();
var sql = new Sql().Select(columns).From<DictionaryDto>(SqlSyntax);
return Database.Fetch<DictionaryItemKeyIdDto>(sql).ToDictionary(x => x.Key, x => x.Id);
}

private class DictionaryItemKeyIdDto
{
public string Key { get; set; }
public Guid Id { get; set; }
}

public IEnumerable<IDictionaryItem> GetDictionaryItemDescendants(Guid? parentId)
{
//This methods will look up children at each level, since we do not store a path for dictionary (ATM), we need to do a recursive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IDictionaryRepository : IRepositoryQueryable<int, IDictionaryIt
IDictionaryItem Get(Guid uniqueId);
IDictionaryItem Get(string key);
IEnumerable<IDictionaryItem> GetDictionaryItemDescendants(Guid? parentId);
Dictionary<string, Guid> GetDictionaryItemKeyMap();
}
}
6 changes: 6 additions & 0 deletions src/Umbraco.Core/Services/ILocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,11 @@ public interface ILocalizationService : IService
/// <param name="language"><see cref="ILanguage"/> to delete</param>
/// <param name="userId">Optional id of the user deleting the language</param>
void Delete(ILanguage language, int userId = 0);

/// <summary>
/// Gets the full dictionary key map.
/// </summary>
/// <returns>The full dictionary key map.</returns>
Dictionary<string, Guid> GetDictionaryItemKeyMap();
}
}
9 changes: 9 additions & 0 deletions src/Umbraco.Core/Services/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ private void EnsureDictionaryItemLanguageCallback(IDictionaryItem d)
}
}

public Dictionary<string, Guid> GetDictionaryItemKeyMap()
{
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
{
var repository = RepositoryFactory.CreateDictionaryRepository(uow);
return repository.GetDictionaryItemKeyMap();
}
}

#region Event Handlers
/// <summary>
/// Occurs before Delete
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void Can_Perform_Get_On_DictionaryRepository()

//re-get
dictionaryItem = repository.Get(dictionaryItem.Id);


// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Expand All @@ -140,10 +141,10 @@ public void Can_Perform_Get_On_DictionaryRepository_When_No_Language_Assigned()
// Arrange
var provider = new PetaPocoUnitOfWorkProvider(Logger);
var unitOfWork = provider.GetUnitOfWork();

using (var repository = CreateRepository(unitOfWork))
{
var dictionaryItem = (IDictionaryItem) new DictionaryItem("Testing1235");
var dictionaryItem = (IDictionaryItem) new DictionaryItem("Testing1235");

repository.AddOrUpdate(dictionaryItem);
unitOfWork.Commit();
Expand All @@ -160,7 +161,6 @@ public void Can_Perform_Get_On_DictionaryRepository_When_No_Language_Assigned()

}


[Test]
public void Can_Perform_GetAll_On_DictionaryRepository()
{
Expand Down Expand Up @@ -319,7 +319,7 @@ public void Can_Perform_Update_WithNewTranslation_On_DictionaryRepository()
unitOfWork.Commit();

var dictionaryItem = (DictionaryItem)repository.Get(1);

// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.Translations.Count(), Is.EqualTo(3));
Expand Down Expand Up @@ -364,6 +364,24 @@ public void Can_Perform_Exists_On_DictionaryRepository()
}
}

[Test]
public void Can_Perform_GetDictionaryItemKeyMap_On_DictionaryRepository()
{
Dictionary<string, Guid> keyMap;

var provider = new PetaPocoUnitOfWorkProvider(Logger);
using (var unitOfWork = provider.GetUnitOfWork(readOnly: true))
{
var repository = CreateRepository(unitOfWork);
keyMap = repository.GetDictionaryItemKeyMap();
}

Assert.IsNotNull(keyMap);
Assert.IsNotEmpty(keyMap);
foreach (var kvp in keyMap)
Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}

[TearDown]
public override void TearDown()
{
Expand Down

0 comments on commit 9a06c3a

Please sign in to comment.