forked from sanchezvem/PyroFetes
35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using API.DTO.Brand.Response;
|
|
using FastEndpoints;
|
|
using PyroFetes.DTO.Brand.Request;
|
|
|
|
namespace PyroFetes.Endpoints.Brand;
|
|
|
|
public class CreateBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateBrandDto, GetBrandDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateBrandDto et l'élement de réponse GetBrandDto
|
|
{
|
|
public override void Configure() //Configuration de l'endpoint
|
|
{
|
|
Post("/api/brands"); //Créer une marque
|
|
AllowAnonymous(); //Autorise l'accès sans authentification
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateBrandDto req, CancellationToken ct)
|
|
{
|
|
|
|
Models.Brand brand = new () //Création d'un nom rentré par l'utilisateur
|
|
{
|
|
Name = req.Name
|
|
};
|
|
|
|
pyrofetesdbcontext.Brands.Add(brand); //Ajout de la marque à la bdd
|
|
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde de la marque dans la bdd
|
|
|
|
Console.WriteLine("Marque créé avec succès !");
|
|
|
|
GetBrandDto responseDto = new () //Renvoie le nom
|
|
{
|
|
Name = req.Name
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct); //Réponse au client
|
|
}
|
|
} |