AJout des DTO et endpoint sur le nouveau git

This commit is contained in:
2025-10-08 15:46:36 +02:00
parent 04cb47802b
commit c729af3d32
66 changed files with 1703 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
using API.DTO.Supplier.Request;
using API.DTO.Supplier.Response;
using FastEndpoints;
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)
{
// Création d'un nouvel objet Supplier
Models.Supplier supplier = new()
{
Name = req.Name,
Email = req.Email,
Phone = req.PhoneNumber,
Address = req.Adress,
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()
{
Id = supplier.Id,
Name = supplier.Name,
Email = supplier.Email,
PhoneNumber = supplier.Phone,
Adress = supplier.Address,
ZipCode = supplier.ZipCode,
City = supplier.City
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,37 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Supplier;
public class DeleteSupplierRequest
{
public int Id { get; set; }
}
public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteSupplierRequest>
{
public override void Configure()
{
Delete("/api/suppliers/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
{
Models.Supplier? supplierToDelete = await pyrofetesdbcontext
.Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
if (supplierToDelete == null)
{
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
pyrofetesdbcontext.Suppliers.Remove(supplierToDelete);
await pyrofetesdbcontext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,32 @@
using API.DTO.Supplier.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Supplier;
public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetSupplierDto>>
{
public override void Configure()
{
Get("/api/suppliers");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetSupplierDto> responseDto = await pyrofetesdbcontext.Suppliers
.Select(s => new GetSupplierDto
{
Id = s.Id,
Name = s.Name!,
Email = s.Email!,
PhoneNumber = s.Phone!,
Adress = s.Address!,
ZipCode = s.ZipCode,
City = s.City!
})
.ToListAsync(ct);
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,46 @@
using API.DTO.Supplier.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Supplier;
public class GetSupplierRequest
{
public int Id { get; set; }
}
public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetSupplierRequest, GetSupplierDto>
{
public override void Configure()
{
Get("/api/suppliers/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct)
{
Models.Supplier? supplier = await pyrofetesdbcontext
.Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
if (supplier == null)
{
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
GetSupplierDto responseDto = new()
{
Id = supplier.Id,
Name = supplier.Name!,
Email = supplier.Email!,
PhoneNumber = supplier.Phone!,
Adress = supplier.Address!,
ZipCode = supplier.ZipCode,
City = supplier.City!
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,52 @@
using API.DTO.Supplier.Request;
using API.DTO.Supplier.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Supplier;
public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateSupplierDto, GetSupplierDto>
{
public override void Configure()
{
Put("/api/suppliers/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
{
Models.Supplier? supplierToEdit = await pyrofetesdbcontext
.Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
if (supplierToEdit == null)
{
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
// Mise à jour des propriétés
supplierToEdit.Name = req.Name;
supplierToEdit.Email = req.Email;
supplierToEdit.Phone = req.PhoneNumber;
supplierToEdit.Address = req.Adress;
supplierToEdit.ZipCode = req.ZipCode;
supplierToEdit.City = req.City;
await pyrofetesdbcontext.SaveChangesAsync(ct);
GetSupplierDto responseDto = new()
{
Id = supplierToEdit.Id,
Name = supplierToEdit.Name,
Email = supplierToEdit.Email,
PhoneNumber = supplierToEdit.Phone,
Adress = supplierToEdit.Address,
ZipCode = supplierToEdit.ZipCode,
City = supplierToEdit.City
};
await Send.OkAsync(responseDto, ct);
}
}