using GermanApp.Domain.Entities; using GermanApp.Domain.Interfaces; using GermanApp.Infrastructure.Data.DbContext; using Microsoft.EntityFrameworkCore; namespace GermanApp.Infrastructure.Data.Repositories; /// /// Entity Framework Core implementation of ILevelRepository. /// This is part of the Infrastructure layer. /// public class LevelRepository : ILevelRepository { private readonly AppDbContext _context; public LevelRepository(AppDbContext context) { _context = context; } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { return await _context.Levels .FirstOrDefaultAsync(l => l.Id == id, cancellationToken); } public async Task> GetAllAsync(CancellationToken cancellationToken = default) { return await _context.Levels .AsNoTracking() .ToListAsync(cancellationToken); } public async Task AddAsync(Level entity, CancellationToken cancellationToken = default) { await _context.Levels.AddAsync(entity, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return entity; } public async Task UpdateAsync(Level entity, CancellationToken cancellationToken = default) { _context.Levels.Update(entity); await _context.SaveChangesAsync(cancellationToken); } public async Task DeleteAsync(Level entity, CancellationToken cancellationToken = default) { _context.Levels.Remove(entity); await _context.SaveChangesAsync(cancellationToken); } public async Task ExistsAsync(int id, CancellationToken cancellationToken = default) { return await _context.Levels .AnyAsync(l => l.Id == id, cancellationToken); } public async Task GetByCodeAsync(string code, CancellationToken cancellationToken = default) { return await _context.Levels .FirstOrDefaultAsync(l => l.Code == code.ToUpperInvariant(), cancellationToken); } public async Task> GetAllOrderedAsync(CancellationToken cancellationToken = default) { return await _context.Levels .OrderBy(l => l.Order) .AsNoTracking() .ToListAsync(cancellationToken); } public async Task GetFirstLevelAsync(CancellationToken cancellationToken = default) { return await _context.Levels .OrderBy(l => l.Order) .FirstOrDefaultAsync(cancellationToken); } public async Task GetNextLevelAsync(int currentLevelId, CancellationToken cancellationToken = default) { var currentLevel = await _context.Levels .Where(l => l.Id == currentLevelId) .Select(l => l.Order) .FirstOrDefaultAsync(cancellationToken); if (currentLevel == default) return null; return await _context.Levels .Where(l => l.Order > currentLevel) .OrderBy(l => l.Order) .FirstOrDefaultAsync(cancellationToken); } }