using Femto.Modules.Media.Data;
using Femto.Modules.Media.Infrastructure;
using MediatR;

namespace Femto.Modules.Media.Contracts.SaveFile;

internal class SaveFileCommandHandler(IStorageProvider storage, MediaContext context)
    : IRequestHandler<SaveFileCommand, Guid>
{
    public async Task<Guid> Handle(SaveFileCommand command, CancellationToken cancellationToken)
    {
        var id = Guid.CreateVersion7();
        await storage.SaveBlob(id.ToString(), command.Data);
        await context.AddAsync(new SavedBlob(id, command.ContentType, command.ContentLength), cancellationToken);
        await context.SaveChangesAsync(cancellationToken);
        return id;
    }
}
