forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
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 class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
: Endpoint<CreateSupplierDto, GetSupplierDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,8 +17,7 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct)
|
||||
{
|
||||
// Création d'un nouvel objet Supplier
|
||||
Models.Supplier supplier = new()
|
||||
var supplier = new Models.Supplier
|
||||
{
|
||||
Name = req.Name,
|
||||
Email = req.Email,
|
||||
@@ -24,15 +26,27 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
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()
|
||||
// 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,
|
||||
@@ -43,6 +57,6 @@ public class CreateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
City = supplier.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
@@ -18,19 +19,32 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplierToDelete = await pyrofetesdbcontext
|
||||
var supplierToDelete = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
|
||||
if (supplierToDelete == null)
|
||||
if (supplierToDelete is null)
|
||||
{
|
||||
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Supprimer les liaisons Price avant le fournisseur
|
||||
var relatedPrices = await pyrofetesdbcontext.Prices
|
||||
.Where(p => p.SupplierId == req.Id)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (relatedPrices.Any())
|
||||
{
|
||||
pyrofetesdbcontext.Prices.RemoveRange(relatedPrices);
|
||||
}
|
||||
|
||||
// Supprimer le fournisseur
|
||||
pyrofetesdbcontext.Suppliers.Remove(supplierToDelete);
|
||||
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
Console.WriteLine($"Fournisseur {req.Id} et ses prix liés supprimés avec succès.");
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
|
@@ -1,10 +1,13 @@
|
||||
using API.DTO.Supplier.Response;
|
||||
using API.DTO.Supplier.Request;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetSupplierDto>>
|
||||
public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
: EndpointWithoutRequest<List<GetSupplierDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,19 +17,28 @@ public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
|
||||
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!
|
||||
})
|
||||
var suppliers = await pyrofetesdbcontext.Suppliers
|
||||
.Include(s => s.Prices)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var responseDto = 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!,
|
||||
|
||||
// 🔹 Liste des produits liés via Price
|
||||
Products = s.Prices.Select(pr => new SupplierProductPriceDto
|
||||
{
|
||||
ProductId = pr.ProductId,
|
||||
SellingPrice = pr.SellingPrice
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using API.DTO.Supplier.Response;
|
||||
using API.DTO.Supplier.Request;
|
||||
using API.DTO.Supplier.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -9,7 +10,8 @@ public class GetSupplierRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetSupplierRequest, GetSupplierDto>
|
||||
public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext)
|
||||
: Endpoint<GetSupplierRequest, GetSupplierDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -19,9 +21,9 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
|
||||
public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplier = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
var supplier = await pyrofetesdbcontext.Suppliers
|
||||
.Include(s => s.Prices)
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
|
||||
if (supplier == null)
|
||||
{
|
||||
@@ -30,7 +32,7 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
return;
|
||||
}
|
||||
|
||||
GetSupplierDto responseDto = new()
|
||||
var responseDto = new GetSupplierDto
|
||||
{
|
||||
Id = supplier.Id,
|
||||
Name = supplier.Name!,
|
||||
@@ -38,7 +40,14 @@ public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoi
|
||||
PhoneNumber = supplier.Phone!,
|
||||
Adress = supplier.Address!,
|
||||
ZipCode = supplier.ZipCode,
|
||||
City = supplier.City!
|
||||
City = supplier.City!,
|
||||
|
||||
// Produits liés
|
||||
Products = supplier.Prices.Select(p => new SupplierProductPriceDto
|
||||
{
|
||||
ProductId = p.ProductId,
|
||||
SellingPrice = p.SellingPrice
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using API.DTO.Supplier.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
@@ -15,18 +16,16 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplierToEdit = await pyrofetesdbcontext
|
||||
var supplierToEdit = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
.Include(s => s.Prices)
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
|
||||
if (supplierToEdit == null)
|
||||
if (supplierToEdit is 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;
|
||||
@@ -34,9 +33,28 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
supplierToEdit.ZipCode = req.ZipCode;
|
||||
supplierToEdit.City = req.City;
|
||||
|
||||
if (req.Products is not null)
|
||||
{
|
||||
// Supprimer les anciennes liaisons
|
||||
var existingPrices = pyrofetesdbcontext.Prices
|
||||
.Where(p => p.SupplierId == supplierToEdit.Id);
|
||||
pyrofetesdbcontext.Prices.RemoveRange(existingPrices);
|
||||
|
||||
// Ajouter les nouvelles liaisons
|
||||
foreach (var p in req.Products)
|
||||
{
|
||||
pyrofetesdbcontext.Prices.Add(new Price
|
||||
{
|
||||
SupplierId = supplierToEdit.Id,
|
||||
ProductId = p.ProductId,
|
||||
SellingPrice = p.SellingPrice
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetSupplierDto responseDto = new()
|
||||
var response = new GetSupplierDto
|
||||
{
|
||||
Id = supplierToEdit.Id,
|
||||
Name = supplierToEdit.Name,
|
||||
@@ -47,6 +65,6 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
City = supplierToEdit.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user