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; }
|
|
|
|
|
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
|
|
|
|
|
|
|
modelBuilder.Entity<Watcher>()
|
|
|
|
.HasOne(w => w.User)
|
|
|
|
.WithMany(u => u.Watchers)
|
|
|
|
.HasForeignKey(w => w.UserId);
|
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))
|
|
|
|
{
|
|
|
|
Logger.LogWarning($"Trying to create more than one player watcher for user [{userId}]");
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|