Files
Core_Be/Repos/PairingCodeRepo.cs
T
Dhananjaya99 bffc9b1686 first commit
2026-09-28 11:44:26 +05:30

40 lines
1.3 KiB
C#

using Fitness_core_BE.Interfaces;
using Fitness_core_BE.Models;
using Microsoft.EntityFrameworkCore;
namespace Fitness_core_BE.Repos
{
public class PairingCodeRepo : IPairingCodeRepo
{
// Central: created by platform Admin (no TenantId claim) and redeemed by an anonymous
// agent enroll call (no tenant context at all yet). See CentralDbContext.PairingCodes.
private readonly CentralDbContext _dbContext;
public PairingCodeRepo(CentralDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<PairingCode> AddAsync(PairingCode pairingCode)
{
await _dbContext.PairingCodes.AddAsync(pairingCode);
return pairingCode;
}
// Active = not used and not expired.
public async Task<PairingCode?> GetActiveByCodeAsync(string code)
{
var now = DateTime.UtcNow;
return await _dbContext.PairingCodes
.FirstOrDefaultAsync(p => p.Code == code && !p.IsUsed && p.ExpiresAt > now);
}
public Task MarkUsedAsync(PairingCode pairingCode)
{
pairingCode.IsUsed = true;
_dbContext.PairingCodes.Update(pairingCode);
return Task.CompletedTask;
}
}
}