forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using API.DTO.Supplier.Request;
|
||||
using API.DTO.Supplier.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateSupplierDto, GetSupplierDto>
|
||||
public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
: Endpoint<CreateSupplierDto, GetSupplierDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,8 +17,7 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct)
|
||||
{
|
||||
// Création d'un nouvel objet Supplier
|
||||
Models.Supplier supplier = new()
|
||||
var supplier = new Models.Supplier
|
||||
{
|
||||
Name = req.Name,
|
||||
Email = req.Email,
|
||||
@@ -24,15 +26,27 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
ZipCode = req.ZipCode,
|
||||
City = req.City
|
||||
};
|
||||
|
||||
// Ajout à la base et sauvegarde
|
||||
|
||||
pyrofetesdbcontext.Suppliers.Add(supplier);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
Console.WriteLine("Fournisseur créé avec succès !");
|
||||
|
||||
// Préparation de la réponse
|
||||
GetSupplierDto responseDto = new()
|
||||
// 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,
|
||||
@@ -43,6 +57,6 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
City = supplier.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user