From f54f5e02dcc004dd7e99a1975dcf700e624fabf9 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Tue, 9 Jun 2026 20:11:33 +0200 Subject: [PATCH] Last changes before exam --- .../GetDeliveryNotePdfEndpoint.cs | 11 +++++++--- .../GetPurchaseOrderPdfEndpoint.cs | 8 ++++--- .../Quotations/GetQuotationPdfEndpoint.cs | 8 ++++--- PyroFetes/Program.cs | 6 +++--- PyroFetes/PyroFetesDbContext.cs | 2 +- .../Services/Pdf/DeliveryNotePdfService.cs | 19 +++++++---------- .../Services/Pdf/PurchaseOrderPdfService.cs | 21 ++++++++----------- PyroFetes/Services/Pdf/QuotationPdfService.cs | 21 ++++++++----------- PyroFetes/Services/StorageService.cs | 8 +++---- 9 files changed, 52 insertions(+), 52 deletions(-) diff --git a/PyroFetes/Endpoints/DeliveryNotes/GetDeliveryNotePdfEndpoint.cs b/PyroFetes/Endpoints/DeliveryNotes/GetDeliveryNotePdfEndpoint.cs index dfacbf68..f22ba3ae 100644 --- a/PyroFetes/Endpoints/DeliveryNotes/GetDeliveryNotePdfEndpoint.cs +++ b/PyroFetes/Endpoints/DeliveryNotes/GetDeliveryNotePdfEndpoint.cs @@ -3,18 +3,23 @@ using FastEndpoints; using PyroFetes.DTO.DeliveryNote.Request; using PyroFetes.Models; using PyroFetes.Repositories; +using PyroFetes.Services; using PyroFetes.Services.Pdf; using PyroFetes.Specifications.DeliveryNotes; namespace PyroFetes.Endpoints.DeliveryNotes; -public class GetDeliveryNotePdfEndpoint(DeliveryNotesRepository deliveryNotesRepository, IDeliveryNotePdfService deliveryNotePdfService, SettingsRepository settingsRepository) +public class GetDeliveryNotePdfEndpoint( + DeliveryNotesRepository deliveryNotesRepository, + DeliveryNotePdfService deliveryNotePdfService, + SettingsRepository settingsRepository, + StorageService storageService) : Endpoint { public override void Configure() { Get("/deliveryNotes/{@Id}/pdf", x => new { x.Id }); - Roles("Admin","Employe"); + Roles("Admin", "Employe"); Description(b => b.Produces(200, MediaTypeNames.Application.Pdf)); } @@ -31,7 +36,7 @@ public class GetDeliveryNotePdfEndpoint(DeliveryNotesRepository deliveryNotesRep return; } - byte[] bytes = deliveryNotePdfService.Generate(deliveryNote, deliveryNote.ProductDeliveries!, setting!); + byte[] bytes = await deliveryNotePdfService.Generate(deliveryNote, setting!, storageService); await Send.BytesAsync( bytes: bytes, diff --git a/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderPdfEndpoint.cs b/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderPdfEndpoint.cs index a5328749..c8285bee 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderPdfEndpoint.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/GetPurchaseOrderPdfEndpoint.cs @@ -3,6 +3,7 @@ using FastEndpoints; using PyroFetes.DTO.PurchaseOrder.Request; using PyroFetes.Models; using PyroFetes.Repositories; +using PyroFetes.Services; using PyroFetes.Services.Pdf; using PyroFetes.Specifications.PurchaseOrders; @@ -10,8 +11,9 @@ namespace PyroFetes.Endpoints.PurchaseOrders; public class GetPurchaseOrderPdfEndpoint( PurchaseOrdersRepository purchaseOrdersRepository, - IPurchaseOrderPdfService purchaseOrderPdfService, - SettingsRepository settingsRepository) + PurchaseOrderPdfService purchaseOrderPdfService, + SettingsRepository settingsRepository, + StorageService storageService) : Endpoint { public override void Configure() @@ -33,7 +35,7 @@ public class GetPurchaseOrderPdfEndpoint( Setting? setting = await settingsRepository.FirstOrDefaultAsync(ct); - byte[] bytes = purchaseOrderPdfService.Generate(purchaseOrder, purchaseOrder.PurchaseProducts!, setting!); + byte[] bytes = await purchaseOrderPdfService.Generate(purchaseOrder, setting!, storageService); await Send.BytesAsync( bytes: bytes, diff --git a/PyroFetes/Endpoints/Quotations/GetQuotationPdfEndpoint.cs b/PyroFetes/Endpoints/Quotations/GetQuotationPdfEndpoint.cs index 4b015d96..a5702d66 100644 --- a/PyroFetes/Endpoints/Quotations/GetQuotationPdfEndpoint.cs +++ b/PyroFetes/Endpoints/Quotations/GetQuotationPdfEndpoint.cs @@ -3,6 +3,7 @@ using FastEndpoints; using PyroFetes.DTO.Quotation.Request; using PyroFetes.Models; using PyroFetes.Repositories; +using PyroFetes.Services; using PyroFetes.Services.Pdf; using PyroFetes.Specifications.Quotations; @@ -10,8 +11,9 @@ namespace PyroFetes.Endpoints.Quotations; public class GetQuotationPdfEndpoint( QuotationsRepository quotationRepository, - IQuotationPdfService quotationPdfService, - SettingsRepository settingsRepository) + QuotationPdfService quotationPdfService, + SettingsRepository settingsRepository, + StorageService storageService) : Endpoint { public override void Configure() @@ -33,7 +35,7 @@ public class GetQuotationPdfEndpoint( Setting? setting = await settingsRepository.FirstOrDefaultAsync(ct); - byte[] bytes = quotationPdfService.Generate(quotation, quotation.QuotationProducts!, setting!); + byte[] bytes = await quotationPdfService.Generate(quotation, setting!, storageService); await Send.BytesAsync( bytes: bytes, diff --git a/PyroFetes/Program.cs b/PyroFetes/Program.cs index 86a80861..f17fba15 100644 --- a/PyroFetes/Program.cs +++ b/PyroFetes/Program.cs @@ -56,9 +56,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); // Ajout des services -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); MapperConfiguration mappingConfig = new(mc => diff --git a/PyroFetes/PyroFetesDbContext.cs b/PyroFetes/PyroFetesDbContext.cs index 9a278ace..e4f2b61a 100644 --- a/PyroFetes/PyroFetesDbContext.cs +++ b/PyroFetes/PyroFetesDbContext.cs @@ -64,7 +64,7 @@ public class PyroFetesDbContext : DbContext { string connectionString = "Server=romaric-thibault.fr;" + - "Database=PyroFetes-Sujet2;" + + "Database=PyroFetes-Sujet2-Cristiano;" + "User Id=pyrofetes;" + "Password=Crablike8-Fringe-Swimmable;" + "TrustServerCertificate=true;"; diff --git a/PyroFetes/Services/Pdf/DeliveryNotePdfService.cs b/PyroFetes/Services/Pdf/DeliveryNotePdfService.cs index 9c20eff7..6a222685 100644 --- a/PyroFetes/Services/Pdf/DeliveryNotePdfService.cs +++ b/PyroFetes/Services/Pdf/DeliveryNotePdfService.cs @@ -5,17 +5,14 @@ using QuestPDF.Infrastructure; namespace PyroFetes.Services.Pdf; -public interface IDeliveryNotePdfService +public class DeliveryNotePdfService { - byte[] Generate(DeliveryNote deliveryNote, List lignes, Setting setting); -} + private static readonly HttpClient HttpClient = new(); -public class DeliveryNotePdfService : IDeliveryNotePdfService -{ - public byte[] Generate(DeliveryNote deliveryNote, List lignes, Setting setting) + public async Task Generate(DeliveryNote deliveryNote, Setting setting, StorageService storageService) { - byte[] logo = Convert.FromBase64String(setting.Logo!); - byte[] signature = Convert.FromBase64String(setting.ElectronicSignature!); + byte[] logoBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.Logo!)); + byte[] signatureBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.ElectronicSignature!)); decimal total = 0; int totalQuantity = 0; Document document = Document.Create(container => @@ -48,7 +45,7 @@ public class DeliveryNotePdfService : IDeliveryNotePdfService // Logo + société à droite row.ConstantItem(200).Column(col => { - col.Item().AlignRight().Height(70).Image(logo, ImageScaling.FitArea); + col.Item().AlignRight().Height(70).Image(logoBytes, ImageScaling.FitArea); col.Item().Height(20); col.Item().AlignRight().Text("Pyro-Fêtes").SemiBold(); col.Item().Height(5); @@ -93,7 +90,7 @@ public class DeliveryNotePdfService : IDeliveryNotePdfService header.Cell().Element(CellHeader).AlignRight().Text("Total"); }); - foreach (ProductDelivery l in lignes) + foreach (ProductDelivery l in deliveryNote.ProductDeliveries!) { decimal price = l.Product!.Prices! .FirstOrDefault(x => x.SupplierId == l.DeliveryNote!.SupplierId && x.ProductId == l.ProductId) @@ -125,7 +122,7 @@ public class DeliveryNotePdfService : IDeliveryNotePdfService }); // Signature en bas à droite - page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signature, ImageScaling.FitArea); }); + page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signatureBytes, ImageScaling.FitArea); }); }); }); diff --git a/PyroFetes/Services/Pdf/PurchaseOrderPdfService.cs b/PyroFetes/Services/Pdf/PurchaseOrderPdfService.cs index 56ef8810..1dfe693f 100644 --- a/PyroFetes/Services/Pdf/PurchaseOrderPdfService.cs +++ b/PyroFetes/Services/Pdf/PurchaseOrderPdfService.cs @@ -5,17 +5,14 @@ using QuestPDF.Infrastructure; namespace PyroFetes.Services.Pdf; -public interface IPurchaseOrderPdfService +public class PurchaseOrderPdfService { - byte[] Generate(PurchaseOrder purchaseOrder, List lignes, Setting setting); -} - -public class PurchaseOrderPdfService : IPurchaseOrderPdfService -{ - public byte[] Generate(PurchaseOrder purchaseOrder, List lignes, Setting setting) + private static readonly HttpClient HttpClient = new(); + + public async Task Generate(PurchaseOrder purchaseOrder, Setting setting, StorageService storageService) { - byte[] logo = Convert.FromBase64String(setting.Logo!); - byte[] signature = Convert.FromBase64String(setting.ElectronicSignature!); + byte[] logoBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.Logo!)); + byte[] signatureBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.ElectronicSignature!)); int totalQuantity = 0; decimal total = 0; Document document = Document.Create(container => @@ -45,7 +42,7 @@ public class PurchaseOrderPdfService : IPurchaseOrderPdfService // Logo + société à droite row.ConstantItem(200).Column(col => { - col.Item().AlignRight().Height(70).Image(logo, ImageScaling.FitArea); + col.Item().AlignRight().Height(70).Image(logoBytes, ImageScaling.FitArea); col.Item().Height(20); col.Item().AlignRight().Text("Pyro-Fêtes").SemiBold(); col.Item().Height(5); @@ -93,7 +90,7 @@ public class PurchaseOrderPdfService : IPurchaseOrderPdfService header.Cell().Element(CellHeader).AlignRight().Text("Total"); }); - foreach (PurchaseProduct l in lignes) + foreach (PurchaseProduct l in purchaseOrder.PurchaseProducts!) { decimal price = l.Product!.Prices! .FirstOrDefault(x => x.SupplierId == l.PurchaseOrder!.SupplierId && x.ProductId == l.ProductId) @@ -141,7 +138,7 @@ public class PurchaseOrderPdfService : IPurchaseOrderPdfService }); // Signature en bas à droite - page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signature, ImageScaling.FitArea); }); + page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signatureBytes, ImageScaling.FitArea); }); }); }); diff --git a/PyroFetes/Services/Pdf/QuotationPdfService.cs b/PyroFetes/Services/Pdf/QuotationPdfService.cs index 94569323..ab3a096b 100644 --- a/PyroFetes/Services/Pdf/QuotationPdfService.cs +++ b/PyroFetes/Services/Pdf/QuotationPdfService.cs @@ -5,17 +5,14 @@ using QuestPDF.Infrastructure; namespace PyroFetes.Services.Pdf; -public interface IQuotationPdfService +public class QuotationPdfService { - byte[] Generate(Quotation quotation, List lignes, Setting setting); -} + private static readonly HttpClient HttpClient = new(); -public class QuotationPdfService : IQuotationPdfService -{ - public byte[] Generate(Quotation quotation, List lignes, Setting setting) + public async Task Generate(Quotation quotation, Setting setting, StorageService storageService) { - byte[] logo = Convert.FromBase64String(setting.Logo!); - byte[] signature = Convert.FromBase64String(setting.ElectronicSignature!); + byte[] logoBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.Logo!)); + byte[] signatureBytes = await HttpClient.GetByteArrayAsync(storageService.GetUrl(setting.ElectronicSignature!)); decimal total = 0; Document document = Document.Create(container => { @@ -42,7 +39,7 @@ public class QuotationPdfService : IQuotationPdfService // Logo + société à droite row.ConstantItem(200).Column(col => { - col.Item().AlignRight().Height(70).Image(logo, ImageScaling.FitArea); + col.Item().AlignRight().Height(70).Image(logoBytes, ImageScaling.FitArea); col.Item().Height(20); col.Item().AlignRight().Text("Pyro-Fêtes").SemiBold(); col.Item().Height(5); @@ -90,7 +87,7 @@ public class QuotationPdfService : IQuotationPdfService header.Cell().Element(CellHeader).AlignRight().Text("Total"); }); - foreach (QuotationProduct l in lignes) + foreach (QuotationProduct l in quotation.QuotationProducts!) { decimal price = l.Product!.Prices! .FirstOrDefault(x => x.SupplierId == l.Quotation!.SupplierId && x.ProductId == l.ProductId) @@ -134,7 +131,7 @@ public class QuotationPdfService : IQuotationPdfService }); // Signature en bas à droite - page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signature, ImageScaling.FitArea); }); + page.Footer().AlignRight().Column(col => { col.Item().AlignRight().Height(100).Image(signatureBytes, ImageScaling.FitArea); }); }); }); @@ -143,4 +140,4 @@ public class QuotationPdfService : IQuotationPdfService return document.GeneratePdf(); } -} \ No newline at end of file +} diff --git a/PyroFetes/Services/StorageService.cs b/PyroFetes/Services/StorageService.cs index 88fc3df0..64b4143f 100644 --- a/PyroFetes/Services/StorageService.cs +++ b/PyroFetes/Services/StorageService.cs @@ -7,12 +7,12 @@ public class StorageService(IAmazonS3 amazonS3, IConfiguration config) { private readonly string _bucket = config["RustFs:BucketName"]!; private readonly string _url = config["RustFs:ServiceUrl"]!; - + public async Task UploadFile(IFormFile file, string type, CancellationToken ct) { if (file.Length == 0) throw new Exception("Fichier vide"); - - string key = $"settings/{type}"; + + string key = $"settings/{type}/{Guid.NewGuid()}"; using MemoryStream memoryStream = new(); await file.CopyToAsync(memoryStream, ct); @@ -25,7 +25,7 @@ public class StorageService(IAmazonS3 amazonS3, IConfiguration config) InputStream = memoryStream, Key = key, }; - + await amazonS3.PutObjectAsync(uploadRequest, ct); return key; }