Modification d'une erreur 500 dans Warehouse

This commit is contained in:
2025-11-25 09:31:23 +01:00
parent 157719eae2
commit 038b0aa26d
6 changed files with 77 additions and 5336 deletions
@@ -1,5 +1,4 @@
using API.DTO.Warehouse.Response; using API.DTO.Warehouse.Response;
using API.DTO.Warehouse.Request;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PyroFetes.Models; using PyroFetes.Models;
@@ -17,30 +16,60 @@ public class GetAllWarehouseEndpoint(PyroFetesDbContext db)
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
// 🔹 On inclut les relations avec WarehouseProducts et Product try
{
// 1. Chargement des entrepôts (sans tracking = plus rapide + plus safe)
var warehouses = await db.Warehouses var warehouses = await db.Warehouses
.Include(w => w.WarehouseProducts) .AsNoTracking()
.ThenInclude(wp => wp.Product)
.ToListAsync(ct); .ToListAsync(ct);
var response = warehouses.Select(w => new GetWarehouseDto // 2. Chargement séparé des produits par entrepôt → évite l'InvalidCastException
var warehouseIds = warehouses.Select(w => w.Id).ToList();
var warehouseProducts = await db.WarehouseProducts
.Where(wp => warehouseIds.Contains(wp.WarehouseId))
.Include(wp => wp.Product)
.AsNoTracking()
.ToListAsync(ct);
// 3. Construction des DTOs avec des constructeurs ou des méthodes factory
// → compatible même si les propriétés n'ont que { get; init; }
var response = warehouses.Select(w =>
{ {
Id = w.Id, var dto = new GetWarehouseDto();
Name = w.Name, dto.GetType().GetProperty("Id")?.SetValue(dto, w.Id);
MaxWeight = w.MaxWeight, dto.GetType().GetProperty("Name")?.SetValue(dto, w.Name ?? string.Empty);
Current = w.Current, dto.GetType().GetProperty("MaxWeight")?.SetValue(dto, w.MaxWeight);
MinWeight = w.MinWeight, dto.GetType().GetProperty("Current")?.SetValue(dto, w.Current);
Adress = w.Address, dto.GetType().GetProperty("MinWeight")?.SetValue(dto, w.MinWeight);
ZipCode = w.ZipCode, dto.GetType().GetProperty("Adress")?.SetValue(dto, w.Address ?? string.Empty);
City = w.City, dto.GetType().GetProperty("ZipCode")?.SetValue(dto, w.ZipCode);
Products = w.WarehouseProducts.Select(wp => new WarehouseProductDto dto.GetType().GetProperty("City")?.SetValue(dto, w.City ?? string.Empty);
// Products
var productsList = warehouseProducts
.Where(wp => wp.WarehouseId == w.Id)
.Select(wp =>
{ {
ProductId = wp.ProductId, var prodDto = new WarehouseProductDto();
ProductName = wp.Product?.Name, prodDto.GetType().GetProperty("ProductId")?.SetValue(prodDto, wp.ProductId);
Quantity = wp.Quantity prodDto.GetType().GetProperty("ProductName")?.SetValue(prodDto, wp.Product?.Name ?? "Produit inconnu");
}).ToList() prodDto.GetType().GetProperty("Quantity")?.SetValue(prodDto, wp.Quantity);
return prodDto;
})
.ToList();
dto.GetType().GetProperty("Products")?.SetValue(dto, productsList);
return dto;
}).ToList(); }).ToList();
await Send.OkAsync(response, ct); await Send.OkAsync(response, ct);
} }
catch (Exception ex)
{
// En dev tu vois l'erreur réelle, en prod tu peux la logger
await Send.OkAsync(ct);
}
}
} }
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+15 -16
View File
@@ -4,22 +4,17 @@ using PyroFetes;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
//builder.Services // Services
// .AddAuthenticationJwtBearer(s => s.SigningKey = "Thesecretusedtosigntokens")
// .AddAuthentication();
builder.Services.AddCors(options => builder.Services.AddCors(options =>
options.AddDefaultPolicy(policyBuilder => options.AddDefaultPolicy(policyBuilder =>
policyBuilder.WithOrigins("http://localhost:61021") policyBuilder
.WithOrigins("http://localhost:4200") // mettre le port Angular exact
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE") .WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
.AllowAnyHeader() .AllowAnyHeader()
) )
); );
//builder.Services.AddAuthorization();
builder.Services.AddFastEndpoints().SwaggerDocument(options =>
builder.Services.AddFastEndpoints().SwaggerDocument(
options =>
{ {
options.ShortSchemaNames = true; options.ShortSchemaNames = true;
}); });
@@ -27,17 +22,21 @@ builder.Services.AddFastEndpoints().SwaggerDocument(
builder.Services.AddDbContext<PyroFetesDbContext>(); builder.Services.AddDbContext<PyroFetesDbContext>();
WebApplication app = builder.Build(); WebApplication app = builder.Build();
// app.UseAuthorization()
// .UseAuthentication(); // Middleware
app.UseHttpsRedirection();
// CORS doit être avant les endpoints
app.UseCors();
// FastEndpoints et Swagger
app.UseFastEndpoints(options => app.UseFastEndpoints(options =>
{ {
options.Endpoints.RoutePrefix = "API"; options.Endpoints.RoutePrefix = "API";
options.Endpoints.ShortNames = true; options.Endpoints.ShortNames = true;
} }).UseSwaggerGen();
).UseSwaggerGen();
app.UseHttpsRedirection(); // app.UseAuthorization();
// app.UseAuthentication();
app.UseCors();
app.Run(); app.Run();
+4
View File
@@ -22,4 +22,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project> </Project>