using GermanApp.Application.DTOs;
using GermanApp.Domain.Entities;
using GermanApp.Domain.Exceptions;
using GermanApp.Domain.Interfaces;
namespace GermanApp.Application.UseCases.Commands;
///
/// Command for creating a new lesson.
/// This follows the CQRS pattern - commands represent write operations.
///
public record CreateLessonCommand(CreateLessonDto LessonData) : ICommand;
///
/// Handler for the CreateLessonCommand.
///
public class CreateLessonCommandHandler : ICommandHandler
{
private readonly ILessonRepository _lessonRepository;
public CreateLessonCommandHandler(ILessonRepository lessonRepository)
{
_lessonRepository = lessonRepository;
}
public async Task Handle(CreateLessonCommand command, CancellationToken cancellationToken)
{
// Convert DTO to domain entity
var lesson = command.LessonData.ToEntity();
// Validate business rules (could be extracted to a validator)
if (lesson.Level < 1 || lesson.Level > 5)
{
throw new ValidationException("Level must be between 1 and 5");
}
// Add to repository
var createdLesson = await _lessonRepository.AddAsync(lesson, cancellationToken);
// Return DTO representation
return createdLesson.ToDto();
}
}
///
/// Generic command interface.
///
/// The result type
public interface ICommand
{
}
///
/// Generic command handler interface.
///
/// The command type
/// The result type
public interface ICommandHandler
where TCommand : ICommand
{
Task Handle(TCommand command, CancellationToken cancellationToken);
}
// Note: For MediatR integration, you would implement IRequestHandler
// instead of the custom interfaces above.