using Microsoft.EntityFrameworkCore;
using NexusReader.Application.Abstractions.Persistence;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
namespace NexusReader.Infrastructure.Persistence;
///
/// EF Core implementation of .
/// Uses for Blazor-safe scoped context creation.
///
internal sealed class ConceptsMapReadRepository : IConceptsMapReadRepository
{
private readonly IDbContextFactory _dbContextFactory;
public ConceptsMapReadRepository(IDbContextFactory dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
///
public async Task GetLastReadPageIdAsync(string userId, CancellationToken cancellationToken = default)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var user = await dbContext.Users
.Where(u => u.Id == userId)
.Select(u => new { u.LastReadPageId })
.FirstOrDefaultAsync(cancellationToken);
return user?.LastReadPageId;
}
///
public async Task> GetKnowledgeUnitsForBookAsync(
Guid bookId,
string tenantId,
CancellationToken cancellationToken = default)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
return await dbContext.KnowledgeUnits
.Where(k => k.EbookId == bookId &&
(k.TenantId == tenantId || k.TenantId == "global" || string.IsNullOrEmpty(k.TenantId)))
.OrderBy(k => k.CreatedAt)
.ToListAsync(cancellationToken);
}
}