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 10, 2024
1 parent 773eda9 commit 849fdb4
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ExportProductsToCsv
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ImportProductsFromCsv
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvReader<T>
{
IEnumerable<T> Read(Stream stream);
Task<T> ReadAsync(Stream stream);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvWriter<T>
{
void Write(IEnumerable<T> collection, Stream stream);
Task WriteAsync(T data, Stream stream);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Excel;

public interface IExcelReader<T>
{
T Read(Stream stream);
Task<T> ReadAsync(Stream stream);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Excel;

public interface IExcelWriter<T>
{
void Write(T data, Stream stream);
Task WriteAsync(T data, Stream stream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.Application\ClassifiedAds.Application.csproj" />
<ProjectReference Include="..\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" />
</ItemGroup>

Expand Down
17 changes: 0 additions & 17 deletions src/Monolith/ClassifiedAds.Infrastructure/Csv/CsvReader.cs

This file was deleted.

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Csv;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.Csv;

public class ExportProductsToCsvHandler : ICsvWriter<ExportProductsToCsv>
{
public Task WriteAsync(ExportProductsToCsv data, Stream stream)
{
using var writer = new StreamWriter(stream);
using var csv = new CsvHelper.CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(data.Products);

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Csv;
using ClassifiedAds.Domain.Entities;
using CsvHelper.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.Csv;

public class ImportProductsFromCsvHandler : ICsvReader<ImportProductsFromCsv>
{
public Task<ImportProductsFromCsv> ReadAsync(Stream stream)
{
using var reader = new StreamReader(stream);

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HeaderValidated = null,
MissingFieldFound = null,
};

using var csv = new CsvHelper.CsvReader(reader, config);

var response = new ImportProductsFromCsv
{
Products = csv.GetRecords<Product>().ToList(),
};

return Task.FromResult(response);
}
}
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.Infrastructure.Excel.ClosedXML;

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

public Task<List<ConfigurationEntry>> ReadAsync(Stream stream)
{
using var workbook = new XLWorkbook(stream);
var worksheet = 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(rows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
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 void Write(List<ConfigurationEntry> data, Stream stream)
public Task WriteAsync(List<ConfigurationEntry> data, Stream stream)
{
using var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sheet1");
Expand All @@ -26,5 +27,7 @@ public void Write(List<ConfigurationEntry> data, Stream stream)
}

workbook.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.Infrastructure.Excel.EPPlus;

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

public Task<List<ConfigurationEntry>> 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(rows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
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 void Write(List<ConfigurationEntry> data, Stream stream)
public Task WriteAsync(List<ConfigurationEntry> data, Stream stream)
{
using var pck = new ExcelPackage();
var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
Expand All @@ -26,5 +27,7 @@ public void Write(List<ConfigurationEntry> data, Stream stream)
}

pck.SaveAs(stream);

return Task.CompletedTask;
}
}
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.Infrastructure.Excel.ExcelDataReader;

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

public Task<List<ConfigurationEntry>> 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(rows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ 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(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class ProductsController : ControllerBase
private readonly ILogger _logger;
private readonly IHtmlGenerator _htmlGenerator;
private readonly IPdfConverter _pdfConverter;
private readonly ICsvWriter<ProductModel> _productCsvWriter;
private readonly ICsvReader<ProductModel> _productCsvReader;
private readonly ICsvWriter<ExportProductsToCsv> _productCsvWriter;
private readonly ICsvReader<ImportProductsFromCsv> _productCsvReader;

public ProductsController(Dispatcher dispatcher,
ILogger<ProductsController> logger,
IHtmlGenerator htmlGenerator,
IPdfConverter pdfConverter,
ICsvWriter<ProductModel> productCsvWriter,
ICsvReader<ProductModel> productCsvReader)
ICsvWriter<ExportProductsToCsv> productCsvWriter,
ICsvReader<ImportProductsFromCsv> productCsvReader)
{
_dispatcher = dispatcher;
_logger = logger;
Expand Down Expand Up @@ -173,19 +173,18 @@ public async Task<IActionResult> ExportAsPdf()
public async Task<IActionResult> ExportAsCsv()
{
var products = await _dispatcher.DispatchAsync(new GetProductsQuery());
var model = products.ToModels();
using var stream = new MemoryStream();
_productCsvWriter.Write(model, stream);
await _productCsvWriter.WriteAsync(new ExportProductsToCsv { Products = products }, stream);
return File(stream.ToArray(), MediaTypeNames.Application.Octet, "Products.csv");
}

[HttpPost("importcsv")]
public IActionResult ImportCsv([FromForm] UploadFileModel model)
public async Task<IActionResult> ImportCsv([FromForm] UploadFileModel model)
{
using var stream = model.FormFile.OpenReadStream();
var products = _productCsvReader.Read(stream);
var result = await _productCsvReader.ReadAsync(stream);

// TODO: import to database
return Ok(products);
return Ok(result.Products);
}
}
Loading

0 comments on commit 849fdb4

Please sign in to comment.