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.Context;
|
||||||
using Lactose.Models;
|
using Lactose.Models;
|
||||||
|
using Lactose.Services;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
|
||||||
namespace Lactose.Repositories;
|
namespace Lactose.Repositories;
|
||||||
|
|
||||||
public class SettingsRepository(LactoseDbContext context) : ISettingsRepository {
|
public class SettingsRepository(LactoseDbContext context, IServiceProvider serviceProvider) : ISettingsRepository {
|
||||||
public void Create(Setting setting) {
|
public void Create(Setting setting) {
|
||||||
//Check if the settings already exist
|
//Check if the settings already exist
|
||||||
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
||||||
if (existingSettings != null)
|
if (existingSettings != null)
|
||||||
throw new DuplicateNameException("A setting with the same key already exists: " + setting.Name);
|
throw new DuplicateNameException("A setting with the same key already exists: " + setting.Name);
|
||||||
|
|
||||||
|
SettingChange(setting);
|
||||||
//Adds the new settings
|
//Adds the new settings
|
||||||
context.Settings.Add(setting);
|
context.Settings.Add(setting);
|
||||||
}
|
}
|
||||||
@@ -22,6 +25,7 @@ public class SettingsRepository(LactoseDbContext context) : ISettingsRepository
|
|||||||
public void Update(Setting setting) {
|
public void Update(Setting setting) {
|
||||||
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
|
||||||
if (existingSettings != null) {
|
if (existingSettings != null) {
|
||||||
|
if (existingSettings.Value != setting.Value) SettingChange(setting);
|
||||||
existingSettings.Value = setting.Value;
|
existingSettings.Value = setting.Value;
|
||||||
context.Settings.Update(existingSettings);
|
context.Settings.Update(existingSettings);
|
||||||
} else {
|
} 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);
|
if (existingSettings == null) throw new KeyNotFoundException("The setting with the specified name does not exist: " + name);
|
||||||
Delete(existingSettings);
|
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(); }
|
public void Dispose() { context.Dispose(); }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user