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
This commit is contained in:
1 parent
c6450ad50d
commit
e17dce1b4f
5 files changed
+55
-15
No files matched your search
@@ -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<IActionResult> 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
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user