using PyroFetes.DTO.Supplier.Request; using PyroFetes.DTO.Supplier.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.Models; namespace PyroFetes.Endpoints.Supplier; public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint { public override void Configure() { Post("/api/suppliers"); AllowAnonymous(); } public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct) { var supplier = new Models.Supplier { Name = req.Name, Email = req.Email, Phone = req.PhoneNumber, Address = req.Adress, ZipCode = req.ZipCode, City = req.City }; pyrofetesdbcontext.Suppliers.Add(supplier); await pyrofetesdbcontext.SaveChangesAsync(ct); // Ajout des liaisons Price si produits renseignés if (req.Products is not null && req.Products.Any()) { foreach (var p in req.Products) { var price = new Price { SupplierId = supplier.Id, ProductId = p.ProductId, SellingPrice = p.SellingPrice }; pyrofetesdbcontext.Prices.Add(price); } await pyrofetesdbcontext.SaveChangesAsync(ct); } var response = new GetSupplierDto { Id = supplier.Id, Name = supplier.Name, Email = supplier.Email, PhoneNumber = supplier.Phone, Adress = supplier.Address, ZipCode = supplier.ZipCode, City = supplier.City }; await Send.OkAsync(response, ct); } }