From fcc35b03c1eefd60fda4cdd02fbefdedeb6d9f3a Mon Sep 17 00:00:00 2001 From: Dave Walker Date: Thu, 30 Nov 2023 16:37:46 +0000 Subject: [PATCH] Added retailer statistics export --- .../Controllers/ExportController.cs | 18 ++++++- .../RetailerStatisticsExportWorkItem.cs | 8 ++++ src/MusicCatalogue.Api/Program.cs | 4 ++ .../RetailerStatisticsExportService.cs | 47 +++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/MusicCatalogue.Api/Entities/RetailerStatisticsExportWorkItem.cs create mode 100644 src/MusicCatalogue.Api/Services/RetailerStatisticsExportService.cs diff --git a/src/MusicCatalogue.Api/Controllers/ExportController.cs b/src/MusicCatalogue.Api/Controllers/ExportController.cs index dfbedc6..c20b9b0 100644 --- a/src/MusicCatalogue.Api/Controllers/ExportController.cs +++ b/src/MusicCatalogue.Api/Controllers/ExportController.cs @@ -15,18 +15,20 @@ public class ExportController : Controller private readonly IBackgroundQueue _artistStatisticsQueue; private readonly IBackgroundQueue _genreStatisticsQueue; private readonly IBackgroundQueue _monthlySpendQueue; + private readonly IBackgroundQueue _retailerStatisticsQueue; public ExportController( IBackgroundQueue catalogueQueue, IBackgroundQueue artistStatisticsQueue, IBackgroundQueue genreStatisticsQueue, - IBackgroundQueue monthlySpendQueue - ) + IBackgroundQueue monthlySpendQueue, + IBackgroundQueue retailerStatisticsQueue) { _catalogueQueue = catalogueQueue; _artistStatisticsQueue = artistStatisticsQueue; _genreStatisticsQueue = genreStatisticsQueue; _monthlySpendQueue = monthlySpendQueue; + _retailerStatisticsQueue = retailerStatisticsQueue; } [HttpPost] @@ -76,5 +78,17 @@ public IActionResult ExportMonthySpendReport([FromBody] MonthlySpendExportWorkIt _monthlySpendQueue.Enqueue(item); return Accepted(); } + + [HttpPost] + [Route("retailerstatistics")] + public IActionResult ExportRetailerStatisticsReport([FromBody] RetailerStatisticsExportWorkItem item) + { + // Set the job name used in the job status record + item.JobName = "Retailer Statistics Export"; + + // Queue the work item + _retailerStatisticsQueue.Enqueue(item); + return Accepted(); + } } } diff --git a/src/MusicCatalogue.Api/Entities/RetailerStatisticsExportWorkItem.cs b/src/MusicCatalogue.Api/Entities/RetailerStatisticsExportWorkItem.cs new file mode 100644 index 0000000..fb874ac --- /dev/null +++ b/src/MusicCatalogue.Api/Entities/RetailerStatisticsExportWorkItem.cs @@ -0,0 +1,8 @@ +namespace MusicCatalogue.Api.Entities +{ + public class RetailerStatisticsExportWorkItem : BackgroundWorkItem + { + public string FileName { get; set; } = ""; + public bool WishList { get; set; } + } +} diff --git a/src/MusicCatalogue.Api/Program.cs b/src/MusicCatalogue.Api/Program.cs index 524b7c7..a35ad00 100644 --- a/src/MusicCatalogue.Api/Program.cs +++ b/src/MusicCatalogue.Api/Program.cs @@ -116,6 +116,10 @@ public static void Main(string[] args) builder.Services.AddSingleton, BackgroundQueue>(); builder.Services.AddHostedService(); + // Add the retailer statistics report exporter hosted service + builder.Services.AddSingleton, BackgroundQueue>(); + builder.Services.AddHostedService(); + // Configure JWT byte[] key = Encoding.ASCII.GetBytes(settings!.Secret); builder.Services.AddAuthentication(x => diff --git a/src/MusicCatalogue.Api/Services/RetailerStatisticsExportService.cs b/src/MusicCatalogue.Api/Services/RetailerStatisticsExportService.cs new file mode 100644 index 0000000..28c6c0b --- /dev/null +++ b/src/MusicCatalogue.Api/Services/RetailerStatisticsExportService.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.Options; +using MusicCatalogue.Api.Entities; +using MusicCatalogue.Api.Interfaces; +using MusicCatalogue.Entities.Config; +using MusicCatalogue.Entities.Interfaces; +using MusicCatalogue.Entities.Reporting; +using MusicCatalogue.Logic.DataExchange.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace MusicCatalogue.Api.Services +{ + [ExcludeFromCodeCoverage] + public class RetailerStatisticsExportService : BackgroundQueueProcessor + { + private readonly MusicApplicationSettings _settings; + public RetailerStatisticsExportService( + ILogger> logger, + IBackgroundQueue queue, + IServiceScopeFactory serviceScopeFactory, + IOptions settings) + : base(logger, queue, serviceScopeFactory) + { + _settings = settings.Value; + } + + /// + /// Export the retailer statistics report + /// + /// + /// + /// + protected override async Task ProcessWorkItem(RetailerStatisticsExportWorkItem item, IMusicCatalogueFactory factory) + { + // Get the report data + MessageLogger.LogInformation("Retrieving the retailer statistics report for export"); + var records = await factory.RetailerStatistics.GenerateReportAsync(item.WishList, 1, int.MaxValue); + + // Construct the full path to the export file + var filePath = Path.Combine(_settings.ReportsExportPath, item.FileName); + + // Export the report + var exporter = new CsvExporter(); + exporter.Export(records, filePath, ','); + MessageLogger.LogInformation("Retailer statistics report export completed"); + } + } +} \ No newline at end of file