From 416dcdf90fa534067553f6e55d0f9288577cd384 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 13 May 2015 16:45:08 +1000 Subject: [PATCH] Updates NotFoundHandlerHelper to use IDomainService and fixes IDomainRepository with the Exists and GetByName methods (with tests) --- .../Persistence/Mappers/DomainMapper.cs | 2 + .../Repositories/DomainRepository.cs | 13 +++- .../Interfaces/IDomainRepository.cs | 1 + src/Umbraco.Core/Services/DomainService.cs | 2 +- .../Repositories/DomainRepositoryTest.cs | 63 +++++++++++++++++++ .../Routing/ContentFinderByLegacy404.cs | 3 +- .../Routing/NotFoundHandlerHelper.cs | 14 +++-- .../umbraco.presentation/NotFoundHandlers.cs | 3 +- 8 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs b/src/Umbraco.Core/Persistence/Mappers/DomainMapper.cs index 15d0852158..63ae627c62 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; @@ -24,6 +25,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 2adba8536a..0dec93e25e 100644 --- a/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/DomainRepository.cs @@ -115,6 +115,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)) @@ -245,7 +256,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 f2ff88e001..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(new 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 7fa4f8aef0..f394bafb1f 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 5acbbc5c38..22c4364891 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs @@ -27,7 +27,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; diff --git a/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs b/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs index a2af85943e..20441b6c11 100644 --- a/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs +++ b/src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs @@ -229,12 +229,14 @@ namespace Umbraco.Web.Routing /// /// /// + /// /// internal static int? GetCurrentNotFoundPageId( IContentErrorPage[] error404Collection, string requestServerName, - IEntityService entityService, - PublishedContentQuery publishedContentQuery) + IEntityService entityService, + ITypedPublishedContentQuery publishedContentQuery, + IDomainService domainService) { if (error404Collection.Count() > 1) { @@ -243,13 +245,13 @@ namespace Umbraco.Web.Routing //TODO: Remove the dependency on this legacy Domain service, // in 7.3 the real domain service should be passed in as a parameter. - if (Domain.Exists(requestServerName)) + if (domainService.Exists(requestServerName)) { - var d = Domain.GetDomain(requestServerName); + var d = domainService.GetByName(requestServerName); // test if a 404 page exists with current culture cultureErr = error404Collection - .FirstOrDefault(x => x.Culture == d.Language.CultureAlias); + .FirstOrDefault(x => x.Culture == d.Language.IsoCode); if (cultureErr != null) { @@ -289,7 +291,7 @@ namespace Umbraco.Web.Routing /// /// /// - internal static int? GetContentIdFromErrorPageConfig(IContentErrorPage errorPage, IEntityService entityService, PublishedContentQuery publishedContentQuery) + internal static int? GetContentIdFromErrorPageConfig(IContentErrorPage errorPage, IEntityService entityService, ITypedPublishedContentQuery publishedContentQuery) { if (errorPage.HasContentId) return errorPage.ContentId; diff --git a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs index 43aeefd970..27e62888d4 100644 --- a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs +++ b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs @@ -325,7 +325,8 @@ namespace umbraco { UmbracoConfig.For.UmbracoSettings().Content.Error404Collection.ToArray(), HttpContext.Current.Request.ServerVariables["SERVER_NAME"], ApplicationContext.Current.Services.EntityService, - new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache)); + new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache), + ApplicationContext.Current.Services.DomainService); if (error404.HasValue) {