Skip to content

Commit

Permalink
added test api for recently added items
Browse files Browse the repository at this point in the history
  • Loading branch information
yuval-ziv committed Jul 21, 2024
1 parent 19937c0 commit cc63cc5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Announcarr/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
async (ICalendarService calendarService, [FromQuery(Name = "start")] DateTimeOffset? start, [FromQuery(Name = "end")] DateTimeOffset? end) =>
Results.Ok((object?)await calendarService.GetAllCalendarItemsAsync(start, end)));

app.MapGet("/recentlyAdded",
async (ICalendarService calendarService, [FromQuery(Name = "start")] DateTimeOffset? start, [FromQuery(Name = "end")] DateTimeOffset? end) =>
Results.Ok((object?)await calendarService.GetAllRecentlyAddedItemsAsync(start, end)));

app.MapGet("/testExporters", async (IEnumerable<IExporterService> exporterServices) => await Task.WhenAll(exporterServices.Select(exporterService => exporterService.TestExporterAsync())));

app.Run();
29 changes: 29 additions & 0 deletions Announcarr/Services/CalendarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public async Task<CalendarResponse> GetAllCalendarItemsAsync(DateTimeOffset? sta
return calendarResponse;
}

public async Task<RecentlyAddedResponse> GetAllRecentlyAddedItemsAsync(DateTimeOffset? start, DateTimeOffset? end, CancellationToken cancellationToken = default)
{
start ??= DateTimeOffset.Now.AddDays(-7);
end ??= start.Value.AddDays(7);

RecentlyAddedResponse[] recentlyAddedResponses = await Task.WhenAll(_integrationServices.Where(integration => integration.IsEnabled())
.Select(async serviceIntegration => await GetRecentlyAddedItemsAsync(serviceIntegration, start.Value, end.Value, cancellationToken)));
RecentlyAddedResponse recentlyAddedResponse = recentlyAddedResponses.Length != 0 ? recentlyAddedResponses.Aggregate(RecentlyAddedResponse.Merge) : new RecentlyAddedResponse();

await Task.WhenAll(_exporterServices.Where(exporter => exporter.IsEnabled())
.Select(exporter => exporter.ExportRecentlyAddedAsync(recentlyAddedResponse, start.Value, end.Value, cancellationToken)));

return recentlyAddedResponse;
}

private async Task<CalendarResponse> GetCalendarResponseAsync(IIntegrationService integrationService, DateTimeOffset start, DateTimeOffset end, CancellationToken cancellationToken = default)
{
try
Expand All @@ -43,4 +58,18 @@ private async Task<CalendarResponse> GetCalendarResponseAsync(IIntegrationServic
return new CalendarResponse();
}
}

private async Task<RecentlyAddedResponse> GetRecentlyAddedItemsAsync(IIntegrationService integrationService, DateTimeOffset start, DateTimeOffset end,
CancellationToken cancellationToken = default)
{
try
{
return await integrationService.GetRecentlyAddedAsync(start, end, cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "Unable to get recently added items from {IntegrationServiceName}", integrationService.GetName());
return new RecentlyAddedResponse();
}
}
}
1 change: 1 addition & 0 deletions Announcarr/Services/ICalendarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace Announcarr.Services;
public interface ICalendarService
{
Task<CalendarResponse> GetAllCalendarItemsAsync(DateTimeOffset? start, DateTimeOffset? end, CancellationToken cancellationToken = default);
Task<RecentlyAddedResponse> GetAllRecentlyAddedItemsAsync(DateTimeOffset? start, DateTimeOffset? end, CancellationToken cancellationToken = default);
}

0 comments on commit cc63cc5

Please sign in to comment.