forked from sanchezvem/PyroFetes
57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using FastEndpoints;
|
|
using PyroFetes.DTO.Product.Request;
|
|
using PyroFetes.DTO.Product.Response;
|
|
|
|
namespace PyroFetes.Endpoints.Product;
|
|
|
|
public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductDto, GetProductDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/products");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
|
|
{
|
|
Models.Product product = new ()
|
|
{
|
|
References = req.References,
|
|
Name = req.Name!,
|
|
Duration = req.Duration,
|
|
Caliber = req.Caliber,
|
|
ApprovalNumber = req.ApprovalNumber,
|
|
Weight = req.Weight,
|
|
Nec = req.Nec,
|
|
SellingPrice = req.SellingPrice,
|
|
Image = req.Image!,
|
|
Link = req.Link!,
|
|
ProductCategoryId = req.ProductCategoryId,
|
|
ClassificationId = req.ClassificationId
|
|
};
|
|
|
|
pyrofetesdbcontext.Products.Add(product);
|
|
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
|
|
|
Console.WriteLine("Product créé avec succès !");
|
|
|
|
GetProductDto responseDto = new ()
|
|
{
|
|
Id = product.Id,
|
|
Reference = req.References,
|
|
Name = req.Name,
|
|
Duration = req.Duration,
|
|
Caliber = req.Caliber,
|
|
ApprovalNumber = req.ApprovalNumber,
|
|
Weight = req.Weight,
|
|
Nec = req.Nec,
|
|
SellingPrice = req.SellingPrice,
|
|
Image = req.Image,
|
|
Link = req.Link,
|
|
ProductCategoryId = req.ProductCategoryId,
|
|
ClassificationId = req.ClassificationId
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
} |