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 virtual 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 virtual 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 virtual async Task GetLevelByCodeAsync(string code, CancellationToken cancellationToken = default)
{
var level = await _levelRepository.GetByCodeAsync(code, cancellationToken);
return level?.ToDto();
}
///
/// Creates a new CEFR level.
///
public virtual 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 virtual 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 virtual 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 virtual async Task GetFirstLevelAsync(CancellationToken cancellationToken = default)
{
var level = await _levelRepository.GetFirstLevelAsync(cancellationToken);
return level?.ToDto();
}
///
/// Gets the next level after the specified one.
///
public virtual async Task GetNextLevelAsync(int currentLevelId, CancellationToken cancellationToken = default)
{
var level = await _levelRepository.GetNextLevelAsync(currentLevelId, cancellationToken);
return level?.ToDto();
}
}