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 ILessonRepository.
/// This is part of the Infrastructure layer.
///
public class LessonRepository : ILessonRepository
{
private readonly AppDbContext _context;
public LessonRepository(AppDbContext context)
{
_context = context;
}
public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.FirstOrDefaultAsync(l => l.Id == id, cancellationToken);
}
public async Task> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task AddAsync(Lesson entity, CancellationToken cancellationToken = default)
{
await _context.Lessons.AddAsync(entity, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return entity;
}
public async Task UpdateAsync(Lesson entity, CancellationToken cancellationToken = default)
{
_context.Lessons.Update(entity);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task DeleteAsync(Lesson entity, CancellationToken cancellationToken = default)
{
_context.Lessons.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task ExistsAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.AnyAsync(l => l.Id == id, cancellationToken);
}
public async Task> GetByLevelAsync(int level, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level == level)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task> GetBeginnerLessonsAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level <= 2)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task> GetAdvancedLessonsAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level >= 4)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
}