try switch to db

This commit is contained in:
Flaminel
2025-06-15 00:42:20 +03:00
parent 033b50519b
commit 9e393eebcc
63 changed files with 3245 additions and 2703 deletions

View File

@@ -2,14 +2,15 @@ using Common.Configuration;
using Common.Configuration.Arr;
using Common.Configuration.DownloadCleaner;
using Common.Configuration.General;
using Common.Configuration.Notification;
using Common.Configuration.QueueCleaner;
using Infrastructure.Configuration;
using Data;
using Infrastructure.Logging;
using Infrastructure.Models;
using Infrastructure.Services.Interfaces;
using Infrastructure.Verticals.ContentBlocker;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Executable.Controllers;
@@ -18,103 +19,361 @@ namespace Executable.Controllers;
public class ConfigurationController : ControllerBase
{
private readonly ILogger<ConfigurationController> _logger;
private readonly IConfigManager _configManager;
private readonly IJobManagementService _jobManagementService;
private readonly DataContext _dataContext;
private readonly LoggingConfigManager _loggingConfigManager;
private readonly IJobManagementService _jobManagementService;
public ConfigurationController(
ILogger<ConfigurationController> logger,
IConfigManager configManager,
IJobManagementService jobManagementService,
LoggingConfigManager loggingConfigManager
DataContext dataContext,
LoggingConfigManager loggingConfigManager,
IJobManagementService jobManagementService
)
{
_logger = logger;
_configManager = configManager;
_jobManagementService = jobManagementService;
_dataContext = dataContext;
_loggingConfigManager = loggingConfigManager;
_jobManagementService = jobManagementService;
}
[HttpGet("queue_cleaner")]
public async Task<IActionResult> GetQueueCleanerConfig()
{
var config = await _configManager.GetConfigurationAsync<QueueCleanerConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.QueueCleanerConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("download_cleaner")]
public async Task<IActionResult> GetDownloadCleanerConfig()
{
var config = await _configManager.GetConfigurationAsync<DownloadCleanerConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.DownloadCleanerConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("download_client")]
public async Task<IActionResult> GetDownloadClientConfig()
{
var config = await _configManager.GetConfigurationAsync<DownloadClientConfigs>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.DownloadClients
.AsNoTracking()
.ToListAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("general")]
public async Task<IActionResult> GetGeneralConfig()
{
var config = await _configManager.GetConfigurationAsync<GeneralConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.GeneralConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("sonarr")]
public async Task<IActionResult> GetSonarrConfig()
{
var config = await _configManager.GetConfigurationAsync<SonarrConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.SonarrConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("radarr")]
public async Task<IActionResult> GetRadarrConfig()
{
var config = await _configManager.GetConfigurationAsync<RadarrConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.RadarrConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("lidarr")]
public async Task<IActionResult> GetLidarrConfig()
{
var config = await _configManager.GetConfigurationAsync<LidarrConfig>();
return Ok(config);
await DataContext.Lock.WaitAsync();
try
{
var config = await _dataContext.LidarrConfigs
.AsNoTracking()
.FirstAsync();
return Ok(config);
}
finally
{
DataContext.Lock.Release();
}
}
[HttpGet("notifications")]
public async Task<IActionResult> GetNotificationsConfig()
{
var config = await _configManager.GetConfigurationAsync<NotificationsConfig>();
return Ok(config);
// TODO get all notification configs
await DataContext.Lock.WaitAsync();
try
{
// var config = await _dataContext.NotificationsConfigs
// .AsNoTracking()
// .FirstAsync();
// return Ok(config);
return null; // Placeholder for future implementation
}
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("queue_cleaner")]
public async Task<IActionResult> UpdateQueueCleanerConfig([FromBody] QueueCleanerConfig dto)
public async Task<IActionResult> UpdateQueueCleanerConfig([FromBody] QueueCleanerConfig newConfig)
{
// Get existing config
var oldConfig = await _configManager.GetConfigurationAsync<QueueCleanerConfig>();
// Apply updates from DTO, preserving sensitive data if not provided
var newConfig = oldConfig.Adapt<QueueCleanerConfig>();
newConfig = dto.Adapt(newConfig);
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.QueueCleanerConfigs
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
// Update the scheduler based on configuration changes
await UpdateJobSchedule(oldConfig, JobType.QueueCleaner);
return Ok(new { Message = "QueueCleaner configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save QueueCleaner configuration");
return StatusCode(500, "Failed to save QueueCleaner configuration");
}
// Update the scheduler based on configuration changes
await UpdateJobSchedule(oldConfig, JobType.QueueCleaner);
return Ok(new { Message = "QueueCleaner configuration updated successfully" });
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("download_cleaner")]
public async Task<IActionResult> UpdateDownloadCleanerConfig([FromBody] DownloadCleanerConfig newConfig)
{
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.DownloadCleanerConfigs
.Include(x => x.Categories)
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
// Update the scheduler based on configuration changes
await UpdateJobSchedule(oldConfig, JobType.DownloadCleaner);
return Ok(new { Message = "DownloadCleaner configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save DownloadCleaner configuration");
return StatusCode(500, "Failed to save DownloadCleaner configuration");
}
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("general")]
public async Task<IActionResult> UpdateGeneralConfig([FromBody] GeneralConfig newConfig)
{
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.GeneralConfigs
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
// Set the logging level based on the new configuration
_loggingConfigManager.SetLogLevel(newConfig.LogLevel);
return Ok(new { Message = "General configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save General configuration");
return StatusCode(500, "Failed to save General configuration");
}
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("sonarr")]
public async Task<IActionResult> UpdateSonarrConfig([FromBody] SonarrConfig newConfig)
{
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.SonarrConfigs
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
return Ok(new { Message = "Sonarr configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save Sonarr configuration");
return StatusCode(500, "Failed to save Sonarr configuration");
}
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("radarr")]
public async Task<IActionResult> UpdateRadarrConfig([FromBody] RadarrConfig newConfig)
{
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.RadarrConfigs
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
return Ok(new { Message = "Radarr configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save Radarr configuration");
return StatusCode(500, "Failed to save Radarr configuration");
}
finally
{
DataContext.Lock.Release();
}
}
[HttpPut("lidarr")]
public async Task<IActionResult> UpdateLidarrConfig([FromBody] LidarrConfig newConfig)
{
await DataContext.Lock.WaitAsync();
try
{
// Validate the configuration
newConfig.Validate();
// Get existing config
var oldConfig = await _dataContext.LidarrConfigs
.FirstAsync();
// Apply updates from DTO
newConfig.Adapt(oldConfig);
// Persist the configuration
await _dataContext.SaveChangesAsync();
return Ok(new { Message = "Lidarr configuration updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save Lidarr configuration");
return StatusCode(500, "Failed to save Lidarr configuration");
}
finally
{
DataContext.Lock.Release();
}
}
/// <summary>
@@ -130,11 +389,11 @@ public class ConfigurationController : ControllerBase
if (!string.IsNullOrEmpty(config.CronExpression))
{
// If the job is enabled, update its schedule with the configured cron expression
_logger.LogInformation("{name} is enabled, updating job schedule with cron expression: {CronExpression}",
_logger.LogInformation("{name} is enabled, updating job schedule with cron expression: {CronExpression}",
jobType.ToString(), config.CronExpression);
_logger.LogCritical("This is a random test log");
// Create a Quartz job schedule with the cron expression
await _jobManagementService.StartJob(jobType, null, config.CronExpression);
}
@@ -142,149 +401,12 @@ public class ConfigurationController : ControllerBase
{
_logger.LogWarning("{name} is enabled, but no cron expression was found in the configuration", jobType.ToString());
}
return;
}
// If the job is disabled, stop it
_logger.LogInformation("{name} is disabled, stopping the job", jobType.ToString());
await _jobManagementService.StopJob(jobType);
}
[HttpPut("content_blocker")]
public async Task<IActionResult> UpdateContentBlockerConfig([FromBody] ContentBlockerConfig newConfig)
{
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save ContentBlocker configuration");
}
return Ok(new { Message = "ContentBlocker configuration updated successfully" });
}
[HttpPut("download_cleaner")]
public async Task<IActionResult> UpdateDownloadCleanerConfig([FromBody] DownloadCleanerConfig dto)
{
// Get existing config
var oldConfig = await _configManager.GetConfigurationAsync<DownloadCleanerConfig>();
// Apply updates from DTO, preserving sensitive data if not provided
var newConfig = oldConfig.Adapt<DownloadCleanerConfig>();
newConfig = dto.Adapt(newConfig);
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save DownloadCleaner configuration");
}
// Update the scheduler based on configuration changes
await UpdateJobSchedule(oldConfig, JobType.DownloadCleaner);
return Ok(new { Message = "DownloadCleaner configuration updated successfully" });
}
[HttpPut("download_client")]
public async Task<IActionResult> UpdateDownloadClientConfig(DownloadClientConfigs newConfigs)
{
// Validate the configuration
newConfigs.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfigs);
if (!result)
{
return StatusCode(500, "Failed to save DownloadClient configuration");
}
return Ok(new { Message = "DownloadClient configuration updated successfully" });
}
[HttpPut("general")]
public async Task<IActionResult> UpdateGeneralConfig([FromBody] GeneralConfig newConfig)
{
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save General configuration");
}
_loggingConfigManager.SetLogLevel(newConfig.LogLevel);
return Ok(new { Message = "General configuration updated successfully" });
}
[HttpPut("sonarr")]
public async Task<IActionResult> UpdateSonarrConfig([FromBody] SonarrConfig newConfig)
{
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save Sonarr configuration");
}
return Ok(new { Message = "Sonarr configuration updated successfully" });
}
[HttpPut("radarr")]
public async Task<IActionResult> UpdateRadarrConfig([FromBody] RadarrConfig newConfig)
{
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save Radarr configuration");
}
return Ok(new { Message = "Radarr configuration updated successfully" });
}
[HttpPut("lidarr")]
public async Task<IActionResult> UpdateLidarrConfig([FromBody] LidarrConfig newConfig)
{
// Validate the configuration
newConfig.Validate();
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save Lidarr configuration");
}
return Ok(new { Message = "Lidarr configuration updated successfully" });
}
[HttpPut("notifications")]
public async Task<IActionResult> UpdateNotificationsConfig([FromBody] NotificationsConfig newConfig)
{
// Persist the configuration
var result = await _configManager.SaveConfigurationAsync(newConfig);
if (!result)
{
return StatusCode(500, "Failed to save Notifications configuration");
}
return Ok(new { Message = "Notifications configuration updated successfully" });
}
}
}