Skip to content

Commit

Permalink
Merge pull request #5455 from dfe-analytical-services/EES-5656_Releas…
Browse files Browse the repository at this point in the history
…e_Series_Change

EES-5656 Allow custom ordering of a publication's latest release when reordering releases
  • Loading branch information
benoutram authored Dec 20, 2024
2 parents 8c58d8b + cf1747a commit 2f093cc
Show file tree
Hide file tree
Showing 59 changed files with 2,197 additions and 2,077 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using GovUk.Education.ExploreEducationStatistics.Content.Model;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Database;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Extensions;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Repository;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Tests.Fixtures;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -26,10 +27,6 @@
using static GovUk.Education.ExploreEducationStatistics.Common.Model.TimeIdentifier;
using static GovUk.Education.ExploreEducationStatistics.Common.Services.CollectionUtils;
using HtmlBlockViewModel = GovUk.Education.ExploreEducationStatistics.Admin.ViewModels.HtmlBlockViewModel;
using IReleaseVersionRepository =
GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.Interfaces.IReleaseVersionRepository;
using ReleaseVersionRepository =
GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.ReleaseVersionRepository;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Tests.Services.ManageContent
{
Expand Down Expand Up @@ -327,12 +324,6 @@ public async Task GetManageContentPageViewModel()
Assert.Equal(publication.ReleaseSeries[2].LegacyLinkUrl,
contentPublicationReleaseSeries[2].LegacyLinkUrl);

var contentPublicationReleases = contentPublication.Releases;
Assert.Single(contentPublicationReleases);
Assert.Equal(otherReleaseVersion.Id, contentPublicationReleases[0].Id);
Assert.Equal(otherReleaseVersion.Slug, contentPublicationReleases[0].Slug);
Assert.Equal(otherReleaseVersion.Title, contentPublicationReleases[0].Title);

Assert.Equal(2, contentPublication.Methodologies.Count);
Assert.Equal(methodology.Versions[0].Id, contentPublication.Methodologies[0].Id);
Assert.Equal(methodology.Versions[0].Title, contentPublication.Methodologies[0].Title);
Expand Down Expand Up @@ -678,7 +669,7 @@ private static ManageContentPageService SetupManageContentPageService(
IDataBlockService? dataBlockService = null,
IMethodologyVersionRepository? methodologyVersionRepository = null,
IReleaseFileService? releaseFileService = null,
IReleaseVersionRepository? releaseVersionRepository = null,
IReleaseRepository? releaseRepository = null,
IUserService? userService = null)
{
return new(
Expand All @@ -688,7 +679,7 @@ private static ManageContentPageService SetupManageContentPageService(
dataBlockService ?? new Mock<IDataBlockService>().Object,
methodologyVersionRepository ?? new Mock<IMethodologyVersionRepository>().Object,
releaseFileService ?? new Mock<IReleaseFileService>().Object,
releaseVersionRepository ?? new ReleaseVersionRepository(contentDbContext),
releaseRepository ?? new ReleaseRepository(contentDbContext),
userService ?? MockUtils.AlwaysTrueUserService().Object
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ private static PublicationService BuildPublicationService(
IReleaseVersionRepository? releaseVersionRepository = null,
IMethodologyService? methodologyService = null,
IPublicationCacheService? publicationCacheService = null,
IReleaseCacheService? releaseCacheService = null,
IMethodologyCacheService? methodologyCacheService = null,
IRedirectsCacheService? redirectsCacheService = null)
{
Expand All @@ -641,6 +642,7 @@ private static PublicationService BuildPublicationService(
releaseVersionRepository ?? Mock.Of<IReleaseVersionRepository>(Strict),
methodologyService ?? Mock.Of<IMethodologyService>(Strict),
publicationCacheService ?? Mock.Of<IPublicationCacheService>(Strict),
releaseCacheService ?? Mock.Of<IReleaseCacheService>(Strict),
methodologyCacheService ?? Mock.Of<IMethodologyCacheService>(Strict),
redirectsCacheService ?? Mock.Of<IRedirectsCacheService>(Strict));
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ public MappingProfiles()
Title = rv.Publication.Title,
Slug = rv.Publication.Slug,
Contact = rv.Publication.Contact,
Releases = rv.Publication.ReleaseVersions
.FindAll(otherReleaseVersion => rv.Id != otherReleaseVersion.Id &&
IsLatestVersionOfRelease(rv.Publication.ReleaseVersions, otherReleaseVersion.Id))
.OrderByDescending(otherReleaseVersion => otherReleaseVersion.Year)
.ThenByDescending(otherReleaseVersion => otherReleaseVersion.TimePeriodCoverage)
.Select(otherReleaseVersion => new PreviousReleaseViewModel
{
Id = otherReleaseVersion.Id,
Slug = otherReleaseVersion.Slug,
Title = otherReleaseVersion.Title,
})
.ToList(),
ReleaseSeries = new List<ReleaseSeriesItemViewModel>(), // Must be hydrated after mapping
ExternalMethodology = rv.Publication.ExternalMethodology != null
? new ExternalMethodology
Expand Down Expand Up @@ -225,10 +213,5 @@ private void CreateContentBlockMap()

CreateMap<MarkDownBlock, MarkDownBlockViewModel>();
}

private static bool IsLatestVersionOfRelease(IEnumerable<ReleaseVersion> releaseVersions, Guid releaseVersionId)
{
return !releaseVersions.Any(rv => rv.PreviousVersionId == releaseVersionId && rv.Id != releaseVersionId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System.Linq;
using System.Threading.Tasks;
using static GovUk.Education.ExploreEducationStatistics.Common.Model.FileType;
using IReleaseVersionRepository = GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.Interfaces.IReleaseVersionRepository;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Services.ManageContent
{
Expand All @@ -31,7 +30,7 @@ public class ManageContentPageService : IManageContentPageService
private readonly IDataBlockService _dataBlockService;
private readonly IMethodologyVersionRepository _methodologyVersionRepository;
private readonly IReleaseFileService _releaseFileService;
private readonly IReleaseVersionRepository _releaseVersionRepository;
private readonly IReleaseRepository _releaseRepository;
private readonly IUserService _userService;

public ManageContentPageService(
Expand All @@ -41,7 +40,7 @@ public ManageContentPageService(
IDataBlockService dataBlockService,
IMethodologyVersionRepository methodologyVersionRepository,
IReleaseFileService releaseFileService,
IReleaseVersionRepository releaseVersionRepository,
IReleaseRepository releaseRepository,
IUserService userService)
{
_contentDbContext = contentDbContext;
Expand All @@ -50,7 +49,7 @@ public ManageContentPageService(
_dataBlockService = dataBlockService;
_methodologyVersionRepository = methodologyVersionRepository;
_releaseFileService = releaseFileService;
_releaseVersionRepository = releaseVersionRepository;
_releaseRepository = releaseRepository;
_userService = userService;
}

Expand Down Expand Up @@ -101,38 +100,12 @@ public async Task<Either<ActionResult, ManageContentPageViewModel>> GetManageCon

var releaseViewModel = _mapper.Map<ManageContentPageViewModel.ReleaseViewModel>(releaseVersion);

// Hydrate Publication.ReleaseSeries
var publishedVersions =
await _releaseVersionRepository
.ListLatestPublishedReleaseVersions(releaseVersion.PublicationId);
var filteredReleaseSeries = releaseVersion.Publication.ReleaseSeries
.Where(rsi => // only show items for legacy links and published releases
rsi.IsLegacyLink
|| publishedVersions
.Any(rv => rsi.ReleaseId == rv.ReleaseId)
).ToList();
releaseViewModel.Publication.ReleaseSeries = filteredReleaseSeries
.Select(rsi =>
{
if (rsi.IsLegacyLink)
{
return new ReleaseSeriesItemViewModel
{
Description = rsi.LegacyLinkDescription!,
LegacyLinkUrl = rsi.LegacyLinkUrl,
};
}

var latestReleaseVersion = publishedVersions
.Single(rv => rv.ReleaseId == rsi.ReleaseId);
var publishedReleases =
await _releaseRepository.ListPublishedReleases(releaseVersion.PublicationId);

return new ReleaseSeriesItemViewModel
{
Description = latestReleaseVersion.Title,
ReleaseId = latestReleaseVersion.ReleaseId,
ReleaseSlug = latestReleaseVersion.Slug,
};
}).ToList();
// Hydrate Publication.ReleaseSeries
releaseViewModel.Publication.ReleaseSeries =
BuildReleaseSeriesItemViewModels(releaseVersion.Publication, publishedReleases);

releaseViewModel.DownloadFiles = files.ToList();
releaseViewModel.Publication.Methodologies =
Expand All @@ -146,6 +119,37 @@ await _releaseVersionRepository
});
}

private static List<ReleaseSeriesItemViewModel> BuildReleaseSeriesItemViewModels(
Publication publication,
List<Release> publishedReleases)
{
var publishedReleasesById = publishedReleases.ToDictionary(r => r.Id);
return publication.ReleaseSeries
// Only include release series items for legacy links and published releases
.Where(rsi => rsi.IsLegacyLink || publishedReleasesById.ContainsKey(rsi.ReleaseId!.Value))
.Select(rsi =>
{
if (rsi.IsLegacyLink)
{
return new ReleaseSeriesItemViewModel
{
Description = rsi.LegacyLinkDescription!,
LegacyLinkUrl = rsi.LegacyLinkUrl
};
}

var release = publishedReleasesById[rsi.ReleaseId!.Value];

return new ReleaseSeriesItemViewModel
{
Description = release.Title,
ReleaseId = release.Id,
ReleaseSlug = release.Slug
};
})
.ToList();
}

private IQueryable<ReleaseVersion> HydrateReleaseQuery(IQueryable<ReleaseVersion> queryable)
{
// Using `AsSplitQuery` as the generated SQL without it is incredibly
Expand Down
Loading

0 comments on commit 2f093cc

Please sign in to comment.