finalizare 1.0
This commit is contained in:
@@ -1,43 +1,38 @@
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Services.PostgreSQL;
|
||||
|
||||
public class BasePostgreSQLRepository<T> where T : class
|
||||
public class BasePostgreSqlRepository<T>(HealthcareManagerDatabase context)
|
||||
where T : class
|
||||
{
|
||||
protected readonly HealthcareManagerDatabase _context;
|
||||
protected readonly HealthcareManagerDatabase Context = context;
|
||||
|
||||
public BasePostgreSQLRepository(HealthcareManagerDatabase context)
|
||||
public async Task<T?> GetByIdAsync(Guid id, CancellationToken token)
|
||||
{
|
||||
_context = context;
|
||||
return await Context.Set<T>().FindAsync(id, token);
|
||||
}
|
||||
|
||||
public async Task<T?> GetByIdAsync(Guid id)
|
||||
public async Task<IEnumerable<T>> GetAllAsync(CancellationToken token)
|
||||
{
|
||||
return await _context.Set<T>().FindAsync(id);
|
||||
return await Context.Set<T>().ToListAsync(token);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllAsync()
|
||||
public async Task AddAsync(T entity, CancellationToken token)
|
||||
{
|
||||
return await _context.Set<T>().ToListAsync();
|
||||
await Context.Set<T>().AddAsync(entity, token);
|
||||
await Context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
public async Task AddAsync(T entity)
|
||||
public async Task UpdateAsync(T entity, CancellationToken token)
|
||||
{
|
||||
await _context.Set<T>().AddAsync(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
Context.Set<T>().Attach(entity);
|
||||
Context.Entry(entity).State = EntityState.Modified;
|
||||
await Context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(T entity)
|
||||
public async Task DeleteAsync(T entity, CancellationToken token)
|
||||
{
|
||||
_context.Set<T>().Attach(entity);
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(T entity)
|
||||
{
|
||||
_context.Set<T>().Remove(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
Context.Set<T>().Remove(entity);
|
||||
await Context.SaveChangesAsync(token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user