DeutschLernen/GermanApp/Infrastructure/Data/Repositories/LevelRepository.cs
Lasse Rune Hansen ba81359afa fix(backend): update Lesson entity and related code to use LevelId foreign key
- Updated Lesson entity to use LevelId (int) instead of Level (int)
- Added navigation property Level (Level entity)
- Added Order, Topic, and IsActive properties to Lesson
- Updated Lesson.Create() factory method signature to accept levelId, title, order, topic, description
- Split Update method into individual property update methods
- Updated LessonDto to use LevelId, LevelName, LevelCode instead of Level int
- Added CreateLessonDto and UpdateLessonDto with all required fields
- Added UpdateFromDto extension method for Lesson
- Updated CreateLessonCommand to validate LevelId instead of Level
- Updated SeedDataExtension to seed Level entities first, then use new Lesson.Create() signature
- Made SeedDatabaseAsync async and updated Program.cs to await it
- Updated LessonsEndpoints to use GetByLevelAsync for beginner/advanced queries
- Fixed missing using directive for Domain.Entities

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-07 21:25:46 +02:00

95 lines
3.1 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 ILevelRepository.
/// This is part of the Infrastructure layer.
/// </summary>
public class LevelRepository : ILevelRepository
{
private readonly AppDbContext _context;
public LevelRepository(AppDbContext context)
{
_context = context;
}
public async Task<Level?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Levels
.FirstOrDefaultAsync(l => l.Id == id, cancellationToken);
}
public async Task<IReadOnlyList<Level>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _context.Levels
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<Level> 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<bool> ExistsAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Levels
.AnyAsync(l => l.Id == id, cancellationToken);
}
public async Task<Level?> GetByCodeAsync(string code, CancellationToken cancellationToken = default)
{
return await _context.Levels
.FirstOrDefaultAsync(l => l.Code == code.ToUpperInvariant(), cancellationToken);
}
public async Task<IReadOnlyList<Level>> GetAllOrderedAsync(CancellationToken cancellationToken = default)
{
return await _context.Levels
.OrderBy(l => l.Order)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<Level?> GetFirstLevelAsync(CancellationToken cancellationToken = default)
{
return await _context.Levels
.OrderBy(l => l.Order)
.FirstOrDefaultAsync(cancellationToken);
}
public async Task<Level?> 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);
}
}