55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using Fitness_core_BE.Interfaces;
|
|
using Fitness_core_BE.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Fitness_core_BE.Repos
|
|
{
|
|
public class AgentRepo : IAgentRepo
|
|
{
|
|
// Central: an agent authenticates via X-Api-Key alone, before any tenant is known.
|
|
// See CentralDbContext.Agents for the full rationale.
|
|
private readonly CentralDbContext _dbContext;
|
|
|
|
public AgentRepo(CentralDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<Agent> AddAsync(Agent agent)
|
|
{
|
|
await _dbContext.Agents.AddAsync(agent);
|
|
return agent;
|
|
}
|
|
|
|
public async Task<Agent?> GetByApiKeyHashAsync(string apiKeyHash)
|
|
{
|
|
return await _dbContext.Agents
|
|
.FirstOrDefaultAsync(a => a.ApiKeyHash == apiKeyHash && a.IsActive == true);
|
|
}
|
|
|
|
public async Task<Agent?> GetByIdAsync(Guid agentId)
|
|
{
|
|
return await _dbContext.Agents
|
|
.FirstOrDefaultAsync(a => a.AgentId == agentId);
|
|
}
|
|
|
|
public async Task<List<Agent>> GetByClientIdAsync(Guid clientId)
|
|
{
|
|
return await _dbContext.Agents
|
|
.AsNoTracking()
|
|
.Where(a => a.FKClientId == clientId)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task UpdateLastSeenAsync(Guid agentId, DateTime seenAt)
|
|
{
|
|
var agent = await _dbContext.Agents.FirstOrDefaultAsync(a => a.AgentId == agentId);
|
|
if (agent == null)
|
|
return;
|
|
|
|
agent.LastSeenAt = seenAt;
|
|
}
|
|
}
|
|
}
|