using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
public class InboundApiRepository : IInboundApiRepository
{
private readonly ScadaBridgeDbContext _context;
///
/// Initializes a new instance of the InboundApiRepository class.
///
/// The database context for accessing inbound API data.
public InboundApiRepository(ScadaBridgeDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
///
public async Task GetApiMethodByIdAsync(int id, CancellationToken cancellationToken = default)
=> await _context.Set().FindAsync(new object[] { id }, cancellationToken);
///
public async Task> GetAllApiMethodsAsync(CancellationToken cancellationToken = default)
=> await _context.Set().ToListAsync(cancellationToken);
///
public async Task GetMethodByNameAsync(string name, CancellationToken cancellationToken = default)
=> await _context.Set().FirstOrDefaultAsync(m => m.Name == name, cancellationToken);
///
public async Task AddApiMethodAsync(ApiMethod method, CancellationToken cancellationToken = default)
=> await _context.Set().AddAsync(method, cancellationToken);
///
public Task UpdateApiMethodAsync(ApiMethod method, CancellationToken cancellationToken = default)
{ _context.Set().Update(method); return Task.CompletedTask; }
///
public async Task DeleteApiMethodAsync(int id, CancellationToken cancellationToken = default)
{
var entity = await GetApiMethodByIdAsync(id, cancellationToken);
if (entity != null) _context.Set().Remove(entity);
}
///
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
=> await _context.SaveChangesAsync(cancellationToken);
}