From e17dce1b4f9a0cdb09e1dcd86df9e03e549fb088 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Fri, 17 Jul 2026 17:36:14 +0000 Subject: [PATCH] fix(api): StatsController uses IStatsService instead of direct AppDbContext - Add IStatsService in Application layer - Add StatsService in Infrastructure layer - StatsController now injects IStatsService (Clean Architecture compliant) - StatsResponse DTO added to Application/DTOs --- .../Controllers/StatsController.cs | 24 ++++++--------- src/Heimdall.Application/DTOs/StatsDto.cs | 8 +++++ .../Services/IStatsService.cs | 8 +++++ .../DependencyInjection.cs | 1 + .../Services/StatsService.cs | 29 +++++++++++++++++++ 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/Heimdall.Application/DTOs/StatsDto.cs create mode 100644 src/Heimdall.Application/Services/IStatsService.cs create mode 100644 src/Heimdall.Infrastructure/Services/StatsService.cs diff --git a/src/Heimdall.Api/Controllers/StatsController.cs b/src/Heimdall.Api/Controllers/StatsController.cs index a4c024d..9ca8a5d 100644 --- a/src/Heimdall.Api/Controllers/StatsController.cs +++ b/src/Heimdall.Api/Controllers/StatsController.cs @@ -1,9 +1,7 @@ -using Heimdall.Domain.Enums; -using Heimdall.Infrastructure.Data; +using Heimdall.Application.Services; using Heimdall.Infrastructure.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; namespace Heimdall.Api.Controllers; @@ -11,26 +9,22 @@ namespace Heimdall.Api.Controllers; [Route("api/stats")] public class StatsController : ControllerBase { - private readonly AppDbContext _db; + private readonly IStatsService _statsService; - public StatsController(AppDbContext db) => _db = db; + public StatsController(IStatsService statsService) => _statsService = statsService; [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)] [HttpGet] public async Task GetStats(CancellationToken ct) { - var siteCount = await _db.Sites.CountAsync(ct); - var cameraCount = await _db.Cameras.CountAsync(ct); - var agentCount = await _db.Agents.CountAsync(ct); - var onlineAgents = await _db.Agents.CountAsync(a => a.Status == AgentStatus.Online, ct); - + var stats = await _statsService.GetStatsAsync(ct); return Ok(new { - totalSites = siteCount, - totalCameras = cameraCount, - totalAgents = agentCount, - onlineAgents, - activeSites = siteCount, // All non-soft-deleted sites are active + totalSites = stats.TotalSites, + totalCameras = stats.TotalCameras, + totalAgents = stats.TotalAgents, + onlineAgents = stats.OnlineAgents, + activeSites = stats.ActiveSites, timestamp = DateTime.UtcNow }); } diff --git a/src/Heimdall.Application/DTOs/StatsDto.cs b/src/Heimdall.Application/DTOs/StatsDto.cs new file mode 100644 index 0000000..af125c1 --- /dev/null +++ b/src/Heimdall.Application/DTOs/StatsDto.cs @@ -0,0 +1,8 @@ +namespace Heimdall.Application.DTOs; + +public record StatsResponse( + int TotalSites, + int TotalCameras, + int TotalAgents, + int OnlineAgents, + int ActiveSites); diff --git a/src/Heimdall.Application/Services/IStatsService.cs b/src/Heimdall.Application/Services/IStatsService.cs new file mode 100644 index 0000000..da6b2ef --- /dev/null +++ b/src/Heimdall.Application/Services/IStatsService.cs @@ -0,0 +1,8 @@ +using Heimdall.Application.DTOs; + +namespace Heimdall.Application.Services; + +public interface IStatsService +{ + Task GetStatsAsync(CancellationToken ct); +} diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 5dd0dfa..c6bc24d 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -60,6 +60,7 @@ public static class DependencyInjection services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } diff --git a/src/Heimdall.Infrastructure/Services/StatsService.cs b/src/Heimdall.Infrastructure/Services/StatsService.cs new file mode 100644 index 0000000..2b41a94 --- /dev/null +++ b/src/Heimdall.Infrastructure/Services/StatsService.cs @@ -0,0 +1,29 @@ +using Heimdall.Application.DTOs; +using Heimdall.Application.Services; +using Heimdall.Domain.Enums; +using Heimdall.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + +namespace Heimdall.Infrastructure.Services; + +public class StatsService : IStatsService +{ + private readonly AppDbContext _db; + + public StatsService(AppDbContext db) => _db = db; + + public async Task GetStatsAsync(CancellationToken ct) + { + var totalSites = await _db.Sites.CountAsync(ct); + var totalCameras = await _db.Cameras.CountAsync(ct); + var totalAgents = await _db.Agents.CountAsync(ct); + var onlineAgents = await _db.Agents.CountAsync(a => a.Status == AgentStatus.Online, ct); + + return new StatsResponse( + totalSites, + totalCameras, + totalAgents, + onlineAgents, + totalSites); + } +}