示例#1
0
    public async Task HandleUpdateAsync(CancellationToken cancellationToken)
    {
        var feeds = await _podcastDbContext.Feeds.Include(x => x.Show !.Episodes).ToListAsync(cancellationToken);

        foreach (var feed in feeds)
        {
            _logger.LogInformation("Updating feed: {url}", feed.Url);

            try
            {
                var show = await _feedClient.GetShowAsync(feed, cancellationToken);

                if (!show.Episodes.Any())
                {
                    continue;
                }

                if (feed.Show == null)
                {
                    feed.Show = show;
                }
                else
                {
                    var newEpisodes = GetNewEpisodes(feed.Show.Episodes, show.Episodes);
                    newEpisodes.ToList().ForEach(episode => feed.Show.Episodes.Add(episode));
                }

                await _podcastDbContext.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error updating feed: {error}", ex.Message);
            }
        }
    }
    public async Task HandleIngestionAsync(string title, string url, IReadOnlyCollection <string> feedCategories,
                                           CancellationToken stoppingToken)
    {
        var isAcceptedTopic = AcceptedTopics.Any(topic => title.Contains(topic, StringComparison.InvariantCultureIgnoreCase));

        if (!isAcceptedTopic)
        {
            return;
        }

        var isExistingShow = await _podcastDbContext.Shows.AnyAsync(show => show.Title == title, stoppingToken);

        if (isExistingShow)
        {
            return;
        }

        var feed = new Feed(Guid.NewGuid(), url);

        var categories = await _podcastDbContext.Categories
                         .Where(category => feedCategories.Any(feedCategory => feedCategory == category.Genre))
                         .ToListAsync(stoppingToken);

        foreach (var category in categories)
        {
            feed.Categories.Add(new FeedCategory(category.Id, feed.Id));
        }

        try
        {
            var show = await _feedClient.GetShowAsync(feed, stoppingToken);

            feed.Show = show;
        }
        catch (Exception ex)
        {
            _logger.LogError("Error adding feed: {error}", ex.Message);
        }

        await _podcastDbContext.Feeds.AddAsync(feed, stoppingToken);

        await _podcastDbContext.SaveChangesAsync(stoppingToken);
    }