use cancellation tokens in many places (#2350)

* use cancellation tokens everywhere

* more cancellation tokens
This commit is contained in:
Jason Dove
2025-08-27 03:20:35 +00:00
committed by GitHub
parent a6198892f0
commit 1c07df5bc3
356 changed files with 3430 additions and 2454 deletions
@@ -6,25 +6,20 @@ using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Infrastructure.Data.Repositories;
public class ConfigElementRepository : IConfigElementRepository
public class ConfigElementRepository(IDbContextFactory<TvContext> dbContextFactory) : IConfigElementRepository
{
private readonly IDbContextFactory<TvContext> _dbContextFactory;
public ConfigElementRepository(IDbContextFactory<TvContext> dbContextFactory) =>
_dbContextFactory = dbContextFactory;
public async Task<Unit> Upsert<T>(ConfigElementKey configElementKey, T value)
public async Task<Unit> Upsert<T>(ConfigElementKey configElementKey, T value, CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<ConfigElement> maybeElement = await dbContext.ConfigElements
.SelectOneAsync(c => c.Key, c => c.Key == configElementKey.Key);
.SelectOneAsync(c => c.Key, c => c.Key == configElementKey.Key, cancellationToken);
await maybeElement.Match(
async element =>
{
element.Value = value.ToString();
await dbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
},
async () =>
{
@@ -34,25 +29,23 @@ public class ConfigElementRepository : IConfigElementRepository
Value = value.ToString()
};
await dbContext.ConfigElements.AddAsync(configElement);
await dbContext.SaveChangesAsync();
await dbContext.ConfigElements.AddAsync(configElement, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
});
return Unit.Default;
}
public async Task<Option<ConfigElement>> GetConfigElement(ConfigElementKey key)
public async Task<Option<ConfigElement>> GetConfigElement(ConfigElementKey key, CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
return await dbContext.ConfigElements
.AsNoTracking()
.OrderBy(ce => ce.Key)
.SingleOrDefaultAsync(ce => ce.Key == key.Key)
.Map(Optional);
.SelectOneAsync(ce => ce.Key, ce => ce.Key == key.Key, cancellationToken);
}
public Task<Option<T>> GetValue<T>(ConfigElementKey key) =>
GetConfigElement(key).MapT(ce =>
public Task<Option<T>> GetValue<T>(ConfigElementKey key, CancellationToken cancellationToken) =>
GetConfigElement(key, cancellationToken).MapT(ce =>
{
if (typeof(T).Name == "Guid")
{
@@ -67,24 +60,24 @@ public class ConfigElementRepository : IConfigElementRepository
return (T)Convert.ChangeType(ce.Value, typeof(T), CultureInfo.InvariantCulture);
});
public async Task Delete(ConfigElement configElement)
public async Task Delete(ConfigElement configElement, CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
dbContext.ConfigElements.Remove(configElement);
await dbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<Unit> Delete(ConfigElementKey configElementKey)
public async Task<Unit> Delete(ConfigElementKey configElementKey, CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<ConfigElement> maybeExisting = await dbContext.ConfigElements
.SelectOneAsync(ce => ce.Key, ce => ce.Key == configElementKey.Key);
.SelectOneAsync(ce => ce.Key, ce => ce.Key == configElementKey.Key, cancellationToken);
foreach (ConfigElement element in maybeExisting)
{
dbContext.ConfigElements.Remove(element);
}
await dbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
return Unit.Default;
}