diff --git a/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ExportConfigurationEntriesToExcel.cs b/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ExportConfigurationEntriesToExcel.cs new file mode 100644 index 00000000..b4a3f86b --- /dev/null +++ b/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ExportConfigurationEntriesToExcel.cs @@ -0,0 +1,10 @@ +using ClassifiedAds.CrossCuttingConcerns.Excel; +using ClassifiedAds.Domain.Entities; +using System.Collections.Generic; + +namespace ClassifiedAds.Application.ConfigurationEntries.DTOs; + +public class ExportConfigurationEntriesToExcel : IExcelRequest +{ + public List ConfigurationEntries { get; set; } +} diff --git a/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ImportConfigurationEntriesFromExcel.cs b/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ImportConfigurationEntriesFromExcel.cs new file mode 100644 index 00000000..24e31c34 --- /dev/null +++ b/src/Monolith/ClassifiedAds.Application/ConfigurationEntries/DTOs/ImportConfigurationEntriesFromExcel.cs @@ -0,0 +1,10 @@ +using ClassifiedAds.CrossCuttingConcerns.Excel; +using ClassifiedAds.Domain.Entities; +using System.Collections.Generic; + +namespace ClassifiedAds.Application.ConfigurationEntries.DTOs; + +public class ImportConfigurationEntriesFromExcel : IExcelResponse +{ + public List ConfigurationEntries { get; set; } +} diff --git a/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelReader.cs b/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelReader.cs index cd3f6774..71c0934c 100644 --- a/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelReader.cs +++ b/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelReader.cs @@ -4,6 +4,11 @@ namespace ClassifiedAds.CrossCuttingConcerns.Excel; public interface IExcelReader + where T : IExcelResponse { Task ReadAsync(Stream stream); } + +public interface IExcelResponse +{ +} diff --git a/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelWriter.cs b/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelWriter.cs index 3622e5b5..536e8ca1 100644 --- a/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelWriter.cs +++ b/src/Monolith/ClassifiedAds.CrossCuttingConcerns/Excel/IExcelWriter.cs @@ -4,6 +4,11 @@ namespace ClassifiedAds.CrossCuttingConcerns.Excel; public interface IExcelWriter + where T : IExcelRequest { Task WriteAsync(T data, Stream stream); } + +public interface IExcelRequest +{ +} \ No newline at end of file diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelWriter.cs b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ExportConfigurationEntriesToExcelHandler.cs similarity index 64% rename from src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelWriter.cs rename to src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ExportConfigurationEntriesToExcelHandler.cs index c222af20..b9827741 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelWriter.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ExportConfigurationEntriesToExcelHandler.cs @@ -1,15 +1,14 @@ -using ClassifiedAds.CrossCuttingConcerns.Excel; -using ClassifiedAds.Domain.Entities; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.CrossCuttingConcerns.Excel; using ClosedXML.Excel; -using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace ClassifiedAds.Infrastructure.Excel.ClosedXML; -public class ConfigurationEntryExcelWriter : IExcelWriter> +public class ExportConfigurationEntriesToExcelHandler : IExcelWriter { - public Task WriteAsync(List data, Stream stream) + public Task WriteAsync(ExportConfigurationEntriesToExcel data, Stream stream) { using var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add("Sheet1"); @@ -19,7 +18,7 @@ public Task WriteAsync(List data, Stream stream) worksheet.Range("A1:B1").Style.Font.Bold = true; int i = 2; - foreach (var row in data) + foreach (var row in data.ConfigurationEntries) { worksheet.Cell("A" + i).Value = row.Key; worksheet.Cell("B" + i).Value = row.Value; diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelReader.cs b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ImportConfigurationEntriesFromExcelHandler.cs similarity index 74% rename from src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelReader.cs rename to src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ImportConfigurationEntriesFromExcelHandler.cs index 5430c231..b1852a96 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ConfigurationEntryExcelReader.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ClosedXML/ImportConfigurationEntriesFromExcelHandler.cs @@ -1,4 +1,5 @@ -using ClassifiedAds.CrossCuttingConcerns.Excel; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.CrossCuttingConcerns.Excel; using ClassifiedAds.CrossCuttingConcerns.Exceptions; using ClassifiedAds.Domain.Entities; using ClosedXML.Excel; @@ -9,7 +10,7 @@ namespace ClassifiedAds.Infrastructure.Excel.ClosedXML; -public class ConfigurationEntryExcelReader : IExcelReader> +public class ImportConfigurationEntriesFromExcelHandler : IExcelReader { private static Dictionary GetCorrectHeaders() { @@ -20,7 +21,7 @@ private static Dictionary GetCorrectHeaders() }; } - public Task> ReadAsync(Stream stream) + public Task ReadAsync(Stream stream) { using var workbook = new XLWorkbook(stream); var worksheet = workbook.Worksheets.First(); @@ -44,6 +45,6 @@ public Task> ReadAsync(Stream stream) rows.Add(row); } - return Task.FromResult(rows); + return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows }); } } diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelWriter.cs b/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ExportConfigurationEntriesToExcelHandler.cs similarity index 64% rename from src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelWriter.cs rename to src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ExportConfigurationEntriesToExcelHandler.cs index ac841fdb..e5a93795 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelWriter.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ExportConfigurationEntriesToExcelHandler.cs @@ -1,15 +1,14 @@ -using ClassifiedAds.CrossCuttingConcerns.Excel; -using ClassifiedAds.Domain.Entities; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.CrossCuttingConcerns.Excel; using OfficeOpenXml; -using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace ClassifiedAds.Infrastructure.Excel.EPPlus; -public class ConfigurationEntryExcelWriter : IExcelWriter> +public class ExportConfigurationEntriesToExcelHandler : IExcelWriter { - public Task WriteAsync(List data, Stream stream) + public Task WriteAsync(ExportConfigurationEntriesToExcel data, Stream stream) { using var pck = new ExcelPackage(); var worksheet = pck.Workbook.Worksheets.Add("Sheet1"); @@ -19,7 +18,7 @@ public Task WriteAsync(List data, Stream stream) worksheet.Cells["A1:B1"].Style.Font.Bold = true; int i = 2; - foreach (var row in data) + foreach (var row in data.ConfigurationEntries) { worksheet.Cells["A" + i].Value = row.Key; worksheet.Cells["B" + i].Value = row.Value; diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelReader.cs b/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ImportConfigurationEntriesFromExcelHandler.cs similarity index 73% rename from src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelReader.cs rename to src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ImportConfigurationEntriesFromExcelHandler.cs index 79baede2..79b1c576 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ConfigurationEntryExcelReader.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Excel/EPPlus/ImportConfigurationEntriesFromExcelHandler.cs @@ -1,4 +1,5 @@ -using ClassifiedAds.CrossCuttingConcerns.Excel; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.CrossCuttingConcerns.Excel; using ClassifiedAds.CrossCuttingConcerns.Exceptions; using ClassifiedAds.Domain.Entities; using OfficeOpenXml; @@ -9,7 +10,7 @@ namespace ClassifiedAds.Infrastructure.Excel.EPPlus; -public class ConfigurationEntryExcelReader : IExcelReader> +public class ImportConfigurationEntriesFromExcelHandler : IExcelReader { private static Dictionary GetCorrectHeaders() { @@ -20,7 +21,7 @@ private static Dictionary GetCorrectHeaders() }; } - public Task> ReadAsync(Stream stream) + public Task ReadAsync(Stream stream) { using var pck = new ExcelPackage(stream); var worksheet = pck.Workbook.Worksheets.First(); @@ -44,6 +45,6 @@ public Task> ReadAsync(Stream stream) rows.Add(row); } - return Task.FromResult(rows); + return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows }); } } diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ConfigurationEntryExcelReader.cs b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ImportConfigurationEntriesFromExcelHandler.cs similarity index 83% rename from src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ConfigurationEntryExcelReader.cs rename to src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ImportConfigurationEntriesFromExcelHandler.cs index 89f36e8a..8b4d9eff 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ConfigurationEntryExcelReader.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Excel/ExcelDataReader/ImportConfigurationEntriesFromExcelHandler.cs @@ -1,4 +1,5 @@ -using ClassifiedAds.CrossCuttingConcerns.Excel; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.CrossCuttingConcerns.Excel; using ClassifiedAds.CrossCuttingConcerns.Exceptions; using ClassifiedAds.Domain.Entities; using ExcelDataReader; @@ -9,7 +10,7 @@ namespace ClassifiedAds.Infrastructure.Excel.ExcelDataReader; -public class ConfigurationEntryExcelReader : IExcelReader> +public class ImportConfigurationEntriesFromExcelHandler : IExcelReader { private static Dictionary GetCorrectHeaders() { @@ -20,7 +21,7 @@ private static Dictionary GetCorrectHeaders() }; } - public Task> ReadAsync(Stream stream) + public Task ReadAsync(Stream stream) { var rows = new List(); int headerIndex = 0; @@ -73,6 +74,6 @@ public Task> ReadAsync(Stream stream) while (reader.NextResult()); } - return Task.FromResult(rows); + return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows }); } } diff --git a/src/Monolith/ClassifiedAds.WebAPI/Controllers/ConfigurationEntriesController.cs b/src/Monolith/ClassifiedAds.WebAPI/Controllers/ConfigurationEntriesController.cs index dafa155f..340fe74c 100644 --- a/src/Monolith/ClassifiedAds.WebAPI/Controllers/ConfigurationEntriesController.cs +++ b/src/Monolith/ClassifiedAds.WebAPI/Controllers/ConfigurationEntriesController.cs @@ -1,4 +1,5 @@ using ClassifiedAds.Application; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; using ClassifiedAds.CrossCuttingConcerns.Excel; using ClassifiedAds.Domain.Entities; using ClassifiedAds.WebAPI.Authorization; @@ -29,14 +30,14 @@ public class ConfigurationEntriesController : ControllerBase private readonly Dispatcher _dispatcher; private readonly ILogger _logger; private readonly AppSettings _appSettings; - private readonly IExcelWriter> _configurationEntriesExcelWriter; - private readonly IExcelReader> _configurationEntriesExcelReader; + private readonly IExcelWriter _configurationEntriesExcelWriter; + private readonly IExcelReader _configurationEntriesExcelReader; public ConfigurationEntriesController(Dispatcher dispatcher, ILogger logger, IOptionsSnapshot appSettings, - IExcelWriter> configurationEntriesExcelWriter, - IExcelReader> configurationEntriesExcelReader) + IExcelWriter configurationEntriesExcelWriter, + IExcelReader configurationEntriesExcelReader) { _dispatcher = dispatcher; _logger = logger; @@ -131,7 +132,7 @@ public async Task ExportAsExcel() { var entries = await _dispatcher.DispatchAsync(new GetEntititesQuery()); using var stream = new MemoryStream(); - await _configurationEntriesExcelWriter.WriteAsync(entries, stream); + await _configurationEntriesExcelWriter.WriteAsync(new ExportConfigurationEntriesToExcel { ConfigurationEntries = entries }, stream); return File(stream.ToArray(), MediaTypeNames.Application.Octet, "ConfigurationEntries.xlsx"); } @@ -142,6 +143,6 @@ public async Task ImportExcel([FromForm] UploadFileModel model) var entries = await _configurationEntriesExcelReader.ReadAsync(stream); // TODO: import to database - return Ok(entries); + return Ok(entries.ConfigurationEntries); } } diff --git a/src/Monolith/ClassifiedAds.WebAPI/Program.cs b/src/Monolith/ClassifiedAds.WebAPI/Program.cs index 1e193bbe..296e502a 100644 --- a/src/Monolith/ClassifiedAds.WebAPI/Program.cs +++ b/src/Monolith/ClassifiedAds.WebAPI/Program.cs @@ -1,7 +1,7 @@ -using ClassifiedAds.Application.Products.DTOs; +using ClassifiedAds.Application.ConfigurationEntries.DTOs; +using ClassifiedAds.Application.Products.DTOs; using ClassifiedAds.CrossCuttingConcerns.Csv; using ClassifiedAds.CrossCuttingConcerns.Excel; -using ClassifiedAds.Domain.Entities; using ClassifiedAds.Domain.Identity; using ClassifiedAds.Infrastructure.Csv; using ClassifiedAds.Infrastructure.Excel.ClosedXML; @@ -231,8 +231,8 @@ services.AddScoped, ImportProductsFromCsvHandler>(); services.AddScoped, ExportProductsToCsvHandler>(); -services.AddScoped>, ConfigurationEntryExcelReader>(); -services.AddScoped>, ConfigurationEntryExcelWriter>(); +services.AddScoped, ImportConfigurationEntriesFromExcelHandler>(); +services.AddScoped, ExportConfigurationEntriesToExcelHandler>(); // Configure the HTTP request pipeline. var app = builder.Build();