2026-08-30 10:09:25 +02:00
|
|
|
using System.Security.Claims;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Cameras.Commands.CreateCamera;
|
|
|
|
|
using Heimdall.Application.Cameras.Commands.UpdateCamera;
|
|
|
|
|
using Heimdall.Application.Cameras.Commands.DeleteCamera;
|
2026-08-19 22:38:34 +02:00
|
|
|
using Heimdall.Application.Cameras.Commands.ToggleCameraStatus;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Cameras.Queries.GetCamera;
|
|
|
|
|
using Heimdall.Application.Cameras.Queries.GetCamerasBySite;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
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/cameras")]
|
|
|
|
|
[EnableRateLimiting("Api")]
|
|
|
|
|
public class CamerasController : 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 CamerasController(IMediator mediator) => _mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
/// <summary>Create a new camera.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(CameraResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Create([FromBody] CreateCameraRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-10-21 15:11:48 +02:00
|
|
|
if (!this.TryGetUserId(out var actorId)) return Unauthorized();
|
|
|
|
|
var command = new CreateCameraCommand(request.SiteId, request.Name, request.RtspUrl,
|
|
|
|
|
request.Manufacturer, request.Model, actorId);
|
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 camera 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(CameraResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-08-30 10:09:25 +02:00
|
|
|
var userId = GetUserId();
|
|
|
|
|
var result = await _mediator.Send(new GetCameraQuery(id, userId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: NotFound(new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>List cameras by site.</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("by-site/{siteId:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(PagedResult<CameraResponse>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> ListBySite(
|
|
|
|
|
Guid siteId, [FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
|
|
|
|
|
{
|
2026-08-30 10:09:25 +02:00
|
|
|
var userId = GetUserId();
|
|
|
|
|
var result = await _mediator.Send(new GetCamerasBySiteQuery(siteId, page, pageSize, userId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Update a camera.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraWrite)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpPut("{id:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(CameraResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateCameraRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-10-21 15:11:48 +02:00
|
|
|
if (!this.TryGetUserId(out var actorId)) return Unauthorized();
|
|
|
|
|
var command = new UpdateCameraCommand(id, request.Name, request.RtspUrl, request.Status,
|
|
|
|
|
request.Manufacturer, request.Model, request.DetectionProfile, actorId);
|
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 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-19 22:38:34 +02:00
|
|
|
/// <summary>Toggle a camera between Online and Disabled.</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraWrite)]
|
|
|
|
|
[HttpPost("{id:guid}/toggle-status")]
|
|
|
|
|
[ProducesResponseType(typeof(CameraResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> ToggleStatus(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-10-21 10:15:26 +02:00
|
|
|
if (!this.TryGetUserId(out var actorId))
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
|
|
|
|
|
var result = await _mediator.Send(new ToggleCameraStatusCommand(id, actorId), ct);
|
2026-08-19 22:38:34 +02:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: NotFound(new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 10:30:01 +00:00
|
|
|
/// <summary>Delete a camera.</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.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-10-21 15:11:48 +02:00
|
|
|
if (!this.TryGetUserId(out var actorId)) return Unauthorized();
|
|
|
|
|
var result = await _mediator.Send(new DeleteCameraCommand(id, actorId), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? NoContent()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
2026-08-30 10:09:25 +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
|
|
|
}
|