Files
2024-05-21 12:10:53 +03:00

38 lines
1.1 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Services.PostgreSQL;
public class BasePostgreSqlRepository<T>(HealthcareManagerDatabase context)
where T : class
{
protected readonly HealthcareManagerDatabase Context = context;
public async Task<T?> GetByIdAsync(Guid id, CancellationToken token)
{
return await Context.Set<T>().FindAsync(id, token);
}
public async Task<IEnumerable<T>> GetAllAsync(CancellationToken token)
{
return await Context.Set<T>().ToListAsync(token);
}
public async Task AddAsync(T entity, CancellationToken token)
{
await Context.Set<T>().AddAsync(entity, token);
await Context.SaveChangesAsync(token);
}
public async Task UpdateAsync(T entity, CancellationToken token)
{
Context.Set<T>().Attach(entity);
Context.Entry(entity).State = EntityState.Modified;
await Context.SaveChangesAsync(token);
}
public async Task DeleteAsync(T entity, CancellationToken token)
{
Context.Set<T>().Remove(entity);
await Context.SaveChangesAsync(token);
}
}