From 5bf33595b745d7e3ece2e27a32039cd21b6697f2 Mon Sep 17 00:00:00 2001 From: KamilKranczuk Date: Thu, 10 Aug 2023 08:15:45 +0200 Subject: [PATCH 1/3] Fix returning null as content usage url --- .../Api/Services/ContentUsageService.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs index 97a13d7..c1a0eb5 100644 --- a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs +++ b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using EPiServer; using EPiServer.Core; @@ -37,12 +38,26 @@ public IEnumerable GetPageUrls(EPiServerContentUsage contentUsage) if (!string.IsNullOrEmpty(url)) return CheckIsPublished(contentLink) ? new[] { url } : new string[] { }; - var pageLinks = _contentSoftLinkRepository.Load(contentLink, true) + var pageUrls = this.SearchForPageUrls(contentLink, contentUsage.LanguageBranch); + + return pageUrls; + } + + private IEnumerable SearchForPageUrls(ContentReference contentLink, string languageBranch) + { + var softLinks = this._contentSoftLinkRepository.Load(contentLink, true); + var pageLinks = softLinks .Where(softLink => softLink.SoftLinkType == ReferenceType.PageLinkReference) .Select(softLink => softLink.OwnerContentLink) - .Where(ownerContentLink => ownerContentLink != null && CheckIsPublished(ownerContentLink)); + .Where(ownerContentLink => ownerContentLink != null && this.CheckIsPublished(ownerContentLink)); - return pageLinks.Select(pageLink => _urlResolver.GetUrl(pageLink, contentUsage.LanguageBranch)); + var pageUrls = pageLinks.SelectMany(pageLink => + { + var contentUrl = this._urlResolver.GetUrl(pageLink, languageBranch); + return string.IsNullOrEmpty(contentUrl) ? this.SearchForPageUrls(pageLink, languageBranch) : new[] { contentUrl }; + }); + + return pageUrls; } public string GetEditUrl(EPiServerContentUsage contentUsage) From 4f52d4d1d289b471f0981632e4fee64bbb783d2f Mon Sep 17 00:00:00 2001 From: KamilKranczuk Date: Fri, 11 Aug 2023 07:23:15 +0200 Subject: [PATCH 2/3] Remove this qualifiers --- .../ContentType/ContentTypeUsagesRepository.cs | 6 +++--- .../Api/Services/ContentTypeService.cs | 6 +++--- .../Api/Services/ContentUsageService.cs | 13 ++++++------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Forte.Optimizely.ContentUsage/Api/Features/ContentType/ContentTypeUsagesRepository.cs b/src/Forte.Optimizely.ContentUsage/Api/Features/ContentType/ContentTypeUsagesRepository.cs index abcb5ee..28a5ae7 100644 --- a/src/Forte.Optimizely.ContentUsage/Api/Features/ContentType/ContentTypeUsagesRepository.cs +++ b/src/Forte.Optimizely.ContentUsage/Api/Features/ContentType/ContentTypeUsagesRepository.cs @@ -9,17 +9,17 @@ namespace Forte.Optimizely.ContentUsage.Api.Features.ContentType; public class ContentTypeUsagesRepository { - private readonly ServiceAccessor dataExecutorAccessor; + private readonly ServiceAccessor _dataExecutorAccessor; public ContentTypeUsagesRepository(ServiceAccessor dataExecutorAccessor) { - this.dataExecutorAccessor = dataExecutorAccessor; + _dataExecutorAccessor = dataExecutorAccessor; } public async Task> ListContentTypesUsagesCounters( CancellationToken cancellationToken) { - var executor = this.dataExecutorAccessor(); + var executor = _dataExecutorAccessor(); return await executor.ExecuteAsync((Func>>)(async () => { var command1 = executor.CreateCommand(); diff --git a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentTypeService.cs b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentTypeService.cs index c18f6ea..2ab222b 100644 --- a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentTypeService.cs +++ b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentTypeService.cs @@ -11,13 +11,13 @@ namespace Forte.Optimizely.ContentUsage.Api.Services; public class ContentTypeService { private readonly IContentTypeRepository _contentTypeRepository; - private readonly ContentTypeUsagesRepository contentTypeUsagesRepository; + private readonly ContentTypeUsagesRepository _contentTypeUsagesRepository; public ContentTypeService(IContentTypeRepository contentTypeRepository, ContentTypeUsagesRepository contentTypeUsagesRepository) { _contentTypeRepository = contentTypeRepository; - this.contentTypeUsagesRepository = contentTypeUsagesRepository; + _contentTypeUsagesRepository = contentTypeUsagesRepository; } public IEnumerable GetAll(ContentTypesFilterCriteria? filterCriteria) @@ -34,7 +34,7 @@ public IEnumerable GetAll(ContentTypesFilterCriteria? filterCriteri public async Task> GetAllCounters(CancellationToken cancellationToken) { - return await contentTypeUsagesRepository.ListContentTypesUsagesCounters(cancellationToken); + return await _contentTypeUsagesRepository.ListContentTypesUsagesCounters(cancellationToken); } public ContentType? Get(Guid guid) diff --git a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs index c1a0eb5..0fc56ae 100644 --- a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs +++ b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using EPiServer; using EPiServer.Core; @@ -38,23 +37,23 @@ public IEnumerable GetPageUrls(EPiServerContentUsage contentUsage) if (!string.IsNullOrEmpty(url)) return CheckIsPublished(contentLink) ? new[] { url } : new string[] { }; - var pageUrls = this.SearchForPageUrls(contentLink, contentUsage.LanguageBranch); + var pageUrls = SearchForPageUrls(contentLink, contentUsage.LanguageBranch); return pageUrls; } private IEnumerable SearchForPageUrls(ContentReference contentLink, string languageBranch) { - var softLinks = this._contentSoftLinkRepository.Load(contentLink, true); + var softLinks = _contentSoftLinkRepository.Load(contentLink, true); var pageLinks = softLinks .Where(softLink => softLink.SoftLinkType == ReferenceType.PageLinkReference) .Select(softLink => softLink.OwnerContentLink) - .Where(ownerContentLink => ownerContentLink != null && this.CheckIsPublished(ownerContentLink)); + .Where(ownerContentLink => ownerContentLink != null && CheckIsPublished(ownerContentLink)); var pageUrls = pageLinks.SelectMany(pageLink => { - var contentUrl = this._urlResolver.GetUrl(pageLink, languageBranch); - return string.IsNullOrEmpty(contentUrl) ? this.SearchForPageUrls(pageLink, languageBranch) : new[] { contentUrl }; + var contentUrl = _urlResolver.GetUrl(pageLink, languageBranch); + return string.IsNullOrEmpty(contentUrl) ? SearchForPageUrls(pageLink, languageBranch) : new[] { contentUrl }; }); return pageUrls; From 53e1906e3f013b04fe53351ca312691249205749 Mon Sep 17 00:00:00 2001 From: KamilKranczuk Date: Fri, 11 Aug 2023 07:30:26 +0200 Subject: [PATCH 3/3] Refactor searching for page urls --- .../Api/Services/ContentUsageService.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs index 0fc56ae..a58b8f1 100644 --- a/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs +++ b/src/Forte.Optimizely.ContentUsage/Api/Services/ContentUsageService.cs @@ -32,11 +32,7 @@ public ContentUsageService(IUrlResolver urlResolver, IContentSoftLinkRepository public IEnumerable GetPageUrls(EPiServerContentUsage contentUsage) { var contentLink = contentUsage.ContentLink; - var url = _urlResolver.GetUrl(contentLink, contentUsage.LanguageBranch); - - if (!string.IsNullOrEmpty(url)) - return CheckIsPublished(contentLink) ? new[] { url } : new string[] { }; - + var pageUrls = SearchForPageUrls(contentLink, contentUsage.LanguageBranch); return pageUrls; @@ -44,17 +40,18 @@ public IEnumerable GetPageUrls(EPiServerContentUsage contentUsage) private IEnumerable SearchForPageUrls(ContentReference contentLink, string languageBranch) { + var url = _urlResolver.GetUrl(contentLink, languageBranch); + + if (!string.IsNullOrEmpty(url)) + return CheckIsPublished(contentLink) ? new[] { url } : new string[] { }; + var softLinks = _contentSoftLinkRepository.Load(contentLink, true); var pageLinks = softLinks .Where(softLink => softLink.SoftLinkType == ReferenceType.PageLinkReference) .Select(softLink => softLink.OwnerContentLink) .Where(ownerContentLink => ownerContentLink != null && CheckIsPublished(ownerContentLink)); - var pageUrls = pageLinks.SelectMany(pageLink => - { - var contentUrl = _urlResolver.GetUrl(pageLink, languageBranch); - return string.IsNullOrEmpty(contentUrl) ? SearchForPageUrls(pageLink, languageBranch) : new[] { contentUrl }; - }); + var pageUrls = pageLinks.SelectMany(pageLink => SearchForPageUrls(pageLink, languageBranch)); return pageUrls; }