Files
Finlytic/FinlyticNews/Database/NewsDbContext.cs
T

106 lines
3.7 KiB
C#

using FinlyticCore.Database;
using FinlyticCore.Entities.Settings;
using FinlyticNews.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace FinlyticNews.Database;
/// <summary>
/// Entity Framework Core database context for the news microservice,
/// managing article sources, processed news, and matched assets.
/// </summary>
public class NewsDbContext : DbContext, ISettingsDbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="NewsDbContext"/> class.
/// </summary>
/// <param name="options">The context configuration options.</param>
public NewsDbContext(DbContextOptions<NewsDbContext> options) : base(options)
{
}
/// <summary>
/// Gets or sets the database set for dynamic settings.
/// </summary>
public DbSet<SettingEntity> DynamicSettings => Set<SettingEntity>();
/// <summary>
/// Gets or sets the database set for configured article sources.
/// </summary>
public DbSet<ArticleSourceEntity> ArticleSources => Set<ArticleSourceEntity>();
/// <summary>
/// Gets or sets the database set for news articles.
/// </summary>
public DbSet<NewsArticleEntity> NewsArticles => Set<NewsArticleEntity>();
/// <summary>
/// Gets or sets the database set for matched assets.
/// </summary>
public DbSet<MatchedAssetEntity> MatchedAssets => Set<MatchedAssetEntity>();
/// <summary>
/// Gets or sets the database set for blocked news URLs (unindexable or duplicate articles).
/// </summary>
public DbSet<BlockedNewsUrlEntity> BlockedNewsUrls => Set<BlockedNewsUrlEntity>();
/// <summary>
/// Configures the model mapping, database constraints, and unique indices.
/// </summary>
/// <param name="modelBuilder">The builder being used to construct the database schema model.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<SettingEntity>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.Key).IsUnique();
});
// Configure unique index on SourceUrl for deduplication check (idempotency)
modelBuilder.Entity<NewsArticleEntity>()
.HasIndex(a => a.SourceUrl)
.IsUnique();
// Performance & deduplication indexes for news queries
modelBuilder.Entity<NewsArticleEntity>()
.HasIndex(a => new { a.PublishedAt, a.ScrapedAt });
modelBuilder.Entity<NewsArticleEntity>()
.HasIndex(a => a.Status);
modelBuilder.Entity<NewsArticleEntity>()
.HasIndex(a => a.TitleHash);
modelBuilder.Entity<NewsArticleEntity>()
.HasIndex(a => a.SimHash);
modelBuilder.Entity<MatchedAssetEntity>()
.HasIndex(m => m.Isin);
// Blocked URLs index
modelBuilder.Entity<BlockedNewsUrlEntity>()
.HasIndex(b => b.Url)
.IsUnique();
// Configure relationship between NewsArticle and MatchedAssets
modelBuilder.Entity<MatchedAssetEntity>()
.HasOne(m => m.NewsArticle)
.WithMany(a => a.MatchedAssets)
.HasForeignKey(m => m.NewsArticleId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public class NewsDbContextFactory : IDesignTimeDbContextFactory<NewsDbContext>
{
public NewsDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<NewsDbContext>();
optionsBuilder.UseNpgsql("Host=localhost;Database=news;Username=postgres;Password=postgres");
return new NewsDbContext(optionsBuilder.Options);
}
}