Skip to content

Commit

Permalink
ExcelReader, ExcelWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Oct 10, 2024
1 parent bde8670 commit b156fa2
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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<ConfigurationEntry> ConfigurationEntries { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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<ConfigurationEntry> ConfigurationEntries { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
namespace ClassifiedAds.CrossCuttingConcerns.Excel;

public interface IExcelReader<T>
where T : IExcelResponse
{
Task<T> ReadAsync(Stream stream);
}

public interface IExcelResponse
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
namespace ClassifiedAds.CrossCuttingConcerns.Excel;

public interface IExcelWriter<T>
where T : IExcelRequest
{
Task WriteAsync(T data, Stream stream);
}

public interface IExcelRequest
{
}
Original file line number Diff line number Diff line change
@@ -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<List<ConfigurationEntry>>
public class ExportConfigurationEntriesToExcelHandler : IExcelWriter<ExportConfigurationEntriesToExcel>
{
public Task WriteAsync(List<ConfigurationEntry> data, Stream stream)
public Task WriteAsync(ExportConfigurationEntriesToExcel data, Stream stream)
{
using var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sheet1");
Expand All @@ -19,7 +18,7 @@ public Task WriteAsync(List<ConfigurationEntry> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,7 @@

namespace ClassifiedAds.Infrastructure.Excel.ClosedXML;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
private static Dictionary<string, string> GetCorrectHeaders()
{
Expand All @@ -20,7 +21,7 @@ private static Dictionary<string, string> GetCorrectHeaders()
};
}

public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
public Task<ImportConfigurationEntriesFromExcel> ReadAsync(Stream stream)
{
using var workbook = new XLWorkbook(stream);
var worksheet = workbook.Worksheets.First();
Expand All @@ -44,6 +45,6 @@ public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
rows.Add(row);
}

return Task.FromResult(rows);
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
@@ -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<List<ConfigurationEntry>>
public class ExportConfigurationEntriesToExcelHandler : IExcelWriter<ExportConfigurationEntriesToExcel>
{
public Task WriteAsync(List<ConfigurationEntry> data, Stream stream)
public Task WriteAsync(ExportConfigurationEntriesToExcel data, Stream stream)
{
using var pck = new ExcelPackage();
var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
Expand All @@ -19,7 +18,7 @@ public Task WriteAsync(List<ConfigurationEntry> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,7 @@

namespace ClassifiedAds.Infrastructure.Excel.EPPlus;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
private static Dictionary<string, string> GetCorrectHeaders()
{
Expand All @@ -20,7 +21,7 @@ private static Dictionary<string, string> GetCorrectHeaders()
};
}

public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
public Task<ImportConfigurationEntriesFromExcel> ReadAsync(Stream stream)
{
using var pck = new ExcelPackage(stream);
var worksheet = pck.Workbook.Worksheets.First();
Expand All @@ -44,6 +45,6 @@ public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
rows.Add(row);
}

return Task.FromResult(rows);
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,7 @@

namespace ClassifiedAds.Infrastructure.Excel.ExcelDataReader;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
private static Dictionary<int, string> GetCorrectHeaders()
{
Expand All @@ -20,7 +21,7 @@ private static Dictionary<int, string> GetCorrectHeaders()
};
}

public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
public Task<ImportConfigurationEntriesFromExcel> ReadAsync(Stream stream)
{
var rows = new List<ConfigurationEntry>();
int headerIndex = 0;
Expand Down Expand Up @@ -73,6 +74,6 @@ public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
while (reader.NextResult());
}

return Task.FromResult(rows);
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ClassifiedAds.Application;
using ClassifiedAds.Application.ConfigurationEntries.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Excel;
using ClassifiedAds.Domain.Entities;
using ClassifiedAds.WebAPI.Authorization;
Expand Down Expand Up @@ -29,14 +30,14 @@ public class ConfigurationEntriesController : ControllerBase
private readonly Dispatcher _dispatcher;
private readonly ILogger _logger;
private readonly AppSettings _appSettings;
private readonly IExcelWriter<List<ConfigurationEntry>> _configurationEntriesExcelWriter;
private readonly IExcelReader<List<ConfigurationEntry>> _configurationEntriesExcelReader;
private readonly IExcelWriter<ExportConfigurationEntriesToExcel> _configurationEntriesExcelWriter;
private readonly IExcelReader<ImportConfigurationEntriesFromExcel> _configurationEntriesExcelReader;

public ConfigurationEntriesController(Dispatcher dispatcher,
ILogger<ConfigurationEntriesController> logger,
IOptionsSnapshot<AppSettings> appSettings,
IExcelWriter<List<ConfigurationEntry>> configurationEntriesExcelWriter,
IExcelReader<List<ConfigurationEntry>> configurationEntriesExcelReader)
IExcelWriter<ExportConfigurationEntriesToExcel> configurationEntriesExcelWriter,
IExcelReader<ImportConfigurationEntriesFromExcel> configurationEntriesExcelReader)
{
_dispatcher = dispatcher;
_logger = logger;
Expand Down Expand Up @@ -131,7 +132,7 @@ public async Task<IActionResult> ExportAsExcel()
{
var entries = await _dispatcher.DispatchAsync(new GetEntititesQuery<ConfigurationEntry>());
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");
}

Expand All @@ -142,6 +143,6 @@ public async Task<IActionResult> ImportExcel([FromForm] UploadFileModel model)
var entries = await _configurationEntriesExcelReader.ReadAsync(stream);

// TODO: import to database
return Ok(entries);
return Ok(entries.ConfigurationEntries);
}
}
8 changes: 4 additions & 4 deletions src/Monolith/ClassifiedAds.WebAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -231,8 +231,8 @@

services.AddScoped<ICsvReader<ImportProductsFromCsv>, ImportProductsFromCsvHandler>();
services.AddScoped<ICsvWriter<ExportProductsToCsv>, ExportProductsToCsvHandler>();
services.AddScoped<IExcelReader<List<ConfigurationEntry>>, ConfigurationEntryExcelReader>();
services.AddScoped<IExcelWriter<List<ConfigurationEntry>>, ConfigurationEntryExcelWriter>();
services.AddScoped<IExcelReader<ImportConfigurationEntriesFromExcel>, ImportConfigurationEntriesFromExcelHandler>();
services.AddScoped<IExcelWriter<ExportConfigurationEntriesToExcel>, ExportConfigurationEntriesToExcelHandler>();

// Configure the HTTP request pipeline.
var app = builder.Build();
Expand Down

0 comments on commit b156fa2

Please sign in to comment.