-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feed endpoint filtering on persistentLocalId
- Loading branch information
1 parent
5809ce4
commit 2cdb061
Showing
5 changed files
with
306 additions
and
59 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
src/AddressRegistry.Api.Legacy/Address/Sync/AddressSyndicationBaseHandler.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,74 @@ | ||
namespace AddressRegistry.Api.Legacy.Address.Sync | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using Be.Vlaanderen.Basisregisters.Api.Search.Pagination; | ||
using Be.Vlaanderen.Basisregisters.Api.Syndication; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Common; | ||
using Infrastructure; | ||
using Infrastructure.Options; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.SyndicationFeed; | ||
using Microsoft.SyndicationFeed.Atom; | ||
|
||
public class AddressSyndicationBaseHandler | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly IOptions<ResponseOptions> _responseOptions; | ||
|
||
public AddressSyndicationBaseHandler(IConfiguration configuration, IOptions<ResponseOptions> responseOptions) | ||
{ | ||
_configuration = configuration; | ||
_responseOptions = responseOptions; | ||
} | ||
|
||
protected async Task<string> BuildAtomFeed( | ||
DateTimeOffset lastFeedUpdate, | ||
PagedQueryable<AddressSyndicationQueryResult> pagedAddresses) | ||
{ | ||
var sw = new StringWriterWithEncoding(Encoding.UTF8); | ||
|
||
await using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings { Async = true, Indent = true, Encoding = sw.Encoding })) | ||
{ | ||
var formatter = new AtomFormatter(null, xmlWriter.Settings) { UseCDATA = true }; | ||
var writer = new AtomFeedWriter(xmlWriter, null, formatter); | ||
var syndicationConfiguration = _configuration.GetSection("Syndication"); | ||
var atomFeedConfig = AtomFeedConfigurationBuilder.CreateFrom(syndicationConfiguration, lastFeedUpdate); | ||
|
||
await writer.WriteDefaultMetadata(atomFeedConfig); | ||
|
||
var addresses = pagedAddresses.Items.ToList(); | ||
|
||
var nextFrom = addresses.Any() | ||
? addresses.Max(x => x.Position) + 1 | ||
: (long?)null; | ||
|
||
var nextUri = BuildNextSyncUri(pagedAddresses.PaginationInfo.Limit, nextFrom, syndicationConfiguration["NextUri"]); | ||
if (nextUri != null) | ||
{ | ||
await writer.Write(new SyndicationLink(nextUri, "next")); | ||
} | ||
|
||
foreach (var address in addresses) | ||
{ | ||
await writer.WriteAddress(_responseOptions, formatter, syndicationConfiguration["Category"], address); | ||
} | ||
|
||
xmlWriter.Flush(); | ||
} | ||
|
||
return sw.ToString(); | ||
} | ||
|
||
private static Uri BuildNextSyncUri(int limit, long? from, string nextUrlBase) | ||
{ | ||
return from.HasValue | ||
? new Uri(string.Format(nextUrlBase, from, limit)) | ||
: null; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/AddressRegistry.Api.Legacy/Address/Sync/AddressSyndicationByPersistentLocalIdHandler.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,50 @@ | ||
namespace AddressRegistry.Api.Legacy.Address.Sync | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Be.Vlaanderen.Basisregisters.Api.Search.Filtering; | ||
using Be.Vlaanderen.Basisregisters.Api.Search.Pagination; | ||
using Be.Vlaanderen.Basisregisters.Api.Search.Sorting; | ||
using Infrastructure.Options; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using Projections.Legacy; | ||
|
||
public record SyndicationByPersistentLocalIdRequest( | ||
FilteringHeader<AddressSyndicationPersistentLocalIdFilter> Filtering, | ||
SortingHeader Sorting, | ||
IPaginationRequest Pagination) | ||
: IRequest<SyndicationAtomContent>; | ||
|
||
public sealed class AddressSyndicationByPersistentLocalIdHandler : AddressSyndicationBaseHandler, IRequestHandler<SyndicationByPersistentLocalIdRequest, SyndicationAtomContent> | ||
{ | ||
private readonly LegacyContext _legacyContext; | ||
|
||
public AddressSyndicationByPersistentLocalIdHandler( | ||
LegacyContext legacyContext, | ||
IOptions<ResponseOptions> responseOptions, | ||
IConfiguration configuration) : base (configuration, responseOptions) | ||
{ | ||
_legacyContext = legacyContext; | ||
} | ||
|
||
public async Task<SyndicationAtomContent> Handle(SyndicationByPersistentLocalIdRequest request, CancellationToken cancellationToken) | ||
{ | ||
var pagedAddresses = | ||
new AddressSyndicationPersistentLocalIdQuery(_legacyContext, request.Filtering.Filter?.Embed) | ||
.Fetch(request.Filtering, request.Sorting, request.Pagination); | ||
|
||
var lastUpdatedDateTime = pagedAddresses.Items | ||
.ToList() | ||
.Last() | ||
.LastChangedOn | ||
.ToDateTimeUtc(); | ||
|
||
return new SyndicationAtomContent(await BuildAtomFeed(lastUpdatedDateTime, pagedAddresses)); | ||
} | ||
} | ||
} |
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.