feature: Schema, Migrations, Core CRUD, and Redis Threshold Cache

This commit is contained in:
voltsrage
2026-06-16 17:59:16 +08:00
commit 882d4af3e6
63 changed files with 3923 additions and 0 deletions
@@ -0,0 +1,70 @@
using Microsoft.AspNetCore.Mvc;
/// <summary>
/// CRUD for clinical alert thresholds with Redis cache invalidation on writes.
/// </summary>
[ApiController]
[Route("api/v1/alert-thresholds")]
[Produces("application/json")]
public class AlertThresholdsController : ControllerBase
{
private readonly IAlertThresholdService _thresholds;
public AlertThresholdsController(IAlertThresholdService thresholds) => _thresholds = thresholds;
/// <summary>
/// Creates a new alert threshold for an observation code.
/// </summary>
/// <param name="req">Threshold bounds and display metadata.</param>
/// <returns>The created threshold.</returns>
[HttpPost]
[ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status409Conflict)]
public async Task<IActionResult> Create([FromBody] AlertThresholdRequest req)
{
var threshold = await _thresholds.CreateAsync(req);
return StatusCode(201, ApiResponse<AlertThreshold>.Created(threshold));
}
/// <summary>
/// Lists all alert thresholds ordered by observation code.
/// </summary>
/// <returns>All configured thresholds.</returns>
[HttpGet]
[ProducesResponseType(typeof(ApiResponse<List<AlertThreshold>>), StatusCodes.Status200OK)]
public async Task<IActionResult> List()
{
var thresholds = await _thresholds.ListAsync();
return Ok(ApiResponse<List<AlertThreshold>>.Ok(thresholds));
}
/// <summary>
/// Gets a single alert threshold by id.
/// </summary>
/// <param name="id">Threshold id.</param>
/// <returns>The threshold record.</returns>
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(Guid id)
{
var threshold = await _thresholds.GetByIdAsync(id);
return Ok(ApiResponse<AlertThreshold>.Ok(threshold));
}
/// <summary>
/// Updates an existing alert threshold and invalidates the Redis cache entry.
/// </summary>
/// <param name="id">Threshold id.</param>
/// <param name="req">Updated threshold bounds and display metadata.</param>
/// <returns>The updated threshold.</returns>
[HttpPut("{id:guid}")]
[ProducesResponseType(typeof(ApiResponse<AlertThreshold>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Update(Guid id, [FromBody] AlertThresholdRequest req)
{
var threshold = await _thresholds.UpdateAsync(id, req);
return Ok(ApiResponse<AlertThreshold>.Ok(threshold));
}
}
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Mvc;
/// <summary>
/// Encounter retrieval, status transitions, and clinical timeline.
/// </summary>
[ApiController]
[Route("api/v1/encounters")]
[Produces("application/json")]
public class EncountersController : ControllerBase
{
private readonly IEncounterService _encounters;
public EncountersController(IEncounterService encounters) => _encounters = encounters;
/// <summary>
/// Gets an encounter with patient, recent observations, and open alerts.
/// </summary>
/// <param name="id">Encounter id.</param>
/// <returns>The encounter with related data.</returns>
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(ApiResponse<Encounter>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(Guid id)
{
var encounter = await _encounters.GetByIdAsync(id);
return Ok(ApiResponse<Encounter>.Ok(encounter));
}
/// <summary>
/// Transitions an encounter to a new status via the encounter state machine.
/// </summary>
/// <param name="id">Encounter id.</param>
/// <param name="req">Target status.</param>
/// <returns>The encounter id and new status.</returns>
[HttpPatch("{id:guid}/status")]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status409Conflict)]
public async Task<IActionResult> TransitionStatus(Guid id, [FromBody] TransitionStatusRequest req)
{
var result = await _encounters.TransitionStatusAsync(id, req.Status);
return Ok(ApiResponse<object>.Ok(new { encounterId = result.EncounterId, newStatus = result.NewStatus }));
}
/// <summary>
/// Returns a merged chronological timeline of observations and alerts for an encounter.
/// </summary>
/// <param name="id">Encounter id.</param>
/// <returns>Ordered timeline events.</returns>
[HttpGet("{id:guid}/timeline")]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Timeline(Guid id)
{
var timeline = await _encounters.GetTimelineAsync(id);
return Ok(ApiResponse<object>.Ok(timeline));
}
}
@@ -0,0 +1,80 @@
using Microsoft.AspNetCore.Mvc;
/// <summary>
/// Patient registration, search, and encounter opening.
/// </summary>
[ApiController]
[Route("api/v1/patients")]
[Produces("application/json")]
public class PatientsController : ControllerBase
{
private readonly IPatientService _patients;
public PatientsController(IPatientService patients) => _patients = patients;
/// <summary>
/// Registers a new patient and assigns a system-generated MRN.
/// </summary>
/// <param name="req">Patient demographics.</param>
/// <returns>The created patient record.</returns>
[HttpPost]
[ProducesResponseType(typeof(ApiResponse<Patient>), StatusCodes.Status201Created)]
public async Task<IActionResult> Register([FromBody] RegisterPatientRequest req)
{
var patient = await _patients.RegisterAsync(req);
return StatusCode(201, ApiResponse<Patient>.Created(patient));
}
/// <summary>
/// Lists patients with optional MRN or name search and pagination.
/// </summary>
/// <param name="q">MRN (exact) or name substring (ILIKE).</param>
/// <param name="page">Page number (1-based).</param>
/// <param name="pageSize">Results per page.</param>
/// <returns>A paginated list of patients.</returns>
[HttpGet]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
public async Task<IActionResult> List([FromQuery] string? q, [FromQuery] int page = 1, [FromQuery] int pageSize = 20)
{
var result = await _patients.ListAsync(q, page, pageSize);
return Ok(ApiResponse<object>.Ok(new
{
items = result.Items,
page = result.Page,
pageSize = result.PageSize,
totalCount = result.TotalCount,
totalPages = result.TotalPages
}));
}
/// <summary>
/// Gets a patient by id, including active encounters.
/// </summary>
/// <param name="id">Patient id.</param>
/// <returns>The patient record.</returns>
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(ApiResponse<Patient>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(Guid id)
{
var patient = await _patients.GetByIdAsync(id);
return Ok(ApiResponse<Patient>.Ok(patient));
}
/// <summary>
/// Opens a new active encounter for the patient.
/// </summary>
/// <param name="id">Patient id.</param>
/// <param name="req">Encounter type, department, and attending physician.</param>
/// <returns>The created encounter.</returns>
[HttpPost("{id:guid}/encounters")]
[ProducesResponseType(typeof(ApiResponse<Encounter>), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status409Conflict)]
public async Task<IActionResult> OpenEncounter(Guid id, [FromBody] OpenEncounterRequest req)
{
var encounter = await _patients.OpenEncounterAsync(id, req);
return StatusCode(201, ApiResponse<Encounter>.Created(encounter));
}
}