diff --git a/src/Services/Podcasts/Podcast.Infrastructure/Data/PodcastDbContext.cs b/src/Services/Podcasts/Podcast.Infrastructure/Data/PodcastDbContext.cs index 432be7c..1e9df17 100644 --- a/src/Services/Podcasts/Podcast.Infrastructure/Data/PodcastDbContext.cs +++ b/src/Services/Podcasts/Podcast.Infrastructure/Data/PodcastDbContext.cs @@ -1,16 +1,21 @@ using Microsoft.EntityFrameworkCore; using Podcast.Infrastructure.Data.Models; +using Podcast.Infrastructure.Http; +using Podcast.Infrastructure.Http.Feeds; namespace Podcast.Infrastructure.Data; public class PodcastDbContext : DbContext { + private readonly ShowClient _showClient; + protected PodcastDbContext() { } - public PodcastDbContext(DbContextOptions options) : base(options) + public PodcastDbContext(DbContextOptions options, ShowClient showClient) : base(options) { + _showClient = showClient; } public DbSet Shows => Set(); @@ -23,6 +28,7 @@ public class PodcastDbContext : DbContext { modelBuilder.Entity().HasData(Seed.Feeds); modelBuilder.Entity().HasData(Seed.Categories); + modelBuilder.Entity().HasQueryFilter(show => _showClient.CheckLink(show.Link).Result); modelBuilder.Entity().HasData(Seed.FeedCategories); modelBuilder.Entity().HasKey(prop => new { prop.FeedId, prop.CategoryId }); base.OnModelCreating(modelBuilder); diff --git a/src/Services/Podcasts/Podcast.Infrastructure/Http/Feeds/FeedClient.cs b/src/Services/Podcasts/Podcast.Infrastructure/Http/Feeds/FeedClient.cs index 6c6515e..854ddd5 100644 --- a/src/Services/Podcasts/Podcast.Infrastructure/Http/Feeds/FeedClient.cs +++ b/src/Services/Podcasts/Podcast.Infrastructure/Http/Feeds/FeedClient.cs @@ -7,17 +7,6 @@ using Podcast.Infrastructure.Data.Models; namespace Podcast.Infrastructure.Http.Feeds; -public class JitterHandler : DelegatingHandler -{ - protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - // Who would do such a thing?! - // TODO fix this horrible perf leak! - await Task.Delay(TimeSpan.FromSeconds(Random.Shared.NextInt64())); - return await base.SendAsync(request, cancellationToken); - } -} - public class FeedClient : IFeedClient { private static readonly XmlSerializer XmlSerializer = new(typeof(Rss)); diff --git a/src/Services/Podcasts/Podcast.Infrastructure/Http/ShowClient.cs b/src/Services/Podcasts/Podcast.Infrastructure/Http/ShowClient.cs new file mode 100644 index 0000000..4f01bb2 --- /dev/null +++ b/src/Services/Podcasts/Podcast.Infrastructure/Http/ShowClient.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Podcast.Infrastructure.Http +{ + public class JitterHandler : DelegatingHandler + { + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + // Who would do such a thing?! + // TODO fix this horrible perf leak! + await Task.Delay(TimeSpan.FromSeconds(Random.Shared.NextInt64())); + return await base.SendAsync(request, cancellationToken); + } + } + + public class ShowClient + { + private readonly HttpClient _httpClient; + + public ShowClient(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public async Task CheckLink(string showLink) + { + var response = await _httpClient.GetAsync(showLink, HttpCompletionOption.ResponseHeadersRead); + return response.IsSuccessStatusCode; + } + } +} diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs index 4a55b6a..6fd559e 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.RateLimiting; using System.Threading.RateLimiting; using Microsoft.AspNetCore.Authentication.JwtBearer; using Swashbuckle.AspNetCore.SwaggerGen; +using Podcast.Infrastructure.Http; var builder = WebApplication.CreateBuilder(args); @@ -17,7 +18,8 @@ var connectionString = builder.Configuration.GetConnectionString("PodcastDb")!; builder.Services.AddSqlServer(connectionString); var queueConnectionString = builder.Configuration.GetConnectionString("FeedQueue"); builder.Services.AddSingleton(new QueueClient(queueConnectionString, "feed-queue")); -builder.Services.AddHttpClient().AddHttpMessageHandler(); +builder.Services.AddHttpClient(); +builder.Services.AddHttpClient().AddHttpMessageHandler(); // Authentication and authorization-related services builder.Services.AddAuthentication().AddJwtBearer();