diff --git a/src/Umbraco.Core/Models/DictionaryItem.cs b/src/Umbraco.Core/Models/DictionaryItem.cs index f816b9cadc..4c649a5cb3 100644 --- a/src/Umbraco.Core/Models/DictionaryItem.cs +++ b/src/Umbraco.Core/Models/DictionaryItem.cs @@ -11,22 +11,22 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - public class DictionaryItem : Entity, IAggregateRoot + public class DictionaryItem : Entity, IDictionaryItem { private Guid _parentId; private string _itemKey; - private IEnumerable _translations; + private IEnumerable _translations; public DictionaryItem(Guid parentId, string itemKey) { _parentId = parentId; _itemKey = itemKey; - _translations = new List(); + _translations = new List(); } private static readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo(x => x.ParentId); private static readonly PropertyInfo ItemKeySelector = ExpressionHelper.GetPropertyInfo(x => x.ItemKey); - private static readonly PropertyInfo TranslationsSelector = ExpressionHelper.GetPropertyInfo>(x => x.Translations); + private static readonly PropertyInfo TranslationsSelector = ExpressionHelper.GetPropertyInfo>(x => x.Translations); /// /// Gets or Sets the Parent Id of the Dictionary Item @@ -60,7 +60,7 @@ namespace Umbraco.Core.Models /// Gets or sets a list of translations for the Dictionary Item /// [DataMember] - public IEnumerable Translations + public IEnumerable Translations { get { return _translations; } set diff --git a/src/Umbraco.Core/Models/DictionaryTranslation.cs b/src/Umbraco.Core/Models/DictionaryTranslation.cs index ad6daa5570..0e0896989e 100644 --- a/src/Umbraco.Core/Models/DictionaryTranslation.cs +++ b/src/Umbraco.Core/Models/DictionaryTranslation.cs @@ -10,32 +10,32 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - public class DictionaryTranslation : Entity + public class DictionaryTranslation : Entity, IDictionaryTranslation { - private Language _language; + private ILanguage _language; private string _value; - public DictionaryTranslation(Language language, string value) + public DictionaryTranslation(ILanguage language, string value) { _language = language; _value = value; } - public DictionaryTranslation(Language language, string value, Guid uniqueId) + public DictionaryTranslation(ILanguage language, string value, Guid uniqueId) { _language = language; _value = value; Key = uniqueId; } - private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo(x => x.Language); + private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo(x => x.Language); private static readonly PropertyInfo ValueSelector = ExpressionHelper.GetPropertyInfo(x => x.Value); /// /// Gets or sets the for the translation /// [DataMember] - public Language Language + public ILanguage Language { get { return _language; } set diff --git a/src/Umbraco.Core/Models/IDictionaryItem.cs b/src/Umbraco.Core/Models/IDictionaryItem.cs new file mode 100644 index 0000000000..9c748dde40 --- /dev/null +++ b/src/Umbraco.Core/Models/IDictionaryItem.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Models +{ + public interface IDictionaryItem : IAggregateRoot + { + /// + /// Gets or Sets the Parent Id of the Dictionary Item + /// + [DataMember] + Guid ParentId { get; set; } + + /// + /// Gets or sets the Key for the Dictionary Item + /// + [DataMember] + string ItemKey { get; set; } + + /// + /// Gets or sets a list of translations for the Dictionary Item + /// + [DataMember] + IEnumerable Translations { get; set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/IDictionaryTranslation.cs b/src/Umbraco.Core/Models/IDictionaryTranslation.cs new file mode 100644 index 0000000000..7011e7faf7 --- /dev/null +++ b/src/Umbraco.Core/Models/IDictionaryTranslation.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Models +{ + public interface IDictionaryTranslation : IEntity + { + /// + /// Gets or sets the for the translation + /// + [DataMember] + ILanguage Language { get; set; } + + /// + /// Gets or sets the translated text + /// + [DataMember] + string Value { get; set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ILanguage.cs b/src/Umbraco.Core/Models/ILanguage.cs new file mode 100644 index 0000000000..7457a097b8 --- /dev/null +++ b/src/Umbraco.Core/Models/ILanguage.cs @@ -0,0 +1,27 @@ +using System.Globalization; +using System.Runtime.Serialization; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Models +{ + public interface ILanguage : IAggregateRoot + { + /// + /// Gets or sets the Iso Code for the Language + /// + [DataMember] + string IsoCode { get; set; } + + /// + /// Gets or sets the Culture Name for the Language + /// + [DataMember] + string CultureName { get; set; } + + /// + /// Returns a object for the current Language + /// + [IgnoreDataMember] + CultureInfo CultureInfo { get; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/Language.cs b/src/Umbraco.Core/Models/Language.cs index 65246bcf63..307584de11 100644 --- a/src/Umbraco.Core/Models/Language.cs +++ b/src/Umbraco.Core/Models/Language.cs @@ -11,7 +11,7 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - public class Language : Entity, IAggregateRoot + public class Language : Entity, ILanguage { private string _isoCode; private string _cultureName; diff --git a/src/Umbraco.Core/Persistence/Factories/DictionaryItemFactory.cs b/src/Umbraco.Core/Persistence/Factories/DictionaryItemFactory.cs index 94f7116e25..1d52c0c52b 100644 --- a/src/Umbraco.Core/Persistence/Factories/DictionaryItemFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/DictionaryItemFactory.cs @@ -4,11 +4,11 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - internal class DictionaryItemFactory : IEntityFactory + internal class DictionaryItemFactory : IEntityFactory { #region Implementation of IEntityFactory - public DictionaryItem BuildEntity(DictionaryDto dto) + public IDictionaryItem BuildEntity(DictionaryDto dto) { return new DictionaryItem(dto.Parent, dto.Key) { @@ -17,7 +17,7 @@ namespace Umbraco.Core.Persistence.Factories }; } - public DictionaryDto BuildDto(DictionaryItem entity) + public DictionaryDto BuildDto(IDictionaryItem entity) { return new DictionaryDto { @@ -31,7 +31,7 @@ namespace Umbraco.Core.Persistence.Factories #endregion - private List BuildLanguageTextDtos(DictionaryItem entity) + private List BuildLanguageTextDtos(IDictionaryItem entity) { var list = new List(); foreach (var translation in entity.Translations) diff --git a/src/Umbraco.Core/Persistence/Factories/DictionaryTranslationFactory.cs b/src/Umbraco.Core/Persistence/Factories/DictionaryTranslationFactory.cs index fb5fd268f5..de2aae5b6c 100644 --- a/src/Umbraco.Core/Persistence/Factories/DictionaryTranslationFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/DictionaryTranslationFactory.cs @@ -4,12 +4,12 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - internal class DictionaryTranslationFactory : IEntityFactory + internal class DictionaryTranslationFactory : IEntityFactory { private readonly Guid _uniqueId; - private Language _language; + private ILanguage _language; - public DictionaryTranslationFactory(Guid uniqueId, Language language) + public DictionaryTranslationFactory(Guid uniqueId, ILanguage language) { _uniqueId = uniqueId; _language = language; @@ -17,13 +17,13 @@ namespace Umbraco.Core.Persistence.Factories #region Implementation of IEntityFactory - public DictionaryTranslation BuildEntity(LanguageTextDto dto) + public IDictionaryTranslation BuildEntity(LanguageTextDto dto) { return new DictionaryTranslation(_language, dto.Value, _uniqueId) {Id = dto.PrimaryKey}; } - public LanguageTextDto BuildDto(DictionaryTranslation entity) + public LanguageTextDto BuildDto(IDictionaryTranslation entity) { var text = new LanguageTextDto { diff --git a/src/Umbraco.Core/Persistence/Factories/LanguageFactory.cs b/src/Umbraco.Core/Persistence/Factories/LanguageFactory.cs index a09225724e..990e6436dd 100644 --- a/src/Umbraco.Core/Persistence/Factories/LanguageFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/LanguageFactory.cs @@ -4,16 +4,16 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - internal class LanguageFactory : IEntityFactory + internal class LanguageFactory : IEntityFactory { #region Implementation of IEntityFactory - public Language BuildEntity(LanguageDto dto) + public ILanguage BuildEntity(LanguageDto dto) { return new Language(dto.IsoCode){CultureName = dto.CultureName, Id = dto.Id}; } - public LanguageDto BuildDto(Language entity) + public LanguageDto BuildDto(ILanguage entity) { var dto = new LanguageDto{ CultureName = entity.CultureName, IsoCode = entity.IsoCode}; if (entity.HasIdentity) diff --git a/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs b/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs index 3931baef22..7fbedcabf7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/DictionaryRepository.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.Caching; using Umbraco.Core.Persistence.Factories; @@ -14,7 +15,7 @@ namespace Umbraco.Core.Persistence.Repositories /// /// Represents a repository for doing CRUD operations for /// - internal class DictionaryRepository : PetaPocoRepositoryBase, IDictionaryRepository + internal class DictionaryRepository : PetaPocoRepositoryBase, IDictionaryRepository { private readonly ILanguageRepository _languageRepository; @@ -31,7 +32,7 @@ namespace Umbraco.Core.Persistence.Repositories #region Overrides of RepositoryBase - protected override DictionaryItem PerformGet(int id) + protected override IDictionaryItem PerformGet(int id) { var sql = GetBaseQuery(false); sql.Append(GetBaseWhereClause(id)); @@ -43,7 +44,7 @@ namespace Umbraco.Core.Persistence.Repositories var factory = new DictionaryItemFactory(); var entity = factory.BuildEntity(dto); - var list = new List(); + var list = new List(); foreach (var textDto in dto.LanguageTextDtos) { var language = _languageRepository.Get(textDto.LanguageId); @@ -55,7 +56,7 @@ namespace Umbraco.Core.Persistence.Repositories return entity; } - protected override IEnumerable PerformGetAll(params int[] ids) + protected override IEnumerable PerformGetAll(params int[] ids) { if (ids.Any()) { @@ -74,10 +75,10 @@ namespace Umbraco.Core.Persistence.Repositories } } - protected override IEnumerable PerformGetByQuery(IQuery query) + protected override IEnumerable PerformGetByQuery(IQuery query) { var sqlClause = GetBaseQuery(false); - var translator = new SqlTranslator(sqlClause, query); + var translator = new SqlTranslator(sqlClause, query); var sql = translator.Translate(); var dtos = Database.Fetch(sql); @@ -125,9 +126,9 @@ namespace Umbraco.Core.Persistence.Repositories #region Unit of Work Implementation - protected override void PersistNewItem(DictionaryItem entity) + protected override void PersistNewItem(IDictionaryItem entity) { - entity.AddingEntity(); + ((Entity)entity).AddingEntity(); var factory = new DictionaryItemFactory(); var dto = factory.BuildDto(entity); @@ -142,12 +143,12 @@ namespace Umbraco.Core.Persistence.Repositories translation.Id = Convert.ToInt32(Database.Insert(textDto)); } - entity.ResetDirtyProperties(); + ((Entity)entity).ResetDirtyProperties(); } - protected override void PersistUpdatedItem(DictionaryItem entity) + protected override void PersistUpdatedItem(IDictionaryItem entity) { - entity.UpdatingEntity(); + ((Entity)entity).UpdatingEntity(); var factory = new DictionaryItemFactory(); var dto = factory.BuildDto(entity); @@ -168,10 +169,10 @@ namespace Umbraco.Core.Persistence.Repositories } } - entity.ResetDirtyProperties(); + ((Entity)entity).ResetDirtyProperties(); } - protected override void PersistDeletedItem(DictionaryItem entity) + protected override void PersistDeletedItem(IDictionaryItem entity) { RecursiveDelete(entity.Key); diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDictionaryRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDictionaryRepository.cs index 5d762eec00..08e2de5cc1 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDictionaryRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDictionaryRepository.cs @@ -2,7 +2,7 @@ namespace Umbraco.Core.Persistence.Repositories { - internal interface IDictionaryRepository : IRepositoryQueryable + internal interface IDictionaryRepository : IRepositoryQueryable { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ILanguageRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ILanguageRepository.cs index 303e573d54..01af81f1b7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/ILanguageRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/ILanguageRepository.cs @@ -2,7 +2,7 @@ namespace Umbraco.Core.Persistence.Repositories { - public interface ILanguageRepository : IRepositoryQueryable + public interface ILanguageRepository : IRepositoryQueryable { } diff --git a/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs b/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs index 4aa183a2ca..fc5c3ef202 100644 --- a/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/LanguageRepository.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.Caching; using Umbraco.Core.Persistence.Factories; @@ -13,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories /// /// Represents a repository for doing CRUD operations for /// - internal class LanguageRepository : PetaPocoRepositoryBase, ILanguageRepository + internal class LanguageRepository : PetaPocoRepositoryBase, ILanguageRepository { public LanguageRepository(IUnitOfWork work) : base(work) { @@ -26,7 +27,7 @@ namespace Umbraco.Core.Persistence.Repositories #region Overrides of RepositoryBase - protected override Language PerformGet(int id) + protected override ILanguage PerformGet(int id) { var sql = GetBaseQuery(false); sql.Append(GetBaseWhereClause(id)); @@ -40,7 +41,7 @@ namespace Umbraco.Core.Persistence.Repositories return entity; } - protected override IEnumerable PerformGetAll(params int[] ids) + protected override IEnumerable PerformGetAll(params int[] ids) { if (ids.Any()) { @@ -59,10 +60,10 @@ namespace Umbraco.Core.Persistence.Repositories } } - protected override IEnumerable PerformGetByQuery(IQuery query) + protected override IEnumerable PerformGetByQuery(IQuery query) { var sqlClause = GetBaseQuery(false); - var translator = new SqlTranslator(sqlClause, query); + var translator = new SqlTranslator(sqlClause, query); var sql = translator.Translate(); var dtos = Database.Fetch(sql); @@ -111,9 +112,9 @@ namespace Umbraco.Core.Persistence.Repositories #region Unit of Work Implementation - protected override void PersistNewItem(Language entity) + protected override void PersistNewItem(ILanguage entity) { - entity.AddingEntity(); + ((Entity)entity).AddingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); @@ -121,19 +122,19 @@ namespace Umbraco.Core.Persistence.Repositories var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; - entity.ResetDirtyProperties(); + ((Entity)entity).ResetDirtyProperties(); } - protected override void PersistUpdatedItem(Language entity) + protected override void PersistUpdatedItem(ILanguage entity) { - entity.UpdatingEntity(); + ((Entity)entity).UpdatingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); Database.Update(dto); - entity.ResetDirtyProperties(); + ((Entity)entity).ResetDirtyProperties(); } #endregion diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 7940527a46..336453985a 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -88,7 +88,10 @@ + + + diff --git a/src/Umbraco.Tests/Persistence/RepositoryResolverTests.cs b/src/Umbraco.Tests/Persistence/RepositoryResolverTests.cs index 6b291418f8..5666b0a551 100644 --- a/src/Umbraco.Tests/Persistence/RepositoryResolverTests.cs +++ b/src/Umbraco.Tests/Persistence/RepositoryResolverTests.cs @@ -94,7 +94,7 @@ namespace Umbraco.Tests.Persistence var uow = new PetaPocoUnitOfWork(); // Act - var repository = RepositoryResolver.ResolveByType(uow); + var repository = RepositoryResolver.ResolveByType(uow); // Assert Assert.That(repository, Is.Not.Null); @@ -107,7 +107,7 @@ namespace Umbraco.Tests.Persistence var uow = new PetaPocoUnitOfWork(); // Act - var repository = RepositoryResolver.ResolveByType(uow); + var repository = RepositoryResolver.ResolveByType(uow); // Assert Assert.That(repository, Is.Not.Null); diff --git a/src/Umbraco.Web/Services/ILocalizationService.cs b/src/Umbraco.Web/Services/ILocalizationService.cs index 5c201c477f..a145277f04 100644 --- a/src/Umbraco.Web/Services/ILocalizationService.cs +++ b/src/Umbraco.Web/Services/ILocalizationService.cs @@ -1,10 +1,104 @@ -namespace Umbraco.Web.Services +using System; +using System.Collections.Generic; +using Umbraco.Core.Models; + +namespace Umbraco.Web.Services { /// /// Defines the Localization Service, which is an easy access to operations involving Languages and Dictionary /// public interface ILocalizationService : IService { - + //Possible to-do list: + //Import DictionaryItem (?) + //RemoveByLanguage (translations) + //Add/Set Text (Insert/Update) + //Remove Text (in translation) + + /// + /// Gets a by its id + /// + /// Id of the + /// + IDictionaryItem GetDictionaryItemById(int id); + + /// + /// Gets a by its id + /// + /// Id of the + /// + IDictionaryItem GetDictionaryItemById(Guid id); + + /// + /// Gets a by its key + /// + /// Key of the + /// + IDictionaryItem GetDictionaryItemByKey(string key); + + /// + /// Gets a list of children for a + /// + /// Id of the parent + /// An enumerable list of objects + IEnumerable GetDictionaryItemChildren(Guid parentId); + + /// + /// Gets the root/top objects + /// + /// An enumerable list of objects + IEnumerable GetRootDictionaryItems(); + + /// + /// Checks if a with given key exists + /// + /// Key of the + /// True if a exists, otherwise false + bool DictionaryItemExists(string key); + + /// + /// Saves a object + /// + /// to save + void Save(IDictionaryItem dictionaryItem); + + /// + /// Deletes a object and its related translations + /// as well as its children. + /// + /// to delete + void Delete(IDictionaryItem dictionaryItem); + + /// + /// Gets a by its id + /// + /// Id of the + /// + ILanguage GetLanguageById(int id); + + /// + /// Gets a by its culture code + /// + /// Culture Code + /// + ILanguage GetLanguageByCultureCode(string culture); + + /// + /// Gets all available languages + /// + /// An enumerable list of objects + IEnumerable GetAllLanguages(); + + /// + /// Saves a object + /// + /// to save + void Save(ILanguage language); + + /// + /// Deletes a by removing it and its usages from the db + /// + /// to delete + void Delete(ILanguage language); } } \ No newline at end of file diff --git a/src/Umbraco.Web/Services/LocalizationService.cs b/src/Umbraco.Web/Services/LocalizationService.cs new file mode 100644 index 0000000000..c26b1fa839 --- /dev/null +++ b/src/Umbraco.Web/Services/LocalizationService.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Querying; +using Umbraco.Core.Persistence.Repositories; +using Umbraco.Core.Persistence.UnitOfWork; + +namespace Umbraco.Web.Services +{ + /// + /// Represents the Localization Service, which is an easy access to operations involving and + /// + public class LocalizationService : ILocalizationService + { + private static readonly Guid RootParentId = new Guid("41c7638d-f529-4bff-853e-59a0c2fb1bde"); + private readonly IUnitOfWorkProvider _provider; + + public LocalizationService() : this(new PetaPocoUnitOfWorkProvider()) + { + } + + public LocalizationService(IUnitOfWorkProvider provider) + { + _provider = provider; + } + + /// + /// Gets a by its id + /// + /// Id of the + /// + public IDictionaryItem GetDictionaryItemById(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + return repository.Get(id); + } + + /// + /// Gets a by its id + /// + /// Id of the + /// + public IDictionaryItem GetDictionaryItemById(Guid id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.Key == id); + var items = repository.GetByQuery(query); + + return items.FirstOrDefault(); + } + + /// + /// Gets a by its key + /// + /// Key of the + /// + public IDictionaryItem GetDictionaryItemByKey(string key) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ItemKey == key); + var items = repository.GetByQuery(query); + + return items.FirstOrDefault(); + } + + /// + /// Gets a list of children for a + /// + /// Id of the parent + /// An enumerable list of objects + public IEnumerable GetDictionaryItemChildren(Guid parentId) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == parentId); + var items = repository.GetByQuery(query); + + return items; + } + + /// + /// Gets the root/top objects + /// + /// An enumerable list of objects + public IEnumerable GetRootDictionaryItems() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == RootParentId); + var items = repository.GetByQuery(query); + + return items; + } + + /// + /// Checks if a with given key exists + /// + /// Key of the + /// True if a exists, otherwise false + public bool DictionaryItemExists(string key) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ItemKey == key); + var items = repository.GetByQuery(query); + + return items.Any(); + } + + /// + /// Saves a object + /// + /// to save + public void Save(IDictionaryItem dictionaryItem) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + repository.AddOrUpdate(dictionaryItem); + unitOfWork.Commit(); + } + + /// + /// Deletes a object and its related translations + /// as well as its children. + /// + /// to delete + public void Delete(IDictionaryItem dictionaryItem) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + //NOTE: The recursive delete is done in the repository + repository.Delete(dictionaryItem); + unitOfWork.Commit(); + } + + /// + /// Gets a by its id + /// + /// Id of the + /// + public ILanguage GetLanguageById(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + return repository.Get(id); + } + + /// + /// Gets a by its culture code + /// + /// Culture Code + /// + public ILanguage GetLanguageByCultureCode(string culture) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.CultureName == culture); + var items = repository.GetByQuery(query); + + return items.FirstOrDefault(); + } + + /// + /// Gets all available languages + /// + /// An enumerable list of objects + public IEnumerable GetAllLanguages() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + var languages = repository.GetAll(); + return languages; + } + + /// + /// Saves a object + /// + /// to save + public void Save(ILanguage language) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + repository.AddOrUpdate(language); + unitOfWork.Commit(); + } + + /// + /// Deletes a by removing it (but not its usages) from the db + /// + /// to delete + public void Delete(ILanguage language) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + //NOTE: There isn't any constraints in the db, so possible references aren't deleted + repository.Delete(language); + unitOfWork.Commit(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 830df26106..d2427ba96b 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -324,6 +324,7 @@ +