Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Product/GetProductEndpoint.cs

56 lines
1.7 KiB
C#

using API.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Product;
public class GetProductRequest
{
public int Id { get; set; }
}
public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetProductRequest, GetProductDto>
{
public override void Configure()
{
Get("/api/product/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
{
Models.Product? product = await pyrofetesdbcontext
.Products.Include(product => product.Classification).Include(product => product.ProductCategory)
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
if (product == null)
{
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
GetProductDto responseDto = new()
{
Id = product.Id,
Reference = product.References,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
SellingPrice = product.SellingPrice,
Image = product.Image,
Link = product.Link,
ClassificationId = product.ClassificationId,
ClassificationLabel = product.Classification!.Label,
ProductCategoryId = product.ProductCategoryId,
ProductCategoryLabel = product.ProductCategory!.Label,
};
await Send.OkAsync(responseDto, ct);
}
}