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.
59 lines
2.0 KiB
59 lines
2.0 KiB
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Core.Interfaces.Locking; |
|
using ErsatzTV.Core.Interfaces.Metadata; |
|
using ErsatzTV.Core.Interfaces.Repositories.Caching; |
|
using ErsatzTV.Core.Interfaces.Search; |
|
using ErsatzTV.Core.Interfaces.Trakt; |
|
using ErsatzTV.Infrastructure.Data; |
|
using Microsoft.EntityFrameworkCore; |
|
using Microsoft.Extensions.Logging; |
|
|
|
namespace ErsatzTV.Application.MediaCollections; |
|
|
|
public class MatchTraktListItemsHandler : TraktCommandBase, |
|
IRequestHandler<MatchTraktListItems, Either<BaseError, Unit>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
private readonly IEntityLocker _entityLocker; |
|
|
|
public MatchTraktListItemsHandler( |
|
ITraktApiClient traktApiClient, |
|
ICachingSearchRepository searchRepository, |
|
ISearchIndex searchIndex, |
|
IFallbackMetadataProvider fallbackMetadataProvider, |
|
IDbContextFactory<TvContext> dbContextFactory, |
|
ILogger<MatchTraktListItemsHandler> logger, |
|
IEntityLocker entityLocker) : base( |
|
traktApiClient, |
|
searchRepository, |
|
searchIndex, |
|
fallbackMetadataProvider, |
|
logger) |
|
{ |
|
_dbContextFactory = dbContextFactory; |
|
_entityLocker = entityLocker; |
|
} |
|
|
|
public async Task<Either<BaseError, Unit>> Handle( |
|
MatchTraktListItems request, |
|
CancellationToken cancellationToken) |
|
{ |
|
try |
|
{ |
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
Validation<BaseError, TraktList> validation = await TraktListMustExist(dbContext, request.TraktListId); |
|
return await validation.Match( |
|
async l => await MatchListItems(dbContext, l).MapT(_ => Unit.Default), |
|
error => Task.FromResult<Either<BaseError, Unit>>(error.Join())); |
|
} |
|
finally |
|
{ |
|
if (request.Unlock) |
|
{ |
|
_entityLocker.UnlockTrakt(); |
|
} |
|
} |
|
} |
|
}
|
|
|