Skip to content

Commit

Permalink
CsvReader, CsvWriter, ExcelReader, ExcelWriter, HtmlWriter, PdfWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Oct 11, 2024
1 parent 41e7c35 commit fce9b5d
Show file tree
Hide file tree
Showing 39 changed files with 349 additions and 225 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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Html;

public interface IHtmlWriter<T>
where T : IHtmlRequest
{
Task WriteAsync(T data, Stream stream);

Task<string> GetStringAsync(T data);
}

public interface IHtmlRequest
{
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Pdf;

public interface IPdfWriter<T>
where T : IPdfRequest
{
Task WriteAsync(T data, Stream stream);

Task<byte[]> GetBytesAsync(T data);
}

public interface IPdfRequest
{
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using ClassifiedAds.CrossCuttingConcerns.HtmlGenerator;
using ClassifiedAds.Infrastructure.HtmlGenerators;
using RazorLight;
using RazorLight;
using System;

namespace Microsoft.Extensions.DependencyInjection;

public static class HtmlGeneratorCollectionExtensions
public static class HtmlCollectionExtensions
{
public static IServiceCollection AddHtmlGenerator(this IServiceCollection services)
public static IServiceCollection AddHtmlRazorLightEngine(this IServiceCollection services)
{
var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Environment.CurrentDirectory)
.UseMemoryCachingProvider()
.Build();

services.AddSingleton<IRazorLightEngine>(engine);
services.AddSingleton<IHtmlGenerator, HtmlGenerator>();

return services;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf;
using DinkToPdf;
using DinkToPdf;
using DinkToPdf.Contracts;

namespace Microsoft.Extensions.DependencyInjection;

public static class DinkToPdfConverterCollectionExtensions
public static class DinkToPdfCollectionExtensions
{
public static IServiceCollection AddDinkToPdfConverter(this IServiceCollection services)
{
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddSingleton<IPdfConverter, DinkToPdfConverter>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp;
using PuppeteerSharp;
using PuppeteerSharp;

namespace Microsoft.Extensions.DependencyInjection;

public static class PuppeteerSharpConverterCollectionExtensions
public static class PuppeteerSharpCollectionExtensions
{
public static IServiceCollection AddPuppeteerSharpPdfConverter(this IServiceCollection services)
{
var browserFetcher = new BrowserFetcher();
browserFetcher.DownloadAsync().GetAwaiter().GetResult();

services.AddSingleton<IPdfConverter, PuppeteerSharpConverter>();

return services;
}
}

This file was deleted.

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

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -30,8 +30,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.Services.Configuration.Authorization;
using ClassifiedAds.Services.Configuration.ConfigurationOptions;
using ClassifiedAds.Services.Configuration.Entities;
using ClassifiedAds.Services.Configuration.Excel;
using ClassifiedAds.Services.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 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,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.Services.Configuration.Entities;
using ClosedXML.Excel;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Services.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;
}
}
Loading

0 comments on commit fce9b5d

Please sign in to comment.