DeutschLernen/GermanApp/Infrastructure/Data/Repositories/LessonRepository.cs
Lasse Rune Hansen d90e4792d6 Initial commit: GermanApp with Clean Architecture
- Domain layer: Lesson entity, GermanWord value object, repository interfaces
- Application layer: CQRS commands, DTOs with mapping
- Infrastructure layer: EF Core with SQLite, LessonRepository
- Presentation layer: Minimal API endpoints for lessons CRUD

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-05-31 18:14:51 +02:00

82 lines
2.7 KiB
C#

using GermanApp.Domain.Entities;
using GermanApp.Domain.Interfaces;
using GermanApp.Infrastructure.Data.DbContext;
using Microsoft.EntityFrameworkCore;
namespace GermanApp.Infrastructure.Data.Repositories;
/// <summary>
/// Entity Framework Core implementation of ILessonRepository.
/// This is part of the Infrastructure layer.
/// </summary>
public class LessonRepository : ILessonRepository
{
private readonly AppDbContext _context;
public LessonRepository(AppDbContext context)
{
_context = context;
}
public async Task<Lesson?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.FirstOrDefaultAsync(l => l.Id == id, cancellationToken);
}
public async Task<IReadOnlyList<Lesson>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<Lesson> 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<bool> ExistsAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.AnyAsync(l => l.Id == id, cancellationToken);
}
public async Task<IReadOnlyList<Lesson>> GetByLevelAsync(int level, CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level == level)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<IReadOnlyList<Lesson>> GetBeginnerLessonsAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level <= 2)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<IReadOnlyList<Lesson>> GetAdvancedLessonsAsync(CancellationToken cancellationToken = default)
{
return await _context.Lessons
.Where(l => l.Level >= 4)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
}