- Session click opens edit form (name, description, image, agent) instead of chat
- Chat is now a separate route (#chat/{id}), opened via button
- Sessions created on save, not on click
- Soft-delete for sessions (is_deleted flag)
- Delete button in session form
- Removed title double-click editing from chat
- Card images now square (aspect-ratio: 1)
- New Session as placeholder card
83 lines
2.8 KiB
C#
83 lines
2.8 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 session = await db.CreateSession(name, description, image, agentId);
|
|
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;
|
|
}
|
|
}
|