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

82 lines
3.6 KiB
C#
Raw Normal View History

using Heimdall.Application.DTOs;
using Heimdall.Application.Services;
using Heimdall.Infrastructure.Security;
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 IPersonService _personService;
public PersonsController(IPersonService personService) => _personService = personService;
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreatePersonRequest request, CancellationToken ct)
{
var result = await _personService.CreateAsync(request, 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 result = await _personService.UpdateAsync(id, request, 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 _personService.GetByIdAsync(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 _personService.GetAllAsync(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 _personService.DeleteAsync(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 result = await _personService.RegisterFaceAsync(request with { PersonId = personId }, 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 _personService.GetFacesAsync(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 _personService.DeleteFaceAsync(faceId, ct);
return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
}
}