feat(db): PendingSecuredWrite entity + migration + repository (T14b)

This commit is contained in:
Joseph Doherty
2026-06-18 02:09:31 -04:00
parent a0ce8b6c44
commit c799f41d53
10 changed files with 2477 additions and 0 deletions
@@ -0,0 +1,82 @@
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.SecuredWrites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
/// <summary>
/// EF Core implementation of <see cref="ISecuredWriteRepository"/> over the central
/// <c>PendingSecuredWrites</c> table (M7 OPC UA / MxGateway UX, Task T14b). Mirrors the
/// <c>SiteCallAuditRepository</c> data-access shape: plain tracked EF reads/writes
/// against the shared <see cref="ScadaBridgeDbContext"/>, no raw SQL needed.
/// </summary>
public class SecuredWriteRepository : ISecuredWriteRepository
{
private readonly ScadaBridgeDbContext _context;
/// <summary>
/// Initializes a new instance of the <see cref="SecuredWriteRepository"/> class.
/// </summary>
/// <param name="context">The EF Core database context.</param>
public SecuredWriteRepository(ScadaBridgeDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
/// <inheritdoc />
public async Task<long> AddAsync(PendingSecuredWrite securedWrite, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(securedWrite);
await _context.Set<PendingSecuredWrite>().AddAsync(securedWrite, ct);
await _context.SaveChangesAsync(ct);
return securedWrite.Id;
}
/// <inheritdoc />
public async Task<PendingSecuredWrite?> GetAsync(long id, CancellationToken ct = default)
{
return await _context.Set<PendingSecuredWrite>()
.FindAsync(new object?[] { id }, ct);
}
/// <inheritdoc />
public async Task UpdateAsync(PendingSecuredWrite securedWrite, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(securedWrite);
// The caller hands back the same tracked instance it read; Update covers the
// detached case too (re-attaches and marks every column modified).
_context.Set<PendingSecuredWrite>().Update(securedWrite);
await _context.SaveChangesAsync(ct);
}
/// <inheritdoc />
public async Task<IReadOnlyList<PendingSecuredWrite>> QueryAsync(
string? status,
string? siteId,
int skip,
int take,
CancellationToken ct = default)
{
IQueryable<PendingSecuredWrite> query = _context.Set<PendingSecuredWrite>()
.AsNoTracking();
if (!string.IsNullOrWhiteSpace(status))
{
query = query.Where(p => p.Status == status);
}
if (!string.IsNullOrWhiteSpace(siteId))
{
query = query.Where(p => p.SiteId == siteId);
}
return await query
.OrderByDescending(p => p.SubmittedAtUtc)
.ThenByDescending(p => p.Id)
.Skip(skip)
.Take(take)
.ToListAsync(ct);
}
}