Possibly fix issues
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.
This commit is contained in:
parent
2e4f644c17
commit
73955353cb
23 changed files with 314 additions and 352 deletions
|
|
@ -3,36 +3,46 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace FreeTubeSync;
|
||||
|
||||
public class Repository<TEntity>(DataContext dbContext) : IRepository<TEntity> where TEntity : class
|
||||
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)
|
||||
{
|
||||
dbContext.Set<TEntity>().Add(entity);
|
||||
_dbSet.Add(entity);
|
||||
if(sync)
|
||||
await dbContext.SaveChangesAsync(ct);
|
||||
await _context.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
||||
{
|
||||
dbContext.Set<TEntity>().Update(entity);
|
||||
_dbSet.Attach(entity);
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
if (sync)
|
||||
await dbContext.SaveChangesAsync(ct);
|
||||
await _context.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(TEntity entity, CancellationToken ct, bool sync = true)
|
||||
{
|
||||
dbContext.Set<TEntity>().Remove(entity);
|
||||
_dbSet.Remove(entity);
|
||||
if (sync)
|
||||
await dbContext.SaveChangesAsync(ct);
|
||||
await _context.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<TEntity?> GetByIdAsync(string id, CancellationToken ct)
|
||||
{
|
||||
return await dbContext.Set<TEntity>().FindAsync(id, ct);
|
||||
return await _dbSet.FindAsync(id, ct);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken ct)
|
||||
{
|
||||
return await dbContext.Set<TEntity>().ToListAsync<TEntity>(ct);
|
||||
return await _dbSet.ToListAsync<TEntity>(ct);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue