feat(cqrs): update controllers, add AutoMapper profiles, SignalR notifications, Hangfire config

- 13 controllers migrated from service DI to IMediator dispatch
- 11 AutoMapper mapping profiles (Sites, Cameras, Auth, Users, Agents, Events, Assignments, Settings, Groups, Persons, Discovery)
- SignalR notifications bridged via MediatR INotificationHandler
- Hangfire config moved from Program.cs to Infrastructure/BackgroundJobs
- 0 build errors, 0 warnings
This commit is contained in:
Arnaud Dartois committed 2026-07-22 19:54:55 +00:00
1 parent c1b3b3d3b4
commit 1d1ac8387e
33 files changed
+611 -195

No files matched your search

+27 -14
View File
@@ -1,8 +1,17 @@
using System.Security.Claims;
using Heimdall.Application.Auth.Commands.Register;
using Heimdall.Application.Auth.Commands.Login;
using Heimdall.Application.Auth.Commands.Verify2fa;
using Heimdall.Application.Auth.Commands.RefreshToken;
using Heimdall.Application.Auth.Commands.ChangePassword;
using Heimdall.Application.Auth.Commands.RegisterAdmin;
using Heimdall.Application.Auth.Commands.Enable2fa;
using Heimdall.Application.Auth.Commands.Disable2fa;
using Heimdall.Application.Auth.Commands.Logout;
using Heimdall.Application.Common;
using Heimdall.Application.DTOs;
using Heimdall.Application.Services;
using Heimdall.Infrastructure.Security;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
@@ -14,12 +23,12 @@ namespace Heimdall.Api.Controllers;
[EnableRateLimiting("Auth")]
public class AuthController : ControllerBase
{
private readonly IAuthService _authService;
private readonly IMediator _mediator;
private readonly IWebHostEnvironment _env;
public AuthController(IAuthService authService, IWebHostEnvironment env)
public AuthController(IMediator mediator, IWebHostEnvironment env)
{
_authService = authService;
_mediator = mediator;
_env = env;
}
@@ -30,7 +39,8 @@ public class AuthController : ControllerBase
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Register([FromBody] RegisterRequest request, CancellationToken ct)
{
var result = await _authService.RegisterAsync(request, ct);
var command = new RegisterCommand(request.Email, request.Password, request.DisplayName);
var result = await _mediator.Send(command, ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
@@ -43,7 +53,8 @@ public class AuthController : ControllerBase
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Login([FromBody] LoginRequest request, CancellationToken ct)
{
var result = await _authService.LoginAsync(request, ct);
var command = new LoginCommand(request.Email, request.Password);
var result = await _mediator.Send(command, ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
@@ -56,7 +67,8 @@ public class AuthController : ControllerBase
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Verify2fa([FromBody] Verify2faRequest request, CancellationToken ct)
{
var result = await _authService.Verify2faAsync(request, ct);
var command = new Verify2faCommand(request.Email, request.Code);
var result = await _mediator.Send(command, ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
@@ -69,7 +81,7 @@ public class AuthController : ControllerBase
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Refresh([FromBody] RefreshTokenRequest request, CancellationToken ct)
{
var result = await _authService.RefreshTokenAsync(request.RefreshToken, ct);
var result = await _mediator.Send(new RefreshTokenCommand(request.RefreshToken), ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
@@ -88,8 +100,8 @@ public class AuthController : ControllerBase
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
return Unauthorized(new { error = "Invalid user identity" });
var result = await _authService.ChangePasswordAsync(
userId, request.CurrentPassword, request.NewPassword, ct);
var command = new ChangePasswordCommand(userId, request.CurrentPassword, request.NewPassword);
var result = await _mediator.Send(command, ct);
return result.IsSuccess
? Ok()
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
@@ -106,7 +118,8 @@ public class AuthController : ControllerBase
if (!_env.IsEnvironment("Testing") && !_env.IsDevelopment())
return NotFound();
var result = await _authService.RegisterAdminAsync(request, ct);
var command = new RegisterAdminCommand(request.Email, request.Password, request.DisplayName);
var result = await _mediator.Send(command, ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
@@ -123,7 +136,7 @@ public class AuthController : ControllerBase
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
return Unauthorized(new { error = "Invalid user identity" });
var result = await _authService.Enable2faAsync(userId, ct);
var result = await _mediator.Send(new Enable2faCommand(userId), ct);
return result.IsSuccess
? Ok()
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
@@ -140,7 +153,7 @@ public class AuthController : ControllerBase
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
return Unauthorized(new { error = "Invalid user identity" });
var result = await _authService.Disable2faAsync(userId, ct);
var result = await _mediator.Send(new Disable2faCommand(userId), ct);
return result.IsSuccess
? Ok()
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
@@ -156,7 +169,7 @@ public class AuthController : ControllerBase
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
return Unauthorized(new { error = "Invalid user identity" });
await _authService.LogoutAsync(userId, request.RefreshToken, ct);
await _mediator.Send(new LogoutCommand(userId, request.RefreshToken), ct);
return Ok();
}
}