53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace StellaOps.Provcache.Api;
|
|
|
|
public static partial class ProvcacheEndpointExtensions
|
|
{
|
|
/// <summary>
|
|
/// POST /v1/provcache
|
|
/// </summary>
|
|
private static async Task<IResult> CreateOrUpdateAsync(
|
|
ProvcacheCreateRequest request,
|
|
IProvcacheService provcacheService,
|
|
ILogger<ProvcacheApiEndpoints> logger,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
logger.LogDebug("POST /v1/provcache for VeriKey {VeriKey}", request.Entry?.VeriKey);
|
|
|
|
if (request.Entry is null)
|
|
{
|
|
return Results.Problem(
|
|
detail: "Request body must contain a valid entry",
|
|
statusCode: StatusCodes.Status400BadRequest,
|
|
title: "Invalid request");
|
|
}
|
|
|
|
try
|
|
{
|
|
var success = await provcacheService.SetAsync(request.Entry, cancellationToken);
|
|
|
|
if (!success)
|
|
{
|
|
return Results.Problem(
|
|
detail: "Failed to store cache entry",
|
|
statusCode: StatusCodes.Status500InternalServerError,
|
|
title: "Cache write failed");
|
|
}
|
|
|
|
return Results.Created($"/v1/provcache/{request.Entry.VeriKey}", new ProvcacheCreateResponse
|
|
{
|
|
VeriKey = request.Entry.VeriKey,
|
|
Success = true,
|
|
ExpiresAt = request.Entry.ExpiresAt
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Error storing cache entry for VeriKey {VeriKey}", request.Entry?.VeriKey);
|
|
return InternalError("Cache write failed");
|
|
}
|
|
}
|
|
}
|