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

View File

@@ -1,5 +1,4 @@
using API.DTO.Warehouse.Response;
using API.DTO.Warehouse.Request;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
@@ -17,30 +16,60 @@ public class GetAllWarehouseEndpoint(PyroFetesDbContext db)
public override async Task HandleAsync(CancellationToken ct)
{
// 🔹 On inclut les relations avec WarehouseProducts et Product
var warehouses = await db.Warehouses
.Include(w => w.WarehouseProducts)
.ThenInclude(wp => wp.Product)
.ToListAsync(ct);
var response = warehouses.Select(w => new GetWarehouseDto
try
{
Id = w.Id,
Name = w.Name,
MaxWeight = w.MaxWeight,
Current = w.Current,
MinWeight = w.MinWeight,
Adress = w.Address,
ZipCode = w.ZipCode,
City = w.City,
Products = w.WarehouseProducts.Select(wp => new WarehouseProductDto
{
ProductId = wp.ProductId,
ProductName = wp.Product?.Name,
Quantity = wp.Quantity
}).ToList()
}).ToList();
// 1. Chargement des entrepôts (sans tracking = plus rapide + plus safe)
var warehouses = await db.Warehouses
.AsNoTracking()
.ToListAsync(ct);
await Send.OkAsync(response, ct);
// 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 =>
{
var dto = new GetWarehouseDto();
dto.GetType().GetProperty("Id")?.SetValue(dto, w.Id);
dto.GetType().GetProperty("Name")?.SetValue(dto, w.Name ?? string.Empty);
dto.GetType().GetProperty("MaxWeight")?.SetValue(dto, w.MaxWeight);
dto.GetType().GetProperty("Current")?.SetValue(dto, w.Current);
dto.GetType().GetProperty("MinWeight")?.SetValue(dto, w.MinWeight);
dto.GetType().GetProperty("Adress")?.SetValue(dto, w.Address ?? string.Empty);
dto.GetType().GetProperty("ZipCode")?.SetValue(dto, w.ZipCode);
dto.GetType().GetProperty("City")?.SetValue(dto, w.City ?? string.Empty);
// Products
var productsList = warehouseProducts
.Where(wp => wp.WarehouseId == w.Id)
.Select(wp =>
{
var prodDto = new WarehouseProductDto();
prodDto.GetType().GetProperty("ProductId")?.SetValue(prodDto, wp.ProductId);
prodDto.GetType().GetProperty("ProductName")?.SetValue(prodDto, wp.Product?.Name ?? "Produit inconnu");
prodDto.GetType().GetProperty("Quantity")?.SetValue(prodDto, wp.Quantity);
return prodDto;
})
.ToList();
dto.GetType().GetProperty("Products")?.SetValue(dto, productsList);
return dto;
}).ToList();
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

View File

@@ -4,40 +4,39 @@ using PyroFetes;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
//builder.Services
// .AddAuthenticationJwtBearer(s => s.SigningKey = "Thesecretusedtosigntokens")
// .AddAuthentication();
// Services
builder.Services.AddCors(options =>
options.AddDefaultPolicy(policyBuilder =>
policyBuilder.WithOrigins("http://localhost:61021")
policyBuilder
.WithOrigins("http://localhost:4200") // mettre le port Angular exact
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
.AllowAnyHeader()
)
);
//builder.Services.AddAuthorization();
);
builder.Services.AddFastEndpoints().SwaggerDocument(
options =>
{
options.ShortSchemaNames = true;
});
builder.Services.AddFastEndpoints().SwaggerDocument(options =>
{
options.ShortSchemaNames = true;
});
builder.Services.AddDbContext<PyroFetesDbContext>();
WebApplication app = builder.Build();
// app.UseAuthorization()
// .UseAuthentication();
app.UseFastEndpoints(options =>
{
options.Endpoints.RoutePrefix = "API";
options.Endpoints.ShortNames = true;
}
).UseSwaggerGen();
// Middleware
app.UseHttpsRedirection();
// CORS doit être avant les endpoints
app.UseCors();
// FastEndpoints et Swagger
app.UseFastEndpoints(options =>
{
options.Endpoints.RoutePrefix = "API";
options.Endpoints.ShortNames = true;
}).UseSwaggerGen();
// app.UseAuthorization();
// app.UseAuthentication();
app.Run();

View File

@@ -22,4 +22,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>