|
|
|
@ -1,5 +1,7 @@ |
|
|
|
using ErsatzTV.Core; |
|
|
|
using ErsatzTV.Core; |
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
|
|
|
|
using ErsatzTV.Core.Interfaces.Metadata; |
|
|
|
|
|
|
|
using ErsatzTV.Core.Interfaces.Repositories; |
|
|
|
using ErsatzTV.Core.Interfaces.Search; |
|
|
|
using ErsatzTV.Core.Interfaces.Search; |
|
|
|
using ErsatzTV.Infrastructure.Data; |
|
|
|
using ErsatzTV.Infrastructure.Data; |
|
|
|
using ErsatzTV.Infrastructure.Extensions; |
|
|
|
using ErsatzTV.Infrastructure.Extensions; |
|
|
|
@ -7,31 +9,38 @@ using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
|
|
|
|
|
|
namespace ErsatzTV.Application.MediaCollections; |
|
|
|
namespace ErsatzTV.Application.MediaCollections; |
|
|
|
|
|
|
|
|
|
|
|
public class DeleteCollectionHandler : IRequestHandler<DeleteCollection, Either<BaseError, Unit>> |
|
|
|
public class DeleteCollectionHandler( |
|
|
|
|
|
|
|
IDbContextFactory<TvContext> dbContextFactory, |
|
|
|
|
|
|
|
ISearchTargets searchTargets, |
|
|
|
|
|
|
|
IMediaCollectionRepository mediaCollectionRepository, |
|
|
|
|
|
|
|
ISearchRepository searchRepository, |
|
|
|
|
|
|
|
IFallbackMetadataProvider fallbackMetadataProvider, |
|
|
|
|
|
|
|
ILanguageCodeService languageCodeService, |
|
|
|
|
|
|
|
ISearchIndex searchIndex) |
|
|
|
|
|
|
|
: IRequestHandler<DeleteCollection, Either<BaseError, Unit>> |
|
|
|
{ |
|
|
|
{ |
|
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
|
|
|
|
|
|
private readonly ISearchTargets _searchTargets; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DeleteCollectionHandler(IDbContextFactory<TvContext> dbContextFactory, ISearchTargets searchTargets) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_dbContextFactory = dbContextFactory; |
|
|
|
|
|
|
|
_searchTargets = searchTargets; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Either<BaseError, Unit>> Handle( |
|
|
|
public async Task<Either<BaseError, Unit>> Handle( |
|
|
|
DeleteCollection request, |
|
|
|
DeleteCollection request, |
|
|
|
CancellationToken cancellationToken) |
|
|
|
CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
Validation<BaseError, Collection> validation = await CollectionMustExist(dbContext, request, cancellationToken); |
|
|
|
Validation<BaseError, Collection> validation = await CollectionMustExist(dbContext, request, cancellationToken); |
|
|
|
return await validation.Apply(c => DoDeletion(dbContext, c, cancellationToken)); |
|
|
|
return await validation.Apply(c => DoDeletion(dbContext, c, cancellationToken)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task<Unit> DoDeletion(TvContext dbContext, Collection collection, CancellationToken cancellationToken) |
|
|
|
private async Task<Unit> DoDeletion(TvContext dbContext, Collection collection, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
var itemIds = (await mediaCollectionRepository.GetItems(collection.Id)).Map(i => i.Id).ToList(); |
|
|
|
dbContext.Collections.Remove(collection); |
|
|
|
dbContext.Collections.Remove(collection); |
|
|
|
await dbContext.SaveChangesAsync(cancellationToken); |
|
|
|
await dbContext.SaveChangesAsync(cancellationToken); |
|
|
|
_searchTargets.SearchTargetsChanged(); |
|
|
|
await searchIndex.RebuildItems( |
|
|
|
|
|
|
|
searchRepository, |
|
|
|
|
|
|
|
fallbackMetadataProvider, |
|
|
|
|
|
|
|
languageCodeService, |
|
|
|
|
|
|
|
itemIds, |
|
|
|
|
|
|
|
cancellationToken); |
|
|
|
|
|
|
|
searchIndex.Commit(); |
|
|
|
|
|
|
|
searchTargets.SearchTargetsChanged(); |
|
|
|
return Unit.Default; |
|
|
|
return Unit.Default; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|