using MediatR;
using Modules.Library.Application.Gateways;

namespace Modules.Library.Application.Commands.Dictionaries.Language;

public class CreateLanguageCommand : IRequest<Guid>
{
    public string Code { get; set; } = default!;
    public string Name { get; set; } = default!;
}

public class CreateLanguageCommandHandler(ILanguageGateway languageGateway) : IRequestHandler<CreateLanguageCommand, Guid>
{
    public async Task<Guid> Handle(CreateLanguageCommand request, CancellationToken cancellationToken)
    {
        if (await languageGateway.IsLanguageExists(request.Code, request.Name)) throw new Exception("Language with the same name or code already exists");
        return await languageGateway.Create(request.Code, request.Name);
    }
}