Added MidrandBooks project

This commit is contained in:
Khwezi Mngoma
2026-05-24 13:38:47 +02:00
parent 70c6e0bfbc
commit f67f5eaf53
17 changed files with 241 additions and 46 deletions
@@ -1,35 +0,0 @@
using LiteCharms.Features.HealthChecks;
using static LiteCharms.Features.Extensions.Postgres;
namespace LiteCharms.Features.Extensions;
public static class HealthChecks
{
public static IServiceCollection AddMidrandShopQuartzHealthCheck(this IServiceCollection services)
{
services.AddHealthChecks().AddCheck<MidrandShopQuartzHealthCheck>("MidrandShopQuartz");
return services;
}
public static IServiceCollection AddMidrandShopPostgresHealthCheck(this IServiceCollection services)
{
services.AddHealthChecks().AddCheck<PostgresMidrandShopHealthCheck>(MidrandShopDbConfigName);
return services;
}
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, IConfiguration configuration)
{
services.AddHealthChecks()
.AddCheck("Self", () => HealthCheckResult.Healthy());
//services.AddHealthChecksUI(setup =>
//{
// setup.AddHealthCheckEndpoint("Lead Generator", $"{configuration["ASPNETCORE_URLS"]}/health");
// setup.SetEvaluationTimeInSeconds(15);
//}).AddInMemoryStorage(databaseName: "healthuidb");
return services;
}
}
+1 -12
View File
@@ -1,17 +1,6 @@
using LiteCharms.Features.MidrandShop.Postgres;
namespace LiteCharms.Features.Extensions;
namespace LiteCharms.Features.Extensions;
public static class Postgres
{
public const string MidrandShopDbConfigName = "PostgresMidrandShop";
public const string SchedulerDbConfigName = "PostgresScheduler";
public static IServiceCollection AddMidrandShopDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddPooledDbContextFactory<MidrandShopDbContext>(options =>
options.UseNpgsql(configuration.GetConnectionString(MidrandShopDbConfigName)));
return services;
}
}
+2 -2
View File
@@ -6,8 +6,8 @@ namespace LiteCharms.Features.Extensions;
public static class Quartz
{
public const string ShopSchedulerName = "shop";
public const string MidrandShopSchedulerName = "midrandshop";
public const string TechShopSchedulerName = "tech-shop";
public const string MidrandShopSchedulerName = "midrand-shop";
public static IServiceCollection AddQuartzSchedulerClient(this IServiceCollection services, string schedulerName, IConfiguration configuration)
{
@@ -1,28 +0,0 @@
using static LiteCharms.Features.Extensions.Quartz;
namespace LiteCharms.Features.HealthChecks;
public class MidrandShopQuartzHealthCheck(ISchedulerFactory schedulerFactory) : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var scheduler = await schedulerFactory.GetScheduler(MidrandShopSchedulerName, cancellationToken);
if(scheduler == null)
return HealthCheckResult.Unhealthy($"Scheduler with name '{MidrandShopSchedulerName}' not found.");
if (!scheduler.IsStarted)
return HealthCheckResult.Unhealthy($"{MidrandShopSchedulerName} Quartz scheduler is not running");
await scheduler.CheckExists(new JobKey(Guid.NewGuid().ToString()), cancellationToken);
return HealthCheckResult.Healthy($"{MidrandShopSchedulerName} Quartz scheduler is ready");
}
catch (SchedulerException)
{
return HealthCheckResult.Unhealthy($"{MidrandShopSchedulerName} Quartz scheduler cannot connect to the store");
}
}
}
@@ -1,28 +0,0 @@
using static LiteCharms.Features.Extensions.Postgres;
namespace LiteCharms.Features.HealthChecks;
public class PostgresMidrandShopHealthCheck(IConfiguration configuration) : IHealthCheck
{
private readonly string connectionString = configuration.GetConnectionString(MidrandShopDbConfigName)!;
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.CommandText = "SELECT 1";
await command.ExecuteScalarAsync(cancellationToken);
return HealthCheckResult.Healthy($"{MidrandShopDbConfigName} is responsive.");
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy($"{MidrandShopDbConfigName} is unreachable.", ex);
}
}
}
@@ -1,6 +0,0 @@
namespace LiteCharms.Features.MidrandShop.Postgres;
public class MidrandShopDbContext(DbContextOptions<MidrandShopDbContext> options) : DbContext(options)
{
}
@@ -1,21 +0,0 @@
using static LiteCharms.Features.Extensions.Postgres;
namespace LiteCharms.Features.MidrandShop.Postgres;
public class MidrandShopDbContextFactory : IDesignTimeDbContextFactory<MidrandShopDbContext>
{
public MidrandShopDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddUserSecrets(typeof(MidrandShopDbContext).Assembly)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var optionsBuilder = new DbContextOptionsBuilder<MidrandShopDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString(MidrandShopDbConfigName));
return new MidrandShopDbContext(optionsBuilder.Options);
}
}