Files
annotations/Azaion.Common/Database/AnnotationRepository.cs
T

49 lines
1.4 KiB
C#

using LinqToDB;
namespace Azaion.Common.Database;
public class AnnotationRepository : IAnnotationRepository
{
private readonly IDbFactory _dbFactory;
public AnnotationRepository(IDbFactory dbFactory)
{
_dbFactory = dbFactory;
}
public async Task<List<Annotation>> GetByMediaHashAsync(string hash, CancellationToken ct = default)
{
return await _dbFactory.Run(async db =>
await db.GetTable<Annotation>()
.Where(x => x.MediaHash == hash)
.ToListAsync(ct));
}
public async Task<Annotation?> GetByNameAsync(string name, CancellationToken ct = default)
{
return await _dbFactory.Run(async db =>
await db.GetTable<Annotation>()
.FirstOrDefaultAsync(x => x.Name == name, ct));
}
public async Task<List<Annotation>> GetAllAsync(CancellationToken ct = default)
{
return await _dbFactory.Run(async db =>
await db.GetTable<Annotation>().ToListAsync(ct));
}
public async Task SaveAsync(Annotation annotation, CancellationToken ct = default)
{
await _dbFactory.RunWrite(async db =>
{
await db.InsertOrReplaceAsync(annotation, token: ct);
});
}
public async Task DeleteAsync(List<string> names, CancellationToken ct = default)
{
await _dbFactory.DeleteAnnotations(names, ct);
}
}