Files
AdPlatform-Server/IntelligenceApi/Controllers/DemographicsController.cs
2026-03-14 13:50:09 -07:00

49 lines
1.5 KiB
C#

using IntelligenceApi.Engines;
using IntelligenceApi.Models;
using Microsoft.AspNetCore.Mvc;
namespace IntelligenceApi.Controllers;
/// <summary>
/// Demographics analysis endpoint.
///
/// Called exclusively by the Gateway's IntelligenceApiClient after it fetches
/// raw census data from the database. This container derives audience
/// recommendations from the raw data — age chips, income tiers, insights.
///
/// POST /api/demographics/analyze
/// </summary>
[ApiController]
[Route("api/demographics")]
public sealed class DemographicsController : ControllerBase
{
private readonly DemographicsAnalyzer _analyzer;
private readonly ILogger<DemographicsController> _log;
public DemographicsController(DemographicsAnalyzer analyzer, ILogger<DemographicsController> log)
{
_analyzer = analyzer;
_log = log;
}
[HttpPost("analyze")]
public IActionResult Analyze([FromBody] DemographicAnalysisRequest? request)
{
if (request == null || string.IsNullOrWhiteSpace(request.Zcta))
return BadRequest(new { ok = false, error = "zcta and census data are required" });
_log.LogInformation("[Demographics] Analyze | ZCTA={Zcta}", request.Zcta);
try
{
var result = _analyzer.Analyze(request);
return Ok(result);
}
catch (Exception ex)
{
_log.LogError(ex, "[Demographics] Analysis error | ZCTA={Zcta}", request.Zcta);
return StatusCode(500, new { ok = false, error = "Demographics analysis error" });
}
}
}