2026-07-20 10:30:01 +00:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Sites.Commands.CreateSite;
|
|
|
|
|
using Heimdall.Application.Sites.Commands.UpdateSite;
|
|
|
|
|
using Heimdall.Application.Sites.Commands.DeleteSite;
|
|
|
|
|
using Heimdall.Application.Sites.Commands.GenerateSiteToken;
|
|
|
|
|
using Heimdall.Application.Sites.Queries.GetSites;
|
|
|
|
|
using Heimdall.Application.Sites.Queries.GetSite;
|
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/sites")]
|
|
|
|
|
[EnableRateLimiting("Api")]
|
|
|
|
|
public class SitesController : 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 SitesController(IMediator mediator) => _mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
/// <summary>Create a new site (Admin only).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(SiteWithTokenResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Create(
|
|
|
|
|
[FromBody] CreateSiteRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
|
|
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
|
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new CreateSiteCommand(request.Name, request.Address, request.Timezone);
|
|
|
|
|
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>List all sites.</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(PagedResult<SiteResponse>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
public async Task<IActionResult> List(
|
|
|
|
|
[FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetSitesQuery(page, pageSize), 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>Get a site by ID.</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)]
|
|
|
|
|
[HttpGet("{id:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(SiteResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetSiteQuery(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: NotFound(new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Update a site (Admin only).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
|
|
|
|
[HttpPut("{id:guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(SiteResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> Update(
|
|
|
|
|
Guid id, [FromBody] UpdateSiteRequest request, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new UpdateSiteCommand(id, request.Name, request.Address, request.Timezone);
|
|
|
|
|
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>Delete a site (Admin only).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteDelete)]
|
|
|
|
|
[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-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new DeleteSiteCommand(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? NoContent()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Generate a new agent registration token (Admin only).</summary>
|
|
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteWrite)]
|
|
|
|
|
[HttpPost("{id:guid}/tokens")]
|
|
|
|
|
[ProducesResponseType(typeof(RegistrationTokenResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> GenerateToken(Guid id, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GenerateSiteTokenCommand(id), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
}
|