feat(API): Edits SettingsRepository to manage FileWatcher Service
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
using Butter.Settings;
|
||||
using Lactose.Context;
|
||||
using Lactose.Models;
|
||||
using Lactose.Services;
|
||||
using System.Data;
|
||||
|
||||
namespace Lactose.Repositories;
|
||||
|
||||
public class SettingsRepository(LactoseDbContext context) : ISettingsRepository {
|
||||
public class SettingsRepository(LactoseDbContext context, IServiceProvider serviceProvider) : ISettingsRepository {
|
||||
public void Create(Setting setting) {
|
||||
//Check if the settings already exist
|
||||
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
||||
if (existingSettings != null)
|
||||
throw new DuplicateNameException("A setting with the same key already exists: " + setting.Name);
|
||||
|
||||
SettingChange(setting);
|
||||
//Adds the new settings
|
||||
context.Settings.Add(setting);
|
||||
}
|
||||
@@ -22,6 +25,7 @@ public class SettingsRepository(LactoseDbContext context) : ISettingsRepository
|
||||
public void Update(Setting setting) {
|
||||
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
||||
if (existingSettings != null) {
|
||||
if (existingSettings.Value != setting.Value) SettingChange(setting);
|
||||
existingSettings.Value = setting.Value;
|
||||
context.Settings.Update(existingSettings);
|
||||
} else {
|
||||
@@ -46,6 +50,23 @@ public class SettingsRepository(LactoseDbContext context) : ISettingsRepository
|
||||
if (existingSettings == null) throw new KeyNotFoundException("The setting with the specified name does not exist: " + name);
|
||||
Delete(existingSettings);
|
||||
}
|
||||
|
||||
|
||||
//TODO: May need to be change on a Event based system, because adding here all the Setting Actions could be too much ramification
|
||||
void SettingChange(Setting setting) {
|
||||
|
||||
if (setting.Name == Settings.FolderScanEnabled.AsString()) {
|
||||
using var scannerService = serviceProvider.GetService<FileSystemScannerService>();
|
||||
if (setting.Value == "false") scannerService?.Disable();
|
||||
else scannerService?.Enable();
|
||||
return;
|
||||
}
|
||||
if (setting.Name == Settings.FolderScanInterval.AsString()) {
|
||||
using var scannerService = serviceProvider.GetService<FileSystemScannerService>();
|
||||
if (!int.TryParse(setting.Value, out var interval)) scannerService?.SetTimerInterval(TimeSpan.FromMinutes(interval));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() { context.Dispose(); }
|
||||
}
|
||||
Reference in New Issue
Block a user