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
@@ -60,6 +60,7 @@ public static class DependencyInjection
|
||||
services.AddScoped<IAgentService, AgentService>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddScoped<ICameraService, CameraService>();
|
||||
services.AddScoped<IStatsService, StatsService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -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<StatsResponse> 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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user