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

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

public class SetGenreNameCommand : IRequest<Unit>
{
    public Guid Id { get; set; } = default!;
    public string Name { get; set; } = default!;
}

public class SetGenreNameCommandHandler(IGenreGateway genreGateway) : IRequestHandler<SetGenreNameCommand, Unit>
{
    public async Task<Unit> Handle(SetGenreNameCommand request, CancellationToken cancellationToken)
    {
        if (await genreGateway.IsGenreExists(request.Name, request.Id)) throw new Exception("Genre with the same name already exist");
        await genreGateway.SetName(request.Id, request.Name);
        return Unit.Value;
    }
}