Skip to content

Commit

Permalink
refactor(DancingGoat): conditional blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bkapustik committed Aug 26, 2024
1 parent 055dcce commit dd9edb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
78 changes: 38 additions & 40 deletions examples/DancingGoat/Search/AdvancedSearchIndexingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,54 +49,52 @@ 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 (item is IndexEventWebPageItemModel indexedPage)
if (item is not IndexEventWebPageItemModel indexedPage)
{
if (string.Equals(item.ContentTypeName, ArticlePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
return null;
}

if (string.Equals(item.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;
}

sortableTitle = title = page?.ArticleTitle ?? string.Empty;

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

sortableTitle = title = page?.ArticleTitle ?? string.Empty;

string rawContent = await webCrawler.CrawlWebPage(page!);
content = htmlSanitizer.SanitizeHtmlDocument(rawContent);
}
else if (string.Equals(item.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;
}

sortableTitle = title = page!.HomePageBanner.First().BannerText;

string rawContent = await webCrawler.CrawlWebPage(page!);
content = htmlSanitizer.SanitizeHtmlDocument(rawContent);
return null;
}
else

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

sortableTitle = title = page!.HomePageBanner.First().BannerText;

string rawContent = await webCrawler.CrawlWebPage(page!);
content = htmlSanitizer.SanitizeHtmlDocument(rawContent);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public LuceneSearchResultModel<DancingGoatSearchResultModel> GlobalSearch(
return new LuceneSearchResultModel<DancingGoatSearchResultModel>
{
Query = searchText ?? "",
Query = searchText ?? string.Empty,
Page = page,
PageSize = pageSize,
TotalPages = topDocs.TotalHits <= 0 ? 0 : ((topDocs.TotalHits - 1) / pageSize) + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public virtual string SanitizeHtmlDocument(string htmlContent)
textContent = HTMLHelper.RegexHtmlToTextWhiteSpace.Replace(textContent, " ");
textContent = textContent.Trim();

string title = doc.Head?.QuerySelector("title")?.TextContent ?? "";
string description = doc.Head?.QuerySelector("meta[name='description']")?.GetAttribute("content") ?? "";
string title = doc.Head?.QuerySelector("title")?.TextContent ?? string.Empty;
string description = doc.Head?.QuerySelector("meta[name='description']")?.GetAttribute("content") ?? string.Empty;

return string.Join(
" ",
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 @@ -29,36 +29,33 @@ IContentQueryExecutor queryExecutor
{
var document = new Document();

string title = "";
string title = string.Empty;

// 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 (item is IndexEventWebPageItemModel indexedPage)
if (item is not IndexEventWebPageItemModel indexedPage)
{
if (string.Equals(item.ContentTypeName, HomePage.CONTENT_TYPE_NAME, StringComparison.OrdinalIgnoreCase))
return null;
}
if (string.Equals(item.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 dd9edb3

Please sign in to comment.