85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
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 SessionEndpoint
|
|
{
|
|
public static void MapSessionEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
app.MapGet("/api/sessions", HandleList);
|
|
app.MapGet("/api/sessions/{sessionId}", HandleGet);
|
|
app.MapPost("/api/sessions", HandleCreate);
|
|
app.MapPut("/api/sessions/{sessionId}", HandleUpdate);
|
|
app.MapDelete("/api/sessions/{sessionId}", HandleDelete);
|
|
}
|
|
|
|
private static async Task HandleList(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessions = await db.GetSessions();
|
|
await ctx.Response.WriteAsJsonAsync(sessions);
|
|
}
|
|
|
|
private static async Task HandleGet(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
var session = await db.GetSession(sessionId);
|
|
if (session == null)
|
|
{
|
|
ctx.Response.StatusCode = 404;
|
|
return;
|
|
}
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleCreate(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
|
|
var name = body.TryGetProperty("name", out var n) ? n.GetString() : "Neue Session";
|
|
var description = body.TryGetProperty("description", out var d) ? d.GetString() : null;
|
|
var image = body.TryGetProperty("image", out var img) ? img.GetString() : null;
|
|
var agentId = body.TryGetProperty("agentId", out var a) ? a.GetString() : null;
|
|
var workspace = body.TryGetProperty("workspace", out var w) ? w.GetString() : null;
|
|
var context = body.TryGetProperty("context", out var ctxEl) ? ctxEl.GetString() : null;
|
|
|
|
var session = await db.CreateSession(name, description, image, agentId, workspace, context);
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleUpdate(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
await db.UpdateSession(sessionId, body);
|
|
|
|
var session = await db.GetSession(sessionId);
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleDelete(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId)) { ctx.Response.StatusCode = 400; return; }
|
|
|
|
await db.DeleteSession(sessionId);
|
|
ctx.Response.StatusCode = 204;
|
|
}
|
|
}
|