forked from sanchezvem/PyroFetes
62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
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 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);
|
|
}
|
|
} |