use bundled http logging feature instead of hand-made middleware

tests
maxime 1 year ago
parent 19ec55009b
commit 9af2167c14

@ -16,6 +16,9 @@ var config = builder.Configuration;
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddHttpLogging(o => {});
builder.Services.AddControllers() builder.Services.AddControllers()
.ConfigureApiBehaviorOptions(options => options.InvalidModelStateResponseFactory = context => .ConfigureApiBehaviorOptions(options => options.InvalidModelStateResponseFactory = context =>
new BadRequestObjectResult(context.ModelState) new BadRequestObjectResult(context.ModelState)
@ -53,16 +56,17 @@ builder.Services.AddScoped<IUserService, DbUserService>();
builder.Services.AddScoped<ITeamService, DbTeamService>(); builder.Services.AddScoped<ITeamService, DbTeamService>();
builder.Services.AddScoped<ITacticService, DbTacticService>(); builder.Services.AddScoped<ITacticService, DbTacticService>();
var app = builder.Build(); var app = builder.Build();
System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
app.Use((context, next) => // app.Use((context, next) =>
{ // {
var req = context.Request; // var req = context.Request;
Console.WriteLine($"{req.Method}: {req.Path}{req.QueryString}"); // Console.WriteLine($"{req.Method}: {req.Path}{req.QueryString}");
return next.Invoke(); // return next.Invoke();
}); // });
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
@ -71,6 +75,7 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.UseHttpLogging();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseCors("cors"); app.UseCors("cors");

@ -136,28 +136,23 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
public async Task<bool> RemoveTacticStep(int tacticId, int stepId) public async Task<bool> RemoveTacticStep(int tacticId, int stepId)
{ {
var toRemove = new Stack<int>(); var toRemove = new Stack<TacticStepEntity>();
toRemove.Push(stepId);
while (toRemove.TryPop(out var id)) var beginStep = await context
{ .TacticSteps
var step = await context.TacticSteps .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId);
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == id);
if (step == null) if (beginStep == null)
{
if (id == stepId)
return false; return false;
throw new Exception(
$"step contains a children that does not exists in the database ({tacticId} / {id})"
);
}
var stepChildren = context.TacticSteps.Where(s => s.ParentId == id); toRemove.Push(beginStep);
foreach (var stepChild in stepChildren)
while (toRemove.TryPop(out var step))
{ {
toRemove.Push(stepChild.StepId);
} await context.TacticSteps
.Where(s => s.TacticId == tacticId && s.ParentId == step.StepId)
.ForEachAsync(toRemove.Push);
context.Remove(step); context.Remove(step);
} }

Loading…
Cancel
Save