using GermanApp.Application.DTOs; using GermanApp.Domain.Entities; using GermanApp.Domain.Interfaces; namespace GermanApp.Application.Services; /// /// Application service for managing CEFR levels. /// This is part of the Application layer. /// public class LevelService { private readonly ILevelRepository _levelRepository; public LevelService(ILevelRepository levelRepository) { _levelRepository = levelRepository; } /// /// Gets all CEFR levels ordered by their sort order. /// public async Task> GetAllLevelsAsync(CancellationToken cancellationToken = default) { var levels = await _levelRepository.GetAllOrderedAsync(cancellationToken); return levels.Select(l => l.ToDto()).ToList(); } /// /// Gets a level by its ID. /// public async Task GetLevelByIdAsync(int id, CancellationToken cancellationToken = default) { var level = await _levelRepository.GetByIdAsync(id, cancellationToken); return level?.ToDto(); } /// /// Gets a level by its code (e.g., "A1", "B2"). /// public async Task GetLevelByCodeAsync(string code, CancellationToken cancellationToken = default) { var level = await _levelRepository.GetByCodeAsync(code, cancellationToken); return level?.ToDto(); } /// /// Creates a new CEFR level. /// public async Task CreateLevelAsync(CreateLevelDto dto, CancellationToken cancellationToken = default) { var level = dto.ToEntity(); var createdLevel = await _levelRepository.AddAsync(level, cancellationToken); return createdLevel.ToDto(); } /// /// Updates an existing CEFR level. /// public async Task UpdateLevelAsync(int id, UpdateLevelDto dto, CancellationToken cancellationToken = default) { var level = await _levelRepository.GetByIdAsync(id, cancellationToken); if (level == null) return null; level.UpdateFromDto(dto); await _levelRepository.UpdateAsync(level, cancellationToken); return level.ToDto(); } /// /// Deletes a CEFR level by its ID. /// public async Task DeleteLevelAsync(int id, CancellationToken cancellationToken = default) { var level = await _levelRepository.GetByIdAsync(id, cancellationToken); if (level == null) return false; await _levelRepository.DeleteAsync(level, cancellationToken); return true; } /// /// Gets the first level (lowest order number) - typically A1. /// public async Task GetFirstLevelAsync(CancellationToken cancellationToken = default) { var level = await _levelRepository.GetFirstLevelAsync(cancellationToken); return level?.ToDto(); } /// /// Gets the next level after the specified one. /// public async Task GetNextLevelAsync(int currentLevelId, CancellationToken cancellationToken = default) { var level = await _levelRepository.GetNextLevelAsync(currentLevelId, cancellationToken); return level?.ToDto(); } }