using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Gott.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; namespace Gott.Endpoints; public static class SkillEndpoint { public static void MapSkillEndpoints(this IEndpointRouteBuilder app) { // Skills (Dateisystem-basiert) app.MapGet("/api/skills", HandleListSkills); // Agent ↔ Skill Zuordnung app.MapGet("/api/agents/{agentId}/skills", HandleGetAgentSkills); app.MapPut("/api/agents/{agentId}/skills", HandleSetAgentSkills); app.MapPost("/api/agents/{agentId}/skills/{slug}", HandleAddAgentSkill); app.MapDelete("/api/agents/{agentId}/skills/{slug}", HandleRemoveAgentSkill); } private static async Task HandleListSkills(HttpContext ctx, SkillService skillService) { var skills = skillService.GetAllSkills(); await ctx.Response.WriteAsJsonAsync(skills); } private static async Task HandleGetAgentSkills(HttpContext ctx, DatabaseService db, SkillService skillService) { var agentId = ctx.Request.RouteValues["agentId"]?.ToString(); if (string.IsNullOrEmpty(agentId)) { ctx.Response.StatusCode = 400; return; } var slugs = await db.GetAgentSkills(agentId); var allSkills = skillService.GetAllSkills(); // Nur Skills zurückgeben die auch wirklich existieren var result = allSkills .Where(s => slugs.Contains(s.Slug)) .ToList(); await ctx.Response.WriteAsJsonAsync(result); } private static async Task HandleSetAgentSkills(HttpContext ctx, DatabaseService db) { var agentId = ctx.Request.RouteValues["agentId"]?.ToString(); if (string.IsNullOrEmpty(agentId)) { ctx.Response.StatusCode = 400; return; } var body = await JsonSerializer.DeserializeAsync(ctx.Request.Body); var slugs = body.TryGetProperty("skills", out var arr) ? arr.EnumerateArray().Select(e => e.GetString()).Where(s => !string.IsNullOrEmpty(s)).ToList() : new System.Collections.Generic.List(); await db.SetAgentSkills(agentId, slugs); var updated = await db.GetAgentSkills(agentId); await ctx.Response.WriteAsJsonAsync(updated); } private static async Task HandleAddAgentSkill(HttpContext ctx, DatabaseService db) { var agentId = ctx.Request.RouteValues["agentId"]?.ToString(); var slug = ctx.Request.RouteValues["slug"]?.ToString(); if (string.IsNullOrEmpty(agentId) || string.IsNullOrEmpty(slug)) { ctx.Response.StatusCode = 400; return; } await db.AddAgentSkill(agentId, slug); ctx.Response.StatusCode = 204; } private static async Task HandleRemoveAgentSkill(HttpContext ctx, DatabaseService db) { var agentId = ctx.Request.RouteValues["agentId"]?.ToString(); var slug = ctx.Request.RouteValues["slug"]?.ToString(); if (string.IsNullOrEmpty(agentId) || string.IsNullOrEmpty(slug)) { ctx.Response.StatusCode = 400; return; } await db.RemoveAgentSkill(agentId, slug); ctx.Response.StatusCode = 204; } }