2025-07-19 04:02:09 -05:00
|
|
|
using FreeTubeSync.Database;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace FreeTubeSync;
|
|
|
|
|
|
|
|
|
|
public class Repository<TEntity>(DataContext dbContext) : IRepository<TEntity> where TEntity : class
|
|
|
|
|
{
|
2025-07-22 17:03:33 -05:00
|
|
|
public async Task AddAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
|
|
|
|
dbContext.Set<TEntity>().Add(entity);
|
2025-07-22 17:03:33 -05:00
|
|
|
if(sync)
|
|
|
|
|
await dbContext.SaveChangesAsync(ct);
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:03:33 -05:00
|
|
|
public async Task UpdateAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
|
|
|
|
dbContext.Set<TEntity>().Update(entity);
|
2025-07-22 17:03:33 -05:00
|
|
|
if (sync)
|
|
|
|
|
await dbContext.SaveChangesAsync(ct);
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 17:03:33 -05:00
|
|
|
public async Task DeleteAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
|
|
|
|
dbContext.Set<TEntity>().Remove(entity);
|
2025-07-22 17:03:33 -05:00
|
|
|
if (sync)
|
|
|
|
|
await dbContext.SaveChangesAsync(ct);
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TEntity?> GetByIdAsync(string id, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
return await dbContext.Set<TEntity>().FindAsync(id, ct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
return await dbContext.Set<TEntity>().ToListAsync<TEntity>(ct);
|
|
|
|
|
}
|
|
|
|
|
}
|