After doing testing with a test project, re-organized code to new method of storing, as well as ensure that all Endpoint lambda's are separated out into functions. Large commit, will be testing.
48 lines
No EOL
1.3 KiB
C#
48 lines
No EOL
1.3 KiB
C#
using FreeTubeSync.Database;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FreeTubeSync;
|
|
|
|
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
|
|
{
|
|
private readonly DataContext _context;
|
|
private readonly DbSet<TEntity> _dbSet;
|
|
|
|
public Repository(DataContext context)
|
|
{
|
|
_context = context;
|
|
_dbSet = _context.Set<TEntity>();
|
|
}
|
|
|
|
public async Task AddAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
|
{
|
|
_dbSet.Add(entity);
|
|
if(sync)
|
|
await _context.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task UpdateAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
|
{
|
|
_dbSet.Attach(entity);
|
|
_context.Entry(entity).State = EntityState.Modified;
|
|
if (sync)
|
|
await _context.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task DeleteAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
|
{
|
|
_dbSet.Remove(entity);
|
|
if (sync)
|
|
await _context.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task<TEntity?> GetByIdAsync(string id, CancellationToken ct)
|
|
{
|
|
return await _dbSet.FindAsync(id, ct);
|
|
}
|
|
|
|
public async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken ct)
|
|
{
|
|
return await _dbSet.ToListAsync<TEntity>(ct);
|
|
}
|
|
} |