Skip to content

Commit

Permalink
CsvReader, CsvWriter, ExcelReader, ExcelWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Oct 11, 2024
1 parent b6b8656 commit d82e5c3
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvReader<T>
where T : ICsvResponse
{
IEnumerable<T> Read(Stream stream);
Task<T> ReadAsync(Stream stream);
}

public interface ICsvResponse
{
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvWriter<T>
where T : ICsvRequest
{
void Write(IEnumerable<T> collection, Stream stream);
Task WriteAsync(T data, Stream stream);
}

public interface ICsvRequest
{
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Excel;

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

public interface IExcelResponse
{
T Read(Stream stream);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Excel;

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

public interface IExcelRequest
{
}
17 changes: 0 additions & 17 deletions src/ModularMonolith/ClassifiedAds.Infrastructure/Csv/CsvReader.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/ModularMonolith/ClassifiedAds.Infrastructure/Csv/CsvWriter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using ClassifiedAds.Modules.Configuration.Authorization;
using ClassifiedAds.Modules.Configuration.ConfigurationOptions;
using ClassifiedAds.Modules.Configuration.Entities;
using ClassifiedAds.Modules.Configuration.Excel;
using ClassifiedAds.Modules.Configuration.Excel.ClosedXML;
using ClassifiedAds.Modules.Configuration.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -35,8 +35,8 @@ public static IServiceCollection AddConfigurationModule(this IServiceCollection

services.AddAuthorizationPolicies(Assembly.GetExecutingAssembly(), AuthorizationPolicyNames.GetPolicyNames());

services.AddScoped<IExcelReader<List<ConfigurationEntry>>, ConfigurationEntryExcelReader>();
services.AddScoped<IExcelWriter<List<ConfigurationEntry>>, ConfigurationEntryExcelWriter>();
services.AddScoped<IExcelReader<ImportConfigurationEntriesFromExcel>, ImportConfigurationEntriesFromExcelHandler>();
services.AddScoped<IExcelWriter<ExportConfigurationEntriesToExcel>, ExportConfigurationEntriesToExcelHandler>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ClassifiedAds.Modules.Configuration.Authorization;
using ClassifiedAds.Modules.Configuration.ConfigurationOptions;
using ClassifiedAds.Modules.Configuration.Entities;
using ClassifiedAds.Modules.Configuration.Excel;
using ClassifiedAds.Modules.Configuration.Models;
using CryptographyHelper;
using CryptographyHelper.AsymmetricAlgorithms;
Expand All @@ -29,14 +30,14 @@ public class ConfigurationEntriesController : ControllerBase
private readonly Dispatcher _dispatcher;
private readonly ILogger _logger;
private readonly ConfigurationModuleOptions _moduleOptions;
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<ConfigurationModuleOptions> moduleOptions,
IExcelWriter<List<ConfigurationEntry>> configurationEntriesExcelWriter,
IExcelReader<List<ConfigurationEntry>> configurationEntriesExcelReader)
IExcelWriter<ExportConfigurationEntriesToExcel> configurationEntriesExcelWriter,
IExcelReader<ImportConfigurationEntriesFromExcel> configurationEntriesExcelReader)
{
_dispatcher = dispatcher;
_logger = logger;
Expand Down Expand Up @@ -131,17 +132,17 @@ public async Task<IActionResult> ExportAsExcel()
{
var entries = await _dispatcher.DispatchAsync(new GetEntititesQuery<ConfigurationEntry>());
using var stream = new MemoryStream();
_configurationEntriesExcelWriter.Write(entries, stream);
await _configurationEntriesExcelWriter.WriteAsync(new ExportConfigurationEntriesToExcel { ConfigurationEntries = entries }, stream);
return File(stream.ToArray(), MediaTypeNames.Application.Octet, "ConfigurationEntries.xlsx");
}

[HttpPost("ImportExcel")]
public IActionResult ImportExcel([FromForm] UploadFileModel model)
public async Task<IActionResult> ImportExcel([FromForm] UploadFileModel model)
{
using var stream = model.FormFile.OpenReadStream();
var entries = _configurationEntriesExcelReader.Read(stream);
var entries = await _configurationEntriesExcelReader.ReadAsync(stream);

// TODO: import to database
return Ok(entries);
return Ok(entries.ConfigurationEntries);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.Excel;
using ClassifiedAds.Modules.Configuration.Entities;
using ClosedXML.Excel;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Configuration.Excel.ClosedXML;

public class ConfigurationEntryExcelWriter : IExcelWriter<List<ConfigurationEntry>>
public class ExportConfigurationEntriesToExcelHandler : IExcelWriter<ExportConfigurationEntriesToExcel>
{
public void Write(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 @@ -18,13 +17,15 @@ public void Write(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;
i++;
}

workbook.SaveAs(stream);

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using ClassifiedAds.CrossCuttingConcerns.Excel;
using ClassifiedAds.CrossCuttingConcerns.Exceptions;
using ClassifiedAds.Modules.Configuration.Entities;
using ClassifiedAds.Modules.Configuration.Excel;
using ClosedXML.Excel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Configuration.Excel.ClosedXML;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
public List<ConfigurationEntry> Read(Stream stream)
private static Dictionary<string, string> GetCorrectHeaders()
{
return new Dictionary<string, string>
{
{ "A", "Key" },
{ "B", "Value" },
};
}

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

return rows;
}

private static Dictionary<string, string> GetCorrectHeaders()
{
return new Dictionary<string, string>
{
{ "A", "Key" },
{ "B", "Value" },
};
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.Excel;
using ClassifiedAds.Modules.Configuration.Entities;
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Configuration.Excel.EPPlus;

public class ConfigurationEntryExcelWriter : IExcelWriter<List<ConfigurationEntry>>
public class ExportConfigurationEntriesToExcelHandler : IExcelWriter<ExportConfigurationEntriesToExcel>
{
public void Write(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 @@ -18,13 +17,15 @@ public void Write(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;
i++;
}

pck.SaveAs(stream);

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Configuration.Excel.EPPlus;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
public List<ConfigurationEntry> Read(Stream stream)
private static Dictionary<string, string> GetCorrectHeaders()
{
return new Dictionary<string, string>
{
{ "A", "Key" },
{ "B", "Value" },
};
}

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

return rows;
}

private static Dictionary<string, string> GetCorrectHeaders()
{
return new Dictionary<string, string>
{
{ "A", "Key" },
{ "B", "Value" },
};
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Configuration.Excel.ExcelDataReader;

public class ConfigurationEntryExcelReader : IExcelReader<List<ConfigurationEntry>>
public class ImportConfigurationEntriesFromExcelHandler : IExcelReader<ImportConfigurationEntriesFromExcel>
{
public List<ConfigurationEntry> Read(Stream stream)
private static Dictionary<int, string> GetCorrectHeaders()
{
return new Dictionary<int, string>
{
{ 0, "Key" },
{ 1, "Value" },
};
}

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

return rows;
}

private static Dictionary<int, string> GetCorrectHeaders()
{
return new Dictionary<int, string>
{
{ 0, "Key" },
{ 1, "Value" },
};
return Task.FromResult(new ImportConfigurationEntriesFromExcel { ConfigurationEntries = rows });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ClassifiedAds.CrossCuttingConcerns.Excel;
using ClassifiedAds.Modules.Configuration.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Modules.Configuration.Excel;

public class ExportConfigurationEntriesToExcel : IExcelRequest
{
public List<ConfigurationEntry> ConfigurationEntries { get; set; }
}
Loading

0 comments on commit d82e5c3

Please sign in to comment.