public async Task <Either <BaseError, LocalLibraryViewModel> > Handle( CreateLocalLibrary request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); Validation <BaseError, LocalLibrary> validation = await Validate(dbContext, request); return(await LanguageExtensions.Apply(validation, localLibrary => PersistLocalLibrary(dbContext, localLibrary))); }
private static Task <Validation <BaseError, LocalLibrary> > MediaSourceMustExist( TvContext dbContext, CreateLocalLibrary request) => dbContext.LocalMediaSources .OrderBy(lms => lms.Id) .FirstOrDefaultAsync() .Map(Optional) .MapT( lms => new LocalLibrary { Name = request.Name, Paths = request.Paths.Map(p => new LibraryPath { Path = p }).ToList(), MediaKind = request.MediaKind, MediaSourceId = lms.Id }) .Map(o => o.ToValidation <BaseError>("LocalMediaSource does not exist."));
private static Task <Validation <BaseError, LocalLibrary> > Validate( TvContext dbContext, CreateLocalLibrary request) => MediaSourceMustExist(dbContext, request) .BindT(localLibrary => NameMustBeValid(request, localLibrary)) .BindT(localLibrary => PathsMustBeValid(dbContext, localLibrary));