private async Task <Validation <BaseError, RequestParameters> > Validate(IScanLocalLibrary request)
    {
        Validation <BaseError, LocalLibrary> libraryResult = await LocalLibraryMustExist(request);

        Validation <BaseError, string> ffprobePathResult = await ValidateFFprobePath();

        Validation <BaseError, string> ffmpegPathResult = await ValidateFFmpegPath();

        Validation <BaseError, int> refreshIntervalResult = await ValidateLibraryRefreshInterval();

        try
        {
            return((libraryResult, ffprobePathResult, ffmpegPathResult, refreshIntervalResult)
                   .Apply(
                       (library, ffprobePath, ffmpegPath, libraryRefreshInterval) => new RequestParameters(
                           library,
                           ffprobePath,
                           ffmpegPath,
                           request.ForceScan,
                           libraryRefreshInterval)));
        }
        finally
        {
            // ensure we unlock the library if any validation is unsuccessful
            foreach (LocalLibrary library in libraryResult.SuccessToSeq())
            {
                if (ffprobePathResult.IsFail || ffmpegPathResult.IsFail || refreshIntervalResult.IsFail)
                {
                    _entityLocker.UnlockLibrary(library.Id);
                }
            }
        }
    }
 private async Task <Validation <BaseError, RequestParameters> > Validate(IScanLocalLibrary request) =>
 (await LocalLibraryMustExist(request), await ValidateFFprobePath())
 Handle(IScanLocalLibrary request) =>
 Validate(request)
 .MapT(parameters => PerformScan(parameters).Map(_ => parameters.LocalLibrary.Name))
 .Bind(v => v.ToEitherAsync());
 private Task <Either <BaseError, string> > Handle(IScanLocalLibrary request, CancellationToken cancellationToken) =>
 Validate(request)
 .MapT(parameters => PerformScan(parameters, cancellationToken).Map(_ => parameters.LocalLibrary.Name))
 .Bind(v => v.ToEitherAsync());
 private Task <Validation <BaseError, LocalLibrary> > LocalLibraryMustExist(
     IScanLocalLibrary request) =>
 _libraryRepository.Get(request.LibraryId)
 .Map(maybeLibrary => maybeLibrary.Map(ms => ms as LocalLibrary))
 .Map(v => v.ToValidation <BaseError>($"Local library {request.LibraryId} does not exist."));