diff --git a/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs b/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs index b4b6ebf05f..46c67e762d 100644 --- a/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using Umbraco.Core.Models; using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.SqlSyntax; @@ -25,6 +26,7 @@ namespace Umbraco.Core.Persistence.Mappers CacheMap(src => src.Id, dto => dto.Id); CacheMap(src => src.RootContentId, dto => dto.RootStructureId); CacheMap(src => src.DefaultLanguageId, dto => dto.DefaultLanguage); + CacheMap(src => src.DomainName, dto => dto.DomainName); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs b/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs index a66a65e73a..711463ec6d 100644 --- a/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs @@ -118,6 +118,17 @@ namespace Umbraco.Core.Persistence.Repositories } } + public IDomain GetByName(string domainName) + { + using (var repo = new CachedDomainRepository(this, UnitOfWork, RepositoryCache, Logger, SqlSyntax)) + { + var factory = new DomainModelFactory(); + return factory.BuildDomainEntity( + repo.GetByQuery(new Query().Where(x => x.DomainName.InvariantEquals(domainName))).FirstOrDefault(), + _contentRepository, _languageRepository); + } + } + public bool Exists(string domainName) { using (var repo = new CachedDomainRepository(this, UnitOfWork, RepositoryCache, Logger, SqlSyntax, _mappingResolver)) @@ -248,7 +259,7 @@ namespace Umbraco.Core.Persistence.Repositories protected override void PersistNewItem(CacheableDomain entity) { - var exists = Database.ExecuteScalar("SELECT COUNT(*) FROM umbracoDomains WHERE domainName = @domainName", new {domainName = entity.DomainName}); + var exists = Database.ExecuteScalar("SELECT COUNT(*) FROM umbracoDomains WHERE domainName = @domainName", new { domainName = entity.DomainName }); if (exists > 0) throw new DuplicateNameException(string.Format("The domain name {0} is already assigned", entity.DomainName)); entity.AddingEntity(); diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDomainRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDomainRepository.cs index 030251d805..0371d70f2e 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDomainRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IDomainRepository.cs @@ -5,6 +5,7 @@ namespace Umbraco.Core.Persistence.Repositories { public interface IDomainRepository : IRepositoryQueryable { + IDomain GetByName(string domainName); bool Exists(string domainName); IEnumerable GetAll(bool includeWildcards); IEnumerable GetAssignedDomains(int contentId, bool includeWildcards); diff --git a/src/Umbraco.Core/Services/DomainService.cs b/src/Umbraco.Core/Services/DomainService.cs index 295bb23859..71429cfb59 100644 --- a/src/Umbraco.Core/Services/DomainService.cs +++ b/src/Umbraco.Core/Services/DomainService.cs @@ -47,7 +47,7 @@ namespace Umbraco.Core.Services var uow = UowProvider.GetUnitOfWork(); using (var repository = RepositoryFactory.CreateDomainRepository(uow)) { - return repository.GetByQuery(repository.Query.Where(x => x.DomainName.InvariantEquals(name))).FirstOrDefault(); + return repository.GetByName(name); } } diff --git a/src/Umbraco.Tests/Persistence/Repositories/DomainRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/DomainRepositoryTest.cs index 7905564a6a..0639353b8e 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/DomainRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/DomainRepositoryTest.cs @@ -8,6 +8,7 @@ using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; using Umbraco.Core.Models; +using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Tests.TestHelpers; @@ -207,6 +208,68 @@ namespace Umbraco.Tests.Persistence.Repositories } + [Test] + public void Exists() + { + var provider = new PetaPocoUnitOfWorkProvider(Logger); + var unitOfWork = provider.GetUnitOfWork(); + + ContentType ct; + var contentId = CreateTestData("en-AU", out ct); + + ContentRepository contentRepo; + LanguageRepository langRepo; + ContentTypeRepository contentTypeRepo; + + using (var repo = CreateRepository(unitOfWork, out contentTypeRepo, out contentRepo, out langRepo)) + { + var lang = langRepo.GetByIsoCode("en-AU"); + var content = contentRepo.Get(contentId); + + for (int i = 0; i < 10; i++) + { + var domain = (IDomain)new UmbracoDomain("test" + i + ".com") { RootContent = content, Language = lang }; + repo.AddOrUpdate(domain); + unitOfWork.Commit(); + } + + var found = repo.Exists("test1.com"); + + Assert.IsTrue(found); + } + } + + [Test] + public void Get_By_Name() + { + var provider = new PetaPocoUnitOfWorkProvider(Logger); + var unitOfWork = provider.GetUnitOfWork(); + + ContentType ct; + var contentId = CreateTestData("en-AU", out ct); + + ContentRepository contentRepo; + LanguageRepository langRepo; + ContentTypeRepository contentTypeRepo; + + using (var repo = CreateRepository(unitOfWork, out contentTypeRepo, out contentRepo, out langRepo)) + { + var lang = langRepo.GetByIsoCode("en-AU"); + var content = contentRepo.Get(contentId); + + for (int i = 0; i < 10; i++) + { + var domain = (IDomain)new UmbracoDomain("test" + i + ".com") { RootContent = content, Language = lang }; + repo.AddOrUpdate(domain); + unitOfWork.Commit(); + } + + var found = repo.GetByName("test1.com"); + + Assert.IsNotNull(found); + } + } + [Test] public void Get_All() { diff --git a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs index f818f7941e..42d5b72127 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs @@ -44,7 +44,8 @@ namespace Umbraco.Web.Routing //TODO: Is there a better way to extract this value? at least we're not relying on singletons here though pcr.RoutingContext.UmbracoContext.HttpContext.Request.ServerVariables["SERVER_NAME"], pcr.RoutingContext.UmbracoContext.Application.Services.EntityService, - new PublishedContentQuery(pcr.RoutingContext.UmbracoContext.ContentCache, pcr.RoutingContext.UmbracoContext.MediaCache)); + new PublishedContentQuery(pcr.RoutingContext.UmbracoContext.ContentCache, pcr.RoutingContext.UmbracoContext.MediaCache), + pcr.RoutingContext.UmbracoContext.Application.Services.DomainService); IPublishedContent content = null;