76 lines
3.1 KiB
C#
76 lines
3.1 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Groups.Commands.AddCameraToGroup;
|
||
|
|
using Heimdall.Application.Groups.Commands.RemoveCameraFromGroup;
|
||
|
|
using Heimdall.Application.Groups.Queries.GetCamerasByGroup;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using MediatR;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Group-Camera access management (Admin only).
|
||
|
|
/// Allows assigning/revoking camera access to groups.
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/groups/{groupId:guid}/cameras")]
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.AdminUsers)]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class GroupCamerasController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IMediator _mediator;
|
||
|
|
|
||
|
|
public GroupCamerasController(IMediator mediator) => _mediator = mediator;
|
||
|
|
|
||
|
|
/// <summary>List all cameras a group has access to.</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(PagedResult<CameraResponse>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> List(
|
||
|
|
Guid groupId,
|
||
|
|
[FromQuery] int page = 1,
|
||
|
|
[FromQuery] int pageSize = 20,
|
||
|
|
CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetCamerasByGroupQuery(groupId, page, pageSize), ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Grant camera access to a group.</summary>
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(GroupCameraAccessResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status409Conflict)]
|
||
|
|
public async Task<IActionResult> Create(
|
||
|
|
Guid groupId,
|
||
|
|
[FromBody] CreateGroupCameraAccessRequest request,
|
||
|
|
CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
if (request.GroupId != groupId)
|
||
|
|
return BadRequest(new { error = "Group ID in body does not match route" });
|
||
|
|
|
||
|
|
var command = new AddCameraToGroupCommand(request.GroupId, request.CameraId);
|
||
|
|
var result = await _mediator.Send(command, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Revoke camera access from a group.</summary>
|
||
|
|
[HttpDelete("{cameraId:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Delete(Guid groupId, Guid cameraId, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var command = new RemoveCameraFromGroupCommand(groupId, cameraId);
|
||
|
|
var result = await _mediator.Send(command, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
}
|