Files
gott/Endpoints/HistoryEndpoint.cs
T

41 lines
1.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Gott.Models;
using Gott.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Gott.Endpoints;
public static class HistoryEndpoint
{
public static void MapHistoryEndpoint(this IEndpointRouteBuilder app)
{
app.MapGet("/api/history", HandleHistory);
}
private static async Task HandleHistory(HttpContext ctx, DatabaseService db)
{
var sessionId = ctx.Request.Query["sessionId"].FirstOrDefault();
var limitStr = ctx.Request.Query["limit"].FirstOrDefault();
var limit = int.TryParse(limitStr, out var l) ? l : 50;
IEnumerable<ChainRecord> chains;
if (string.IsNullOrEmpty(sessionId) || sessionId == "null")
chains = await db.GetRecentChains(limit);
else
chains = await db.GetSessionChains(sessionId, limit);
var result = new List<object>();
foreach (var chain in chains)
{
var messages = await db.GetChainMessages(chain.ChainId);
result.Add(new { chain, messages });
}
await ctx.Response.WriteAsJsonAsync(result);
}
}