Skip to content

Commit

Permalink
fix(DancingGoat): refactor conditional blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bkapustik committed Aug 26, 2024
1 parent 2d309c2 commit 0c712db
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 100 deletions.
71 changes: 34 additions & 37 deletions examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,48 @@ WebCrawlerService webCrawler

// IIndexEventItemModel could be a reusable content item or a web page item, so we use
// pattern matching to get access to the web page item specific type and fields
if (algoliaPageItem is IndexEventWebPageItemModel indexedPage)
if (algoliaPageItem is not IndexEventWebPageItemModel indexedPage)
{
if (string.Equals(algoliaPageItem.ContentTypeName, ArticlePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
return null;
}
if (string.Equals(algoliaPageItem.ContentTypeName, ArticlePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
{
// The implementation of GetPage<T>() is below
var page = await GetPage<ArticlePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
ArticlePage.CONTENT_TYPE_NAME);

if (page is null)
{
// The implementation of GetPage<T>() is below
var page = await GetPage<ArticlePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
ArticlePage.CONTENT_TYPE_NAME);

if (page is null)
{
return null;
}

resultProperties.SortableTitle = resultProperties.Title = page?.ArticleTitle ?? string.Empty;

string rawContent = await webCrawler.CrawlWebPage(page!);
resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent);
return null;
}
else if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))

resultProperties.SortableTitle = resultProperties.Title = page?.ArticleTitle ?? string.Empty;

string rawContent = await webCrawler.CrawlWebPage(page!);
resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent);
}
else if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
{
var page = await GetPage<HomePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
HomePage.CONTENT_TYPE_NAME);

if (page is null)
{
var page = await GetPage<HomePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
HomePage.CONTENT_TYPE_NAME);

if (page is null)
{
return null;
}

if (page.HomePageBanner.IsNullOrEmpty())
{
return null;
}

resultProperties.Title = page!.HomePageBanner.First().BannerHeaderText;
return null;
}
else

if (page.HomePageBanner.IsNullOrEmpty())
{
return null;
}

resultProperties.Title = page!.HomePageBanner.First().BannerHeaderText;
}
else
{
Expand Down
81 changes: 39 additions & 42 deletions examples/DancingGoat/Search/ReusableContentItemsIndexingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,54 +63,51 @@ WebCrawlerService webCrawler

// IIndexEventItemModel could be a reusable content item or a web page item, so we use
// pattern matching to get access to the web page item specific type and fields
if (algoliaPageItem is IndexEventReusableItemModel indexedItem)
if (algoliaPageItem is not IndexEventReusableItemModel indexedItem)
{
if (string.Equals(algoliaPageItem.ContentTypeName, Banner.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
return null;
}
if (string.Equals(algoliaPageItem.ContentTypeName, Banner.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
{
var query = new ContentItemQueryBuilder()
.ForContentType(HomePage.CONTENT_TYPE_NAME,
config =>
config
.WithLinkedItems(4)
// Because the changedItem is a reusable content item, we don't have a website channel name to use here
// so we use a hardcoded channel name.
.ForWebsite(INDEXED_WEBSITECHANNEL_NAME)
// Retrieves all HomePages that link to the Banner through the HomePage.HomePageBanner field
.Linking(nameof(HomePage.HomePageBanner), new[] { indexedItem.ItemID }))
.InLanguage(indexedItem.LanguageName);

var associatedWebPageItem = (await queryExecutor.GetWebPageResult(query, webPageMapper.Map<HomePage>)).First();
string url = string.Empty;
try
{
var query = new ContentItemQueryBuilder()
.ForContentType(HomePage.CONTENT_TYPE_NAME,
config =>
config
.WithLinkedItems(4)
// Because the changedItem is a reusable content item, we don't have a website channel name to use here
// so we use a hardcoded channel name.
.ForWebsite(INDEXED_WEBSITECHANNEL_NAME)
// Retrieves all HomePages that link to the Banner through the HomePage.HomePageBanner field
.Linking(nameof(HomePage.HomePageBanner), new[] { indexedItem.ItemID }))
.InLanguage(indexedItem.LanguageName);

var associatedWebPageItem = (await queryExecutor.GetWebPageResult(query, webPageMapper.Map<HomePage>)).First();
string url = string.Empty;
try
{
url = (await urlRetriever.Retrieve(associatedWebPageItem.SystemFields.WebPageItemTreePath,
INDEXED_WEBSITECHANNEL_NAME, indexedItem.LanguageName)).RelativePath;
}
catch (Exception)
{
// Retrieve can throw an exception when processing a page update LuceneQueueItem
// and the page was deleted before the update task has processed. In this case, return no item.
return null;
}

//If the indexed item is a reusable content item, we need to set the url manually.
resultProperties.Url = url;
resultProperties.SortableTitle = resultProperties.Title = associatedWebPageItem!.HomePageBanner.First().BannerText;
string rawContent = await webCrawler.CrawlWebPage(associatedWebPageItem!);
resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent);

//If the indexed item is a reusable content item, we need to set the url manually.
var result = new List<JObject>()
{
AssignProperties(resultProperties)
};

return result;
url = (await urlRetriever.Retrieve(associatedWebPageItem.SystemFields.WebPageItemTreePath,
INDEXED_WEBSITECHANNEL_NAME, indexedItem.LanguageName)).RelativePath;
}
else
catch (Exception)
{
// Retrieve can throw an exception when processing a page update LuceneQueueItem
// and the page was deleted before the update task has processed. In this case, return no item.
return null;
}

//If the indexed item is a reusable content item, we need to set the url manually.
resultProperties.Url = url;
resultProperties.SortableTitle = resultProperties.Title = associatedWebPageItem!.HomePageBanner.First().BannerText;
string rawContent = await webCrawler.CrawlWebPage(associatedWebPageItem!);
resultProperties.Content = htmlSanitizer.SanitizeHtmlDocument(rawContent);

//If the indexed item is a reusable content item, we need to set the url manually.
var result = new List<JObject>()
{
AssignProperties(resultProperties)
};

return result;
}
else
{
Expand Down
39 changes: 18 additions & 21 deletions examples/DancingGoat/Search/SimpleSearchIndexingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,33 @@ public override IndexSettings GetAlgoliaIndexSettings() =>
{
var result = new List<JObject>();

string title = string.Empty;
string title;

// IIndexEventItemModel could be a reusable content item or a web page item, so we use
// pattern matching to get access to the web page item specific type and fields
if (algoliaPageItem is IndexEventWebPageItemModel indexedPage)
if (algoliaPageItem is not IndexEventWebPageItemModel indexedPage)
{
if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
return null;
}
if (string.Equals(algoliaPageItem.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
{
var page = await GetPage<HomePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
HomePage.CONTENT_TYPE_NAME);

if (page is null)
{
var page = await GetPage<HomePage>(
indexedPage.ItemGuid,
indexedPage.WebsiteChannelName,
indexedPage.LanguageName,
HomePage.CONTENT_TYPE_NAME);

if (page is null)
{
return null;
}

if (page.HomePageBanner.IsNullOrEmpty())
{
return null;
}

title = page!.HomePageBanner.First().BannerHeaderText;
return null;
}
else

if (page.HomePageBanner.IsNullOrEmpty())
{
return null;
}

title = page!.HomePageBanner.First().BannerHeaderText;
}
else
{
Expand Down

0 comments on commit 0c712db

Please sign in to comment.