From 9af2167c148e31cd06bca3f2edae29a097e9c20f Mon Sep 17 00:00:00 2001 From: maxime Date: Sat, 24 Feb 2024 14:53:55 +0100 Subject: [PATCH] use bundled http logging feature instead of hand-made middleware --- API/Program.cs | 17 +++++++++++------ DbServices/DbTacticService.cs | 35 +++++++++++++++-------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/API/Program.cs b/API/Program.cs index 0c40272..278bca2 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -16,6 +16,9 @@ var config = builder.Configuration; // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); + +builder.Services.AddHttpLogging(o => {}); + builder.Services.AddControllers() .ConfigureApiBehaviorOptions(options => options.InvalidModelStateResponseFactory = context => new BadRequestObjectResult(context.ModelState) @@ -53,16 +56,17 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + var app = builder.Build(); System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); -app.Use((context, next) => -{ - var req = context.Request; - Console.WriteLine($"{req.Method}: {req.Path}{req.QueryString}"); - return next.Invoke(); -}); +// app.Use((context, next) => +// { +// var req = context.Request; +// Console.WriteLine($"{req.Method}: {req.Path}{req.QueryString}"); +// return next.Invoke(); +// }); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) @@ -71,6 +75,7 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } +app.UseHttpLogging(); app.UseHttpsRedirection(); app.UseCors("cors"); diff --git a/DbServices/DbTacticService.cs b/DbServices/DbTacticService.cs index 2e74adc..72910e9 100644 --- a/DbServices/DbTacticService.cs +++ b/DbServices/DbTacticService.cs @@ -136,28 +136,23 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService public async Task RemoveTacticStep(int tacticId, int stepId) { - var toRemove = new Stack(); - toRemove.Push(stepId); + var toRemove = new Stack(); - while (toRemove.TryPop(out var id)) + var beginStep = await context + .TacticSteps + .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId); + + if (beginStep == null) + return false; + + toRemove.Push(beginStep); + + while (toRemove.TryPop(out var step)) { - var step = await context.TacticSteps - .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == id); - - if (step == null) - { - if (id == stepId) - 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); - foreach (var stepChild in stepChildren) - { - toRemove.Push(stepChild.StepId); - } + + await context.TacticSteps + .Where(s => s.TacticId == tacticId && s.ParentId == step.StepId) + .ForEachAsync(toRemove.Push); context.Remove(step); }