Files
Heimdall/src/Heimdall.Web/Controllers/PersonsController.cs
T

93 lines
4.3 KiB
C#
Raw Normal View History

using Heimdall.Application.DTOs;
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;
using Heimdall.Infrastructure.Security;
using MediatR;
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
{
private readonly IMediator _mediator;
public PersonsController(IMediator mediator) => _mediator = mediator;
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreatePersonRequest request, CancellationToken ct)
{
var command = new CreatePersonCommand(request.DisplayName, request.Category, request.Notes, request.IsWatchlisted);
var result = await _mediator.Send(command, ct);
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpPut("{id:guid}")]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdatePersonRequest request, CancellationToken ct)
{
var command = new UpdatePersonCommand(id, request.DisplayName, request.Notes, request.Category, request.IsWatchlisted);
var result = await _mediator.Send(command, ct);
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)]
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
{
var result = await _mediator.Send(new GetPersonQuery(id), ct);
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)]
[HttpGet]
public async Task<IActionResult> GetAll([FromQuery] string? category, CancellationToken ct)
{
var result = await _mediator.Send(new GetPersonsQuery(category), ct);
return Ok(result.Data);
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
{
var result = await _mediator.Send(new DeletePersonCommand(id), ct);
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpPost("{personId:guid}/faces")]
public async Task<IActionResult> RegisterFace(Guid personId, [FromBody] RegisterFaceRequest request, CancellationToken ct)
{
var command = new RegisterFaceCommand(personId, request.Embedding, request.ThumbnailUrl, request.SourceEventId);
var result = await _mediator.Send(command, ct);
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)]
[HttpGet("{personId:guid}/faces")]
public async Task<IActionResult> GetFaces(Guid personId, CancellationToken ct)
{
var result = await _mediator.Send(new GetFacesQuery(personId), ct);
return Ok(result.Data);
}
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpDelete("faces/{faceId:guid}")]
public async Task<IActionResult> DeleteFace(Guid faceId, CancellationToken ct)
{
var result = await _mediator.Send(new DeleteFaceCommand(faceId), ct);
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
}
}