freetubesync/FreeTubeSync/Repository.cs
Mario Steele 4985dc4179 Started the Split
Split Json data models coming from REST Api, from the Database models
storing them in a SQLite database.
Work to re-engineer endpoints to use Database objects, and copy/update
data from the json objects.
More work is needed.
2025-07-22 17:03:33 -05:00

38 lines
No EOL
1.1 KiB
C#

using FreeTubeSync.Database;
using Microsoft.EntityFrameworkCore;
namespace FreeTubeSync;
public class Repository<TEntity>(DataContext dbContext) : IRepository<TEntity> where TEntity : class
{
public async Task AddAsync(TEntity entity, CancellationToken ct, bool sync = true)
{
dbContext.Set<TEntity>().Add(entity);
if(sync)
await dbContext.SaveChangesAsync(ct);
}
public async Task UpdateAsync(TEntity entity, CancellationToken ct, bool sync = true)
{
dbContext.Set<TEntity>().Update(entity);
if (sync)
await dbContext.SaveChangesAsync(ct);
}
public async Task DeleteAsync(TEntity entity, CancellationToken ct, bool sync = true)
{
dbContext.Set<TEntity>().Remove(entity);
if (sync)
await dbContext.SaveChangesAsync(ct);
}
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);
}
}