47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
using LiquidCode.Db;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LiquidCode.Repositories;
|
|
|
|
/// <summary>
|
|
/// Base repository implementation providing common CRUD operations
|
|
/// </summary>
|
|
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
|
|
{
|
|
protected readonly LiquidDbContext DbContext;
|
|
protected readonly DbSet<TEntity> DbSet;
|
|
|
|
public Repository(LiquidDbContext dbContext)
|
|
{
|
|
DbContext = dbContext;
|
|
DbSet = dbContext.Set<TEntity>();
|
|
}
|
|
|
|
public virtual async Task<TEntity?> FindByIdAsync(int id, CancellationToken cancellationToken = default) =>
|
|
await DbSet.FindAsync(new object?[] { id }, cancellationToken);
|
|
|
|
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default) =>
|
|
await DbSet.ToListAsync(cancellationToken);
|
|
|
|
public virtual async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
await DbSet.AddAsync(entity, cancellationToken);
|
|
await SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public virtual async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
DbSet.Update(entity);
|
|
await SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public virtual async Task RemoveAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
DbSet.Remove(entity);
|
|
await SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task SaveChangesAsync(CancellationToken cancellationToken = default) =>
|
|
await DbContext.SaveChangesAsync(cancellationToken);
|
|
}
|