-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split page queries
- Loading branch information
Showing
19 changed files
with
678 additions
and
291 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Dfe.PlanTech.Application/Content/Queries/GetButtonWithEntryReferencesQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using AutoMapper; | ||
using Dfe.PlanTech.Application.Persistence.Interfaces; | ||
using Dfe.PlanTech.Domain.Content.Interfaces; | ||
using Dfe.PlanTech.Domain.Content.Models; | ||
using Dfe.PlanTech.Domain.Content.Models.Buttons; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dfe.PlanTech.Application.Content.Queries; | ||
|
||
public class GetButtonWithEntryReferencesQuery : IGetPageChildrenQuery | ||
{ | ||
private readonly ICmsDbContext _db; | ||
private readonly ILogger<GetButtonWithEntryReferencesQuery> _logger; | ||
|
||
public GetButtonWithEntryReferencesQuery(ICmsDbContext db, ILogger<GetButtonWithEntryReferencesQuery> logger) | ||
{ | ||
_db = db; | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// If the Page.Content has any <see cref="ButtonWithEntryReferenceDbEntity"/>s, load the link to entry reference from the database for it | ||
/// </summary> | ||
/// <param name="page"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task TryLoadChildren(PageDbEntity page, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
var buttons = page.Content.Exists(content => content is ButtonWithEntryReferenceDbEntity); | ||
|
||
if (!buttons) return; | ||
|
||
var buttonQuery = ButtonWithEntryReferencesQueryable(page); | ||
|
||
await _db.ToListAsync(buttonQuery, cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error loading button references for {page}", page.Id); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Quer to get <see cref="ButtonWithEntryReferenceDbEntity">s for the given page, but with only necessary information we require | ||
/// </summary> | ||
/// <param name="page"></param> | ||
/// <returns></returns> | ||
private IQueryable<ButtonWithEntryReferenceDbEntity> ButtonWithEntryReferencesQueryable(PageDbEntity page) | ||
=> _db.ButtonWithEntryReferences.Where(button => button.ContentPages.Any(contentPage => contentPage.Id == page.Id)) | ||
.Select(button => new ButtonWithEntryReferenceDbEntity() | ||
{ | ||
Id = button.Id, | ||
LinkToEntry = new PageDbEntity() | ||
{ | ||
Slug = ((IHasSlug)button.LinkToEntry).Slug | ||
} | ||
}); | ||
} |
102 changes: 102 additions & 0 deletions
102
src/Dfe.PlanTech.Application/Content/Queries/GetCategorySectionsQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Dfe.PlanTech.Application.Persistence.Interfaces; | ||
using Dfe.PlanTech.Domain.Content.Interfaces; | ||
using Dfe.PlanTech.Domain.Content.Models; | ||
using Dfe.PlanTech.Domain.Questionnaire.Models; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dfe.PlanTech.Application.Content.Queries; | ||
|
||
public class GetCategorySectionsQuery : IGetPageChildrenQuery | ||
{ | ||
private readonly ICmsDbContext _db; | ||
private readonly ILogger<GetCategorySectionsQuery> _logger; | ||
|
||
public GetCategorySectionsQuery(ICmsDbContext db, ILogger<GetCategorySectionsQuery> logger) | ||
{ | ||
_db = db; | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// If there are any "Category" components in the Page.Content, then load the required Section information for each one. | ||
/// </summary> | ||
/// <param name="page"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task TryLoadChildren(PageDbEntity page, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
var pageHasCategories = page.Content.Exists(content => content is CategoryDbEntity); | ||
|
||
if (!pageHasCategories) return; | ||
|
||
var sections = await _db.ToListAsync(SectionsForPageQueryable(page), cancellationToken); | ||
|
||
CopySectionsToPage(page, sections); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error fetching categories for {page}", page.Id); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Copies the retrieved Sections from the database over the corresponding Category's section | ||
/// </summary> | ||
/// <param name="page">Page that contains categories</param> | ||
/// <param name="sections">"Complete" section from database</param> | ||
private static void CopySectionsToPage(PageDbEntity page, List<SectionDbEntity> sections) | ||
{ | ||
var sectionsGroupedByCategory = sections.GroupBy(section => section.CategoryId); | ||
|
||
foreach (var cat in sectionsGroupedByCategory) | ||
{ | ||
var matching = page.Content.OfType<CategoryDbEntity>() | ||
.FirstOrDefault(category => category != null && category.Id == cat.Key); | ||
|
||
if (matching == null) | ||
{ | ||
continue; | ||
} | ||
|
||
matching.Sections = cat.ToList(); | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Quer to get <see cref="SectionDbEntity">s for the given page, but with only necessary information we require | ||
/// </summary> | ||
/// <param name="page"></param> | ||
/// <returns></returns> | ||
private IQueryable<SectionDbEntity> SectionsForPageQueryable(PageDbEntity page) | ||
=> _db.Sections.Where(section => section.Category != null && section.Category.ContentPages.Any(categoryPage => categoryPage.Slug == page.Slug)) | ||
.Select(section => new SectionDbEntity() | ||
{ | ||
CategoryId = section.CategoryId, | ||
Id = section.Id, | ||
Name = section.Name, | ||
Questions = section.Questions.Select(question => new QuestionDbEntity() | ||
{ | ||
Slug = question.Slug, | ||
Id = question.Id, | ||
}).ToList(), | ||
Recommendations = section.Recommendations.Select(recommendation => new RecommendationPageDbEntity() | ||
{ | ||
DisplayName = recommendation.DisplayName, | ||
Maturity = recommendation.Maturity, | ||
Page = new PageDbEntity() | ||
{ | ||
Slug = recommendation.Page.Slug | ||
}, | ||
Id = recommendation.Id | ||
}).ToList(), | ||
InterstitialPage = section.InterstitialPage == null ? null : new PageDbEntity() | ||
{ | ||
Slug = section.InterstitialPage.Slug, | ||
Id = section.InterstitialPage.Id | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.