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.
Readarr/src/NzbDrone.Core/Books/Repositories/AuthorMetadataRepository.cs

70 lines
2.3 KiB

using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Books
{
public interface IAuthorMetadataRepository : IBasicRepository<AuthorMetadata>
{
List<AuthorMetadata> FindById(List<string> foreignIds);
bool UpsertMany(List<AuthorMetadata> data);
}
public class AuthorMetadataRepository : BasicRepository<AuthorMetadata>, IAuthorMetadataRepository
{
private readonly Logger _logger;
public AuthorMetadataRepository(IMainDatabase database, IEventAggregator eventAggregator, Logger logger)
: base(database, eventAggregator)
{
_logger = logger;
}
public List<AuthorMetadata> FindById(List<string> foreignIds)
{
return Query(x => Enumerable.Contains(foreignIds, x.ForeignAuthorId));
}
public bool UpsertMany(List<AuthorMetadata> data)
{
var existingMetadata = FindById(data.Select(x => x.ForeignAuthorId).ToList());
var updateMetadataList = new List<AuthorMetadata>();
var addMetadataList = new List<AuthorMetadata>();
var upToDateMetadataCount = 0;
foreach (var meta in data)
{
var existing = existingMetadata.SingleOrDefault(x => x.ForeignAuthorId == meta.ForeignAuthorId);
if (existing != null)
{
// populate Id in remote data
meta.UseDbFieldsFrom(existing);
// responses vary, so try adding remote to what we have
if (!meta.Equals(existing))
{
updateMetadataList.Add(meta);
}
else
{
upToDateMetadataCount++;
}
}
else
{
addMetadataList.Add(meta);
}
}
UpdateMany(updateMetadataList);
InsertMany(addMetadataList);
_logger.Debug($"{upToDateMetadataCount} author metadata up to date; Updating {updateMetadataList.Count}, Adding {addMetadataList.Count} author metadata entries.");
return updateMetadataList.Count > 0 || addMetadataList.Count > 0;
}
}
}