forked from sanchezvem/PyroFetes
Modification d'une erreur 500 dans Warehouse
This commit is contained in:
@@ -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
|
||||||
var warehouses = await db.Warehouses
|
|
||||||
.Include(w => w.WarehouseProducts)
|
|
||||||
.ThenInclude(wp => wp.Product)
|
|
||||||
.ToListAsync(ct);
|
|
||||||
|
|
||||||
var response = warehouses.Select(w => new GetWarehouseDto
|
|
||||||
{
|
{
|
||||||
Id = w.Id,
|
// 1. Chargement des entrepôts (sans tracking = plus rapide + plus safe)
|
||||||
Name = w.Name,
|
var warehouses = await db.Warehouses
|
||||||
MaxWeight = w.MaxWeight,
|
.AsNoTracking()
|
||||||
Current = w.Current,
|
.ToListAsync(ct);
|
||||||
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();
|
|
||||||
|
|
||||||
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
@@ -4,40 +4,39 @@ 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;
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.Services.AddDbContext<PyroFetesDbContext>();
|
builder.Services.AddDbContext<PyroFetesDbContext>();
|
||||||
|
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
// app.UseAuthorization()
|
|
||||||
// .UseAuthentication();
|
|
||||||
app.UseFastEndpoints(options =>
|
|
||||||
{
|
|
||||||
options.Endpoints.RoutePrefix = "API";
|
|
||||||
options.Endpoints.ShortNames = true;
|
|
||||||
}
|
|
||||||
).UseSwaggerGen();
|
|
||||||
|
|
||||||
|
// Middleware
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
// CORS doit être avant les endpoints
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
|
|
||||||
|
// FastEndpoints et Swagger
|
||||||
|
app.UseFastEndpoints(options =>
|
||||||
|
{
|
||||||
|
options.Endpoints.RoutePrefix = "API";
|
||||||
|
options.Endpoints.ShortNames = true;
|
||||||
|
}).UseSwaggerGen();
|
||||||
|
|
||||||
|
// app.UseAuthorization();
|
||||||
|
// app.UseAuthentication();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user