using System; using System.Threading; using System.Threading.Tasks; using FastEndpoints; using Dapper; using Npgsql; using Microsoft.Extensions.Configuration; namespace QuantEngine.Web.Endpoints; public record UpdateThresholdRequest(string FactorId, string CalibrationState, string ThresholdParamsJson); public record UpdateThresholdResponse(bool Success, string Message); /// /// BFF FastEndpoints for updating factor threshold parameter logic. /// SOLID: Single Responsibility for updating factor settings. /// public class UpdateFactorThresholdEndpoint : Endpoint { private readonly string _connectionString; public UpdateFactorThresholdEndpoint(IConfiguration config) { _connectionString = config.GetConnectionString("DefaultConnection") ?? throw new ArgumentNullException("ConnectionStrings__DefaultConnection"); } public override void Configure() { Post("/api/admin/factors/update-threshold"); AllowAnonymous(); } public override async Task HandleAsync(UpdateThresholdRequest req, CancellationToken ct) { using var conn = new NpgsqlConnection(_connectionString); await conn.OpenAsync(ct); const string sql = @" UPDATE quantengine.factor_version_history SET calibration_state = @CalibrationState, threshold_params = @ThresholdParams::jsonb, updated_at = NOW() WHERE factor_id = @FactorId;"; int affectedRows = await conn.ExecuteAsync(sql, new { req.FactorId, req.CalibrationState, ThresholdParams = req.ThresholdParamsJson }); if (affectedRows > 0) { await SendAsync(new UpdateThresholdResponse(true, "성공적으로 반영되었습니다."), cancellation: ct); } else { await SendAsync(new UpdateThresholdResponse(false, "해당 Factor ID를 찾을 수 없습니다."), statusCode: 404, cancellation: ct); } } }