Пример #1
0
    private async Task <Either <BaseError, MediaItemScanResult <PlexEpisode> > > UpdateMetadata(
        MediaItemScanResult <PlexEpisode> result,
        PlexEpisode incoming)
    {
        PlexEpisode existing = result.Item;

        var toUpdate = existing.EpisodeMetadata
                       .Where(em => incoming.EpisodeMetadata.Any(em2 => em2.EpisodeNumber == em.EpisodeNumber))
                       .ToList();
        var toRemove = existing.EpisodeMetadata.Except(toUpdate).ToList();
        var toAdd    = incoming.EpisodeMetadata
                       .Where(em => existing.EpisodeMetadata.All(em2 => em2.EpisodeNumber != em.EpisodeNumber))
                       .ToList();

        foreach (EpisodeMetadata metadata in toRemove)
        {
            await _televisionRepository.RemoveMetadata(existing, metadata);
        }

        foreach (EpisodeMetadata metadata in toAdd)
        {
            metadata.EpisodeId = existing.Id;
            metadata.Episode   = existing;
            existing.EpisodeMetadata.Add(metadata);

            await _metadataRepository.Add(metadata);
        }

        // TODO: update existing metadata

        return(result);
    }