mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.7 KiB
46 lines
1.7 KiB
using System.Collections.Generic; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Core.Interfaces.Repositories; |
|
using ErsatzTV.Core.Interfaces.Search; |
|
using LanguageExt; |
|
|
|
namespace ErsatzTV.Application.Libraries.Commands |
|
{ |
|
public class |
|
DeleteLocalLibraryPathHandler : MediatR.IRequestHandler<DeleteLocalLibraryPath, Either<BaseError, Unit>> |
|
{ |
|
private readonly ILibraryRepository _libraryRepository; |
|
private readonly ISearchIndex _searchIndex; |
|
|
|
public DeleteLocalLibraryPathHandler(ILibraryRepository libraryRepository, ISearchIndex searchIndex) |
|
{ |
|
_libraryRepository = libraryRepository; |
|
_searchIndex = searchIndex; |
|
} |
|
|
|
public Task<Either<BaseError, Unit>> Handle( |
|
DeleteLocalLibraryPath request, |
|
CancellationToken cancellationToken) => |
|
MediaSourceMustExist(request) |
|
.MapT(DoDeletion) |
|
.Bind(t => t.ToEitherAsync()); |
|
|
|
private async Task<Unit> DoDeletion(LibraryPath libraryPath) |
|
{ |
|
List<int> ids = await _libraryRepository.GetMediaIdsByLocalPath(libraryPath.Id); |
|
await _searchIndex.RemoveItems(ids); |
|
_searchIndex.Commit(); |
|
await _libraryRepository.DeleteLocalPath(libraryPath.Id); |
|
return Unit.Default; |
|
} |
|
|
|
private async Task<Validation<BaseError, LibraryPath>> MediaSourceMustExist(DeleteLocalLibraryPath request) => |
|
(await _libraryRepository.GetPath(request.LocalLibraryPathId)) |
|
.HeadOrNone() |
|
.ToValidation<BaseError>( |
|
$"Local library path {request.LocalLibraryPathId} does not exist."); |
|
} |
|
}
|
|
|