2026-08-27 20:28:41 +02:00
|
|
|
using System.Security.Claims;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Application.DTOs;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Events.Commands.CreateEvent;
|
|
|
|
|
using Heimdall.Application.Events.Commands.AttachSnapshot;
|
|
|
|
|
using Heimdall.Application.Events.Commands.DeleteEvent;
|
|
|
|
|
using Heimdall.Application.Events.Queries.GetEvent;
|
|
|
|
|
using Heimdall.Application.Events.Queries.GetEvents;
|
|
|
|
|
using Heimdall.Application.Events.Queries.GetRecentByCamera;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using Heimdall.Infrastructure.Security;
|
2026-07-22 19:54:55 +00:00
|
|
|
using MediatR;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Api.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Camera detection events — motion, person, vehicle, face recognition, etc.
|
|
|
|
|
/// Agents POST events as they detect them; users GET filtered views.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/events")]
|
|
|
|
|
[EnableRateLimiting("Api")]
|
|
|
|
|
public class EventsController : ControllerBase
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
private readonly IMediator _mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
public EventsController(IMediator mediator) => _mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
/// <summary>Record a new detection event (called by agents).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "ApiKey", Policy = AppPermissions.CameraWrite)]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(CameraEventResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Create([FromBody] CreateCameraEventRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-10-22 20:12:25 +02:00
|
|
|
var command = new CreateEventCommand(request.CameraId, request.Type, request.Confidence, request.Label, request.MetadataJson, request.OccurredAt, request.EventId, request.PersonId);
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Get a single event by ID.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("{id:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(CameraEventResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-08-27 20:28:41 +02:00
|
|
|
var userId = GetUserId();
|
|
|
|
|
var result = await _mediator.Send(new GetEventQuery(id, userId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Query events with filters. Supports pagination.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(PagedResult<CameraEventResponse>), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> Query(
|
|
|
|
|
[FromQuery] Guid? cameraId,
|
|
|
|
|
[FromQuery] Guid? siteId,
|
|
|
|
|
[FromQuery] string? type,
|
2026-08-10 21:48:53 +02:00
|
|
|
[FromQuery] string? q,
|
2026-07-20 10:30:01 +00:00
|
|
|
[FromQuery] DateTime? from,
|
|
|
|
|
[FromQuery] DateTime? to,
|
|
|
|
|
[FromQuery] double? minConfidence,
|
2026-10-20 07:32:56 +02:00
|
|
|
[FromQuery] bool hasSnapshot = false,
|
2026-07-20 10:30:01 +00:00
|
|
|
[FromQuery] int page = 1,
|
|
|
|
|
[FromQuery] int pageSize = 50,
|
2026-10-22 20:12:25 +02:00
|
|
|
[FromQuery] Guid? personId = null,
|
2026-07-20 10:30:01 +00:00
|
|
|
CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
EventType? parsedType = null;
|
|
|
|
|
if (type is not null && Enum.TryParse<EventType>(type, true, out var t))
|
|
|
|
|
parsedType = t;
|
|
|
|
|
|
2026-08-27 20:28:41 +02:00
|
|
|
var userId = GetUserId();
|
2026-10-22 20:12:25 +02:00
|
|
|
var result = await _mediator.Send(new GetEventsQuery(cameraId, siteId, parsedType, from, to, minConfidence, page, pageSize, q, userId, hasSnapshot, personId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Get recent events for a camera.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("camera/{cameraId:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(List<CameraEventResponse>), StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> GetRecentByCamera(Guid cameraId, [FromQuery] int count = 20, CancellationToken ct = default)
|
|
|
|
|
{
|
2026-08-27 20:28:41 +02:00
|
|
|
var userId = GetUserId();
|
|
|
|
|
var result = await _mediator.Send(new GetRecentByCameraQuery(cameraId, count, userId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Attach a snapshot URL to an event (called by agents after upload).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "ApiKey", Policy = AppPermissions.CameraWrite)]
|
|
|
|
|
[HttpPost("{id:guid}/snapshot")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> AttachSnapshot(Guid id, [FromBody] AttachSnapshotRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new AttachSnapshotCommand(id, request.SnapshotUrl, request.ThumbUrl), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Delete an event.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpDelete("{id:guid}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new DeleteEventCommand(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
2026-08-27 20:28:41 +02:00
|
|
|
|
|
|
|
|
private Guid? GetUserId()
|
|
|
|
|
{
|
|
|
|
|
var sub = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value
|
|
|
|
|
?? User?.FindFirst("sub")?.Value;
|
|
|
|
|
return Guid.TryParse(sub, out var userId) ? userId : null;
|
|
|
|
|
}
|
2026-07-20 10:30:01 +00:00
|
|
|
}
|