academia/src/backend/Adapters/Driven/Persistence/DataLoaders/ProfessorByIdDataLoader.cs

31 lines
993 B
C#

namespace Adapters.Driven.Persistence.DataLoaders;
using Adapters.Driven.Persistence.Context;
using Domain.Entities;
using GreenDonut;
using Microsoft.EntityFrameworkCore;
public class ProfessorByIdDataLoader : BatchDataLoader<int, Professor>
{
private readonly IDbContextFactory<AppDbContext> _contextFactory;
public ProfessorByIdDataLoader(
IDbContextFactory<AppDbContext> contextFactory,
IBatchScheduler batchScheduler,
DataLoaderOptions? options = null)
: base(batchScheduler, options ?? new DataLoaderOptions())
{
_contextFactory = contextFactory;
}
protected override async Task<IReadOnlyDictionary<int, Professor>> LoadBatchAsync(
IReadOnlyList<int> keys,
CancellationToken ct)
{
await using var context = await _contextFactory.CreateDbContextAsync(ct);
return await context.Professors
.Where(p => keys.Contains(p.Id))
.ToDictionaryAsync(p => p.Id, ct);
}
}