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); 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); 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); 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); }