From 0b355973476db651e00ac6960e9e0bac2b5ee65f Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 1 Aug 2025 10:08:59 +0200 Subject: [PATCH] =?UTF-8?q?aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Webshop.Api/Program.cs | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 0a9165f..6af172a 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -126,15 +126,46 @@ builder.Services.AddEndpointsApiExplorer(); // Swagger / OpenAPI Konfiguration builder.Services.AddSwaggerGen(c => { + // --- 1. Gruppierung der Endpunkte (mit Unterpunkten) --- c.TagActionsBy(api => { - if (api.RelativePath.StartsWith("api/v1/admin")) return new[] { "Admin" }; - if (api.RelativePath.StartsWith("api/v1/auth")) return new[] { "Auth" }; - if (api.RelativePath.StartsWith("api/v1/customer")) return new[] { "Customer" }; - if (api.RelativePath.StartsWith("api/v1/public")) return new[] { "Public" }; - return new[] { "Default" }; + // Der Name des Controllers wird als Unterpunkt verwendet (z.B. "Products", "Users") + // Wir entfernen das "Controller"-Suffix für einen sauberen Tag. + var controllerName = api.ActionDescriptor.RouteValues["controller"]; + if (controllerName == null) + { + return new[] { "Default" }; + } + var tag = controllerName.Replace("Admin", "").Replace("Controller", ""); // Entfernt Präfixe/Suffixe + + // Erkenne den Hauptbereich anhand des Routen-Präfixes + if (api.RelativePath.StartsWith("api/v1/admin")) + { + // Erzeugt einen Tag wie "Admin - Products", "Admin - Users" etc. + return new[] { $"Admin - {tag}" }; + } + if (api.RelativePath.StartsWith("api/v1/auth")) + { + // Auth hat normalerweise nur einen Controller, daher keine Untergruppe + return new[] { "Auth" }; + + } + if (api.RelativePath.StartsWith("api/v1/customer")) + { + // Erzeugt einen Tag wie "Customer - Profile", "Customer - Orders" + return new[] { $"Customer - {tag}" }; + } + if (api.RelativePath.StartsWith("api/v1/public")) + { + // Erzeugt einen Tag wie "Public - Products", "Public - Categorys" + return new[] { $"Public - {tag}" }; + } + + // Fallback-Tag + return new[] { tag }; }); + // --- 2. JWT Security Konfiguration --- c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header. Example: \"Authorization: Bearer {token}\"", @@ -144,6 +175,21 @@ builder.Services.AddSwaggerGen(c => Scheme = "Bearer" }); + c.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new string[] {} + } + }); + c.OperationFilter(); c.OperationFilter(); c.OperationFilter();