Change logging to just log a message, instead of the exception. Moved logic to Syncer involvement. When posting the data, if a 500 is returned, then it is up to the Syncer to re-submit it.
51 lines
No EOL
1.4 KiB
C#
51 lines
No EOL
1.4 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)
|
|
{
|
|
if (!_dbSet.Local.Contains(entity))
|
|
{
|
|
_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);
|
|
}
|
|
} |