From 6a47f84fccdf3b64ea3368af78ce3848da02527c Mon Sep 17 00:00:00 2001 From: MrFastwind Date: Wed, 30 Jul 2025 20:44:59 +0200 Subject: [PATCH] feat(API): Edits SettingsRepository to manage FileWatcher Service --- Lactose/Repositories/SettingsRepository.cs | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Lactose/Repositories/SettingsRepository.cs b/Lactose/Repositories/SettingsRepository.cs index cb2d7b0..e72930f 100644 --- a/Lactose/Repositories/SettingsRepository.cs +++ b/Lactose/Repositories/SettingsRepository.cs @@ -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(); + if (setting.Value == "false") scannerService?.Disable(); + else scannerService?.Enable(); + return; + } + if (setting.Name == Settings.FolderScanInterval.AsString()) { + using var scannerService = serviceProvider.GetService(); + if (!int.TryParse(setting.Value, out var interval)) scannerService?.SetTimerInterval(TimeSpan.FromMinutes(interval)); + return; + } + } public void Dispose() { context.Dispose(); } } \ No newline at end of file