using Microsoft.EntityFrameworkCore; using ScadaLink.Commons.Entities.ExternalSystems; using ScadaLink.Commons.Interfaces.Repositories; namespace ScadaLink.ConfigurationDatabase.Repositories; public class ExternalSystemRepository : IExternalSystemRepository { private readonly ScadaLinkDbContext _context; public ExternalSystemRepository(ScadaLinkDbContext context) { _context = context ?? throw new ArgumentNullException(nameof(context)); } public async Task GetExternalSystemByIdAsync(int id, CancellationToken cancellationToken = default) => await _context.Set().FindAsync(new object[] { id }, cancellationToken); // ExternalSystemGateway-011: genuine name-keyed query (server-side WHERE) so the // gateway's hot-path resolution does not fetch every system and filter in memory. public async Task GetExternalSystemByNameAsync(string name, CancellationToken cancellationToken = default) => await _context.Set() .FirstOrDefaultAsync(s => s.Name == name, cancellationToken); public async Task> GetAllExternalSystemsAsync(CancellationToken cancellationToken = default) => await _context.Set().ToListAsync(cancellationToken); public async Task AddExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default) => await _context.Set().AddAsync(definition, cancellationToken); public Task UpdateExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default) { _context.Set().Update(definition); return Task.CompletedTask; } public async Task DeleteExternalSystemAsync(int id, CancellationToken cancellationToken = default) { var entity = await GetExternalSystemByIdAsync(id, cancellationToken); if (entity != null) _context.Set().Remove(entity); } public async Task GetExternalSystemMethodByIdAsync(int id, CancellationToken cancellationToken = default) => await _context.Set().FindAsync(new object[] { id }, cancellationToken); // ExternalSystemGateway-011: genuine name-keyed query scoped to the parent system. public async Task GetMethodByNameAsync(int externalSystemId, string methodName, CancellationToken cancellationToken = default) => await _context.Set() .FirstOrDefaultAsync( m => m.ExternalSystemDefinitionId == externalSystemId && m.Name == methodName, cancellationToken); public async Task> GetMethodsByExternalSystemIdAsync(int externalSystemId, CancellationToken cancellationToken = default) => await _context.Set().Where(m => m.ExternalSystemDefinitionId == externalSystemId).ToListAsync(cancellationToken); public async Task AddExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default) => await _context.Set().AddAsync(method, cancellationToken); public Task UpdateExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default) { _context.Set().Update(method); return Task.CompletedTask; } public async Task DeleteExternalSystemMethodAsync(int id, CancellationToken cancellationToken = default) { var entity = await GetExternalSystemMethodByIdAsync(id, cancellationToken); if (entity != null) _context.Set().Remove(entity); } public async Task GetDatabaseConnectionByIdAsync(int id, CancellationToken cancellationToken = default) => await _context.Set().FindAsync(new object[] { id }, cancellationToken); // ExternalSystemGateway-011: genuine name-keyed query (server-side WHERE). public async Task GetDatabaseConnectionByNameAsync(string name, CancellationToken cancellationToken = default) => await _context.Set() .FirstOrDefaultAsync(c => c.Name == name, cancellationToken); public async Task> GetAllDatabaseConnectionsAsync(CancellationToken cancellationToken = default) => await _context.Set().ToListAsync(cancellationToken); public async Task AddDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default) => await _context.Set().AddAsync(definition, cancellationToken); public Task UpdateDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default) { _context.Set().Update(definition); return Task.CompletedTask; } public async Task DeleteDatabaseConnectionAsync(int id, CancellationToken cancellationToken = default) { var entity = await GetDatabaseConnectionByIdAsync(id, cancellationToken); if (entity != null) _context.Set().Remove(entity); } public async Task SaveChangesAsync(CancellationToken cancellationToken = default) => await _context.SaveChangesAsync(cancellationToken); }