Selector/Selector.Model/ApplicationDbContext.cs

162 lines
6.1 KiB
C#
Raw Normal View History

2021-10-23 22:16:37 +01:00
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2021-10-23 22:16:37 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
2021-10-24 00:23:45 +01:00
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
2021-10-24 00:23:45 +01:00
2021-10-23 22:16:37 +01:00
namespace Selector.Model
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
2021-10-23 22:16:37 +01:00
{
private readonly ILogger<ApplicationDbContext> Logger;
2021-10-23 22:16:37 +01:00
public DbSet<Watcher> Watcher { get; set; }
public DbSet<UserScrobble> Scrobble { get; set; }
public DbSet<TrackLastfmSpotifyMapping> TrackMapping { get; set; }
public DbSet<AlbumLastfmSpotifyMapping> AlbumMapping { get; set; }
public DbSet<ArtistLastfmSpotifyMapping> ArtistMapping { get; set; }
2021-10-23 22:16:37 +01:00
public DbSet<SpotifyListen> SpotifyListen { get; set; }
public ApplicationDbContext(
DbContextOptions<ApplicationDbContext> options,
ILogger<ApplicationDbContext> logger
) : base(options)
2021-10-23 22:16:37 +01:00
{
Logger = logger;
2021-10-23 22:16:37 +01:00
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
2021-10-23 22:16:37 +01:00
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
2021-10-24 00:23:45 +01:00
base.OnModelCreating(modelBuilder);
modelBuilder.HasCollation("case_insensitive", locale: "en-u-ks-primary", provider: "icu", deterministic: false);
modelBuilder.Entity<ApplicationUser>()
.Property(u => u.SpotifyIsLinked)
.IsRequired();
modelBuilder.Entity<ApplicationUser>()
.Property(u => u.LastFmUsername)
.UseCollation("case_insensitive");
modelBuilder.Entity<Watcher>()
.HasOne(w => w.User)
.WithMany(u => u.Watchers)
.HasForeignKey(w => w.UserId);
modelBuilder.Entity<UserScrobble>()
.HasOne(w => w.User)
.WithMany(u => u.Scrobbles)
.HasForeignKey(w => w.UserId);
modelBuilder.Entity<UserScrobble>()
.Property(s => s.TrackName)
.UseCollation("case_insensitive");
modelBuilder.Entity<UserScrobble>()
.Property(s => s.AlbumName)
.UseCollation("case_insensitive");
modelBuilder.Entity<UserScrobble>()
.Property(s => s.ArtistName)
.UseCollation("case_insensitive");
2022-11-05 22:39:07 +00:00
//modelBuilder.Entity<UserScrobble>()
// .HasIndex(x => new { x.UserId, x.ArtistName, x.TrackName });
modelBuilder.Entity<TrackLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
modelBuilder.Entity<TrackLastfmSpotifyMapping>()
.Property(s => s.LastfmTrackName)
.UseCollation("case_insensitive");
modelBuilder.Entity<TrackLastfmSpotifyMapping>()
.Property(s => s.LastfmArtistName)
.UseCollation("case_insensitive");
modelBuilder.Entity<AlbumLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
modelBuilder.Entity<AlbumLastfmSpotifyMapping>()
.Property(s => s.LastfmAlbumName)
.UseCollation("case_insensitive");
modelBuilder.Entity<AlbumLastfmSpotifyMapping>()
.Property(s => s.LastfmArtistName)
.UseCollation("case_insensitive");
2022-10-07 23:33:54 +01:00
modelBuilder.Entity<ArtistLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
modelBuilder.Entity<ArtistLastfmSpotifyMapping>()
.Property(s => s.LastfmArtistName)
.UseCollation("case_insensitive");
modelBuilder.Entity<SpotifyListen>().HasKey(s => s.Id);
modelBuilder.Entity<SpotifyListen>()
.Property(s => s.TrackName)
.UseCollation("case_insensitive");
modelBuilder.Entity<SpotifyListen>()
.Property(s => s.AlbumName)
.UseCollation("case_insensitive");
modelBuilder.Entity<SpotifyListen>()
.Property(s => s.ArtistName)
.UseCollation("case_insensitive");
2022-11-05 22:39:07 +00:00
//modelBuilder.Entity<SpotifyListen>()
// .HasIndex(x => new { x.UserId, x.ArtistName, x.TrackName });
SeedData.Seed(modelBuilder);
2021-10-23 22:16:37 +01:00
}
public void CreatePlayerWatcher(string userId)
{
if(Watcher.Any(w => w.UserId == userId && w.Type == WatcherType.Player))
{
2022-07-01 22:04:17 +01:00
Logger.LogWarning("Trying to create more than one player watcher for user [{id}]", userId);
return;
}
Watcher.Add(new Watcher {
UserId = userId,
Type = WatcherType.Player
});
SaveChanges();
}
2021-10-23 22:16:37 +01:00
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
2021-10-23 22:16:37 +01:00
{
private static string GetPath(string env) => $"{@Directory.GetCurrentDirectory()}/../Selector.Web/appsettings.{env}.json";
public ApplicationDbContext CreateDbContext(string[] args)
2021-10-23 22:16:37 +01:00
{
string configFile;
if(File.Exists(GetPath("Development")))
{
configFile = GetPath("Development");
}
else if(File.Exists(GetPath("Production")))
{
configFile = GetPath("Production");
}
else
{
throw new FileNotFoundException("No config file available to load a connection string from");
}
2021-10-23 22:16:37 +01:00
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(configFile)
2021-10-23 22:16:37 +01:00
.Build();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
2021-10-23 22:16:37 +01:00
builder.UseNpgsql(configuration.GetConnectionString("Default"));
return new ApplicationDbContext(builder.Options, NullLogger<ApplicationDbContext>.Instance);
2021-10-23 22:16:37 +01:00
}
}
}