2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Application.DTOs;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Persons.Commands.CreatePerson;
|
|
|
|
|
using Heimdall.Application.Persons.Commands.UpdatePerson;
|
|
|
|
|
using Heimdall.Application.Persons.Commands.DeletePerson;
|
|
|
|
|
using Heimdall.Application.Persons.Commands.RegisterFace;
|
|
|
|
|
using Heimdall.Application.Persons.Commands.DeleteFace;
|
|
|
|
|
using Heimdall.Application.Persons.Queries.GetPerson;
|
|
|
|
|
using Heimdall.Application.Persons.Queries.GetPersons;
|
|
|
|
|
using Heimdall.Application.Persons.Queries.GetFaces;
|
2026-07-20 10:30:01 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/persons")]
|
|
|
|
|
[EnableRateLimiting("Api")]
|
|
|
|
|
public class PersonsController : 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 PersonsController(IMediator mediator) => _mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Create([FromBody] CreatePersonRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new CreatePersonCommand(request.DisplayName, request.Category, request.Notes, request.IsWatchlisted);
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpPut("{id:guid}")]
|
|
|
|
|
public async Task<IActionResult> Update(Guid id, [FromBody] UpdatePersonRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new UpdatePersonCommand(id, request.DisplayName, request.Notes, request.Category, request.IsWatchlisted);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("{id:guid}")]
|
|
|
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetPersonQuery(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetAll([FromQuery] string? category, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetPersonsQuery(category), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpDelete("{id:guid}")]
|
|
|
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new DeletePersonCommand(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpPost("{personId:guid}/faces")]
|
|
|
|
|
public async Task<IActionResult> RegisterFace(Guid personId, [FromBody] RegisterFaceRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new RegisterFaceCommand(personId, request.Embedding, request.ThumbnailUrl, request.SourceEventId);
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("{personId:guid}/faces")]
|
|
|
|
|
public async Task<IActionResult> GetFaces(Guid personId, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetFacesQuery(personId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.PersonWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpDelete("faces/{faceId:guid}")]
|
|
|
|
|
public async Task<IActionResult> DeleteFace(Guid faceId, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new DeleteFaceCommand(faceId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
}
|