2022-02-18 00:08:42 +00:00
|
|
|
|
using IF.Lastfm.Core.Api;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2022-02-18 00:08:42 +00:00
|
|
|
|
using Selector.Model;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
using SpotifyAPI.Web;
|
2022-10-10 11:47:50 +01:00
|
|
|
|
using StackExchange.Redis;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
using System.Linq;
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
|
|
|
|
namespace Selector.CLI.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class CommandContextExtensions
|
|
|
|
|
{
|
|
|
|
|
public static CommandContext WithConfig(this CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
var configBuild = new ConfigurationBuilder();
|
|
|
|
|
configBuild.AddJsonFile("appsettings.json", optional: true)
|
|
|
|
|
.AddJsonFile("appsettings.Development.json", optional: true)
|
|
|
|
|
.AddJsonFile("appsettings.Production.json", optional: true);
|
|
|
|
|
context.Config = configBuild.Build().ConfigureOptions();
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommandContext WithLogger(this CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
context.Logger = LoggerFactory.Create(builder =>
|
|
|
|
|
{
|
|
|
|
|
//builder.AddConsole(a => a.);
|
|
|
|
|
builder.AddSimpleConsole(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SingleLine = true;
|
|
|
|
|
});
|
|
|
|
|
builder.SetMinimumLevel(LogLevel.Trace);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 22:13:16 +00:00
|
|
|
|
public static CommandContext WithDb(this CommandContext context, string connectionString = null)
|
2022-02-18 00:08:42 +00:00
|
|
|
|
{
|
|
|
|
|
if (context.Config is null)
|
|
|
|
|
{
|
|
|
|
|
context.WithConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.DatabaseConfig = new DbContextOptionsBuilder<ApplicationDbContext>();
|
2022-02-18 22:13:16 +00:00
|
|
|
|
context.DatabaseConfig.UseNpgsql(string.IsNullOrWhiteSpace(connectionString) ? context.Config.DatabaseOptions.ConnectionString : connectionString);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommandContext WithLastfmApi(this CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Config is null)
|
|
|
|
|
{
|
|
|
|
|
context.WithConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.LastFmClient = new LastfmClient(new LastAuth(context.Config.LastfmClient, context.Config.LastfmSecret));
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
2022-02-24 00:27:34 +00:00
|
|
|
|
|
|
|
|
|
public static CommandContext WithSpotify(this CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Config is null)
|
|
|
|
|
{
|
|
|
|
|
context.WithConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var refreshToken = context.Config.RefreshToken;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(refreshToken))
|
|
|
|
|
{
|
|
|
|
|
if (context.DatabaseConfig is null)
|
|
|
|
|
{
|
|
|
|
|
context.WithDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var db = new ApplicationDbContext(context.DatabaseConfig.Options, NullLogger<ApplicationDbContext>.Instance);
|
|
|
|
|
|
2022-03-03 22:32:29 +00:00
|
|
|
|
var user = db.Users.FirstOrDefault(u => u.UserName == "sarsoo");
|
|
|
|
|
|
|
|
|
|
refreshToken = user?.SpotifyRefreshToken;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var configFactory = new RefreshTokenFactory(context.Config.ClientId, context.Config.ClientSecret, refreshToken);
|
|
|
|
|
|
|
|
|
|
context.Spotify = new SpotifyClient(configFactory.GetConfig().Result);
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
2022-10-10 11:47:50 +01:00
|
|
|
|
|
|
|
|
|
public static CommandContext WithRedis(this CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Config is null)
|
|
|
|
|
{
|
|
|
|
|
context.WithConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connectionString = context.Config.RedisOptions.ConnectionString;
|
|
|
|
|
|
|
|
|
|
var connMulti = ConnectionMultiplexer.Connect(connectionString);
|
|
|
|
|
|
|
|
|
|
context.RedisMux = connMulti;
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|