2021-10-23 22:16:37 +01:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2021-11-10 09:15:39 +00:00
|
|
|
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;
|
2021-11-10 09:15:39 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
2021-10-23 22:16:37 +01:00
|
|
|
{
|
2021-11-10 09:15:39 +00:00
|
|
|
private readonly ILogger<ApplicationDbContext> Logger;
|
|
|
|
|
2021-10-23 22:16:37 +01:00
|
|
|
public DbSet<Watcher> Watcher { get; set; }
|
2022-02-16 23:38:45 +00:00
|
|
|
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
|
|
|
|
2022-10-07 18:29:33 +01:00
|
|
|
public DbSet<SpotifyListen> SpotifyListen { get; set; }
|
|
|
|
|
2021-11-10 09:15:39 +00:00
|
|
|
public ApplicationDbContext(
|
|
|
|
DbContextOptions<ApplicationDbContext> options,
|
|
|
|
ILogger<ApplicationDbContext> logger
|
|
|
|
) : base(options)
|
2021-10-23 22:16:37 +01:00
|
|
|
{
|
2021-11-10 09:15:39 +00:00
|
|
|
Logger = logger;
|
2021-10-23 22:16:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
{
|
2021-10-24 22:40:15 +01:00
|
|
|
|
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);
|
2021-10-24 22:40:15 +01:00
|
|
|
|
2022-02-16 23:38:45 +00:00
|
|
|
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");
|
|
|
|
|
2021-10-24 22:40:15 +01:00
|
|
|
modelBuilder.Entity<Watcher>()
|
|
|
|
.HasOne(w => w.User)
|
|
|
|
.WithMany(u => u.Watchers)
|
|
|
|
.HasForeignKey(w => w.UserId);
|
2021-10-26 19:26:41 +01:00
|
|
|
|
2022-02-16 23:38:45 +00:00
|
|
|
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 });
|
2022-02-16 23:38:45 +00:00
|
|
|
|
2022-02-24 22:35:39 +00:00
|
|
|
modelBuilder.Entity<TrackLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
|
2022-02-16 23:38:45 +00:00
|
|
|
modelBuilder.Entity<TrackLastfmSpotifyMapping>()
|
|
|
|
.Property(s => s.LastfmTrackName)
|
|
|
|
.UseCollation("case_insensitive");
|
|
|
|
modelBuilder.Entity<TrackLastfmSpotifyMapping>()
|
|
|
|
.Property(s => s.LastfmArtistName)
|
|
|
|
.UseCollation("case_insensitive");
|
|
|
|
|
2022-02-24 22:35:39 +00:00
|
|
|
modelBuilder.Entity<AlbumLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
|
2022-02-16 23:38:45 +00:00
|
|
|
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);
|
2022-10-07 18:29:33 +01:00
|
|
|
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)
|
2022-02-16 23:38:45 +00:00
|
|
|
.UseCollation("case_insensitive");
|
2022-11-05 22:39:07 +00:00
|
|
|
//modelBuilder.Entity<SpotifyListen>()
|
|
|
|
// .HasIndex(x => new { x.UserId, x.ArtistName, x.TrackName });
|
2022-02-16 23:38:45 +00:00
|
|
|
|
2021-10-26 19:26:41 +01:00
|
|
|
SeedData.Seed(modelBuilder);
|
2021-10-23 22:16:37 +01:00
|
|
|
}
|
2021-11-10 09:15:39 +00: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);
|
2021-11-10 09:15:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Watcher.Add(new Watcher {
|
|
|
|
UserId = userId,
|
|
|
|
Type = WatcherType.Player
|
|
|
|
});
|
|
|
|
|
|
|
|
SaveChanges();
|
|
|
|
}
|
2021-10-23 22:16:37 +01:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
|
2021-10-23 22:16:37 +01:00
|
|
|
{
|
2021-12-04 12:49:09 +00:00
|
|
|
private static string GetPath(string env) => $"{@Directory.GetCurrentDirectory()}/../Selector.Web/appsettings.{env}.json";
|
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
public ApplicationDbContext CreateDbContext(string[] args)
|
2021-10-23 22:16:37 +01:00
|
|
|
{
|
2021-12-04 12:49:09 +00:00
|
|
|
string configFile;
|
|
|
|
|
|
|
|
if(File.Exists(GetPath("Development")))
|
|
|
|
{
|
|
|
|
configFile = GetPath("Development");
|
|
|
|
}
|
2021-12-07 23:31:17 +00:00
|
|
|
else if(File.Exists(GetPath("Production")))
|
2021-12-04 12:49:09 +00:00
|
|
|
{
|
2021-12-07 23:31:17 +00:00
|
|
|
configFile = GetPath("Production");
|
2021-12-04 12:49:09 +00:00
|
|
|
}
|
|
|
|
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())
|
2021-12-04 12:49:09 +00:00
|
|
|
.AddJsonFile(configFile)
|
2021-10-23 22:16:37 +01:00
|
|
|
.Build();
|
|
|
|
|
2021-10-24 20:15:09 +01:00
|
|
|
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
2021-10-23 22:16:37 +01:00
|
|
|
builder.UseNpgsql(configuration.GetConnectionString("Default"));
|
|
|
|
|
2021-11-10 09:15:39 +00:00
|
|
|
return new ApplicationDbContext(builder.Options, NullLogger<ApplicationDbContext>.Instance);
|
2021-10-23 22:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|