-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
849fdb4
commit bde8670
Showing
23 changed files
with
205 additions
and
140 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
src/Monolith/ClassifiedAds.Application/Products/DTOs/ExportProductsToCsv.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using ClassifiedAds.Domain.Entities; | ||
using ClassifiedAds.CrossCuttingConcerns.Csv; | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ExportProductsToCsv | ||
public record ExportProductsToCsv : ICsvRequest | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Monolith/ClassifiedAds.Application/Products/DTOs/ExportProductsToHtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ExportProductsToHtml : IHtmlRequest | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Monolith/ClassifiedAds.Application/Products/DTOs/ExportProductsToPdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ExportProductsToPdf : IPdfRequest | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
5 changes: 3 additions & 2 deletions
5
src/Monolith/ClassifiedAds.Application/Products/DTOs/ImportProductsFromCsv.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using ClassifiedAds.Domain.Entities; | ||
using ClassifiedAds.CrossCuttingConcerns.Csv; | ||
using ClassifiedAds.Domain.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Application.Products.DTOs; | ||
|
||
public record ImportProductsFromCsv | ||
public record ImportProductsFromCsv : ICsvResponse | ||
{ | ||
public List<Product> Products { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Html/IHtmlWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
8 changes: 0 additions & 8 deletions
8
src/Monolith/ClassifiedAds.CrossCuttingConcerns/HtmlGenerator/IHtmlGenerator.cs
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/Monolith/ClassifiedAds.CrossCuttingConcerns/Pdf/IPdfWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
15 changes: 0 additions & 15 deletions
15
src/Monolith/ClassifiedAds.CrossCuttingConcerns/PdfConverter/IPdfConverter.cs
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/Monolith/ClassifiedAds.Infrastructure/Html/ExportProductsToHtmlHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using RazorLight; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Html; | ||
|
||
public class ExportProductsToHtmlHandler : IHtmlWriter<ExportProductsToHtml> | ||
{ | ||
private readonly IRazorLightEngine _razorLightEngine; | ||
|
||
public ExportProductsToHtmlHandler(IRazorLightEngine razorLightEngine) | ||
{ | ||
_razorLightEngine = razorLightEngine; | ||
} | ||
|
||
public async Task WriteAsync(ExportProductsToHtml data, Stream stream) | ||
{ | ||
using var sw = new StreamWriter(stream, Encoding.UTF8); | ||
await sw.WriteAsync(await GetStringAsync(data)); | ||
} | ||
|
||
public async Task<string> GetStringAsync(ExportProductsToHtml data) | ||
{ | ||
var template = Path.Combine(Environment.CurrentDirectory, $"Templates/ProductList.cshtml"); | ||
string html = await _razorLightEngine.CompileRenderAsync(template, data.Products); | ||
return html; | ||
} | ||
} |
11 changes: 6 additions & 5 deletions
11
...tors/HtmlGeneratorCollectionExtensions.cs → ...tructure/Html/HtmlCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/Monolith/ClassifiedAds.Infrastructure/HtmlGenerators/HtmlGenerator.cs
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/Monolith/ClassifiedAds.Infrastructure/Pdf/DinkToPdf/DinkToPdfCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf; | ||
using DinkToPdf; | ||
using DinkToPdf.Contracts; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class DinkToPdfCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDinkToPdfWriters(this IServiceCollection services) | ||
{ | ||
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools())); | ||
services.AddSingleton<IPdfWriter<ExportProductsToPdf>, ExportProductsToPdfHandler>(); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Monolith/ClassifiedAds.Infrastructure/Pdf/PuppeteerSharp/ExportProductsToPdfHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using PuppeteerSharp; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp; | ||
|
||
public class ExportProductsToPdfHandler : IPdfWriter<ExportProductsToPdf> | ||
{ | ||
private readonly IHtmlWriter<ExportProductsToHtml> _htmlWriter; | ||
|
||
public ExportProductsToPdfHandler(IHtmlWriter<ExportProductsToHtml> htmlWriter) | ||
{ | ||
_htmlWriter = htmlWriter; | ||
} | ||
|
||
public async Task<byte[]> GetBytesAsync(ExportProductsToPdf data) | ||
{ | ||
var html = await _htmlWriter.GetStringAsync(new ExportProductsToHtml { Products = data.Products }); | ||
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }); | ||
await using var page = await browser.NewPageAsync(); | ||
await page.SetContentAsync(html); | ||
|
||
var bytes = await page.PdfDataAsync(new PdfOptions | ||
{ | ||
PrintBackground = true, | ||
}); | ||
|
||
return bytes; | ||
} | ||
|
||
public async Task WriteAsync(ExportProductsToPdf data, Stream stream) | ||
{ | ||
using var sw = new BinaryWriter(stream); | ||
sw.Write(await GetBytesAsync(data)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ith/ClassifiedAds.Infrastructure/Pdf/PuppeteerSharp/PuppeteerSharpCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using ClassifiedAds.Application.Products.DTOs; | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp; | ||
using PuppeteerSharp; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class PuppeteerSharpCollectionExtensions | ||
{ | ||
public static IServiceCollection AddPuppeteerSharpPdfWriter(this IServiceCollection services) | ||
{ | ||
var browserFetcher = new BrowserFetcher(); | ||
browserFetcher.DownloadAsync().GetAwaiter().GetResult(); | ||
|
||
services.AddSingleton<IPdfWriter<ExportProductsToPdf>, ExportProductsToPdfHandler>(); | ||
|
||
return services; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
...ifiedAds.Infrastructure/PdfConverters/DinkToPdf/DinkToPdfConverterCollectionExtensions.cs
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
...lith/ClassifiedAds.Infrastructure/PdfConverters/PuppeteerSharp/PuppeteerSharpConverter.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.