Files
MilkyShots/Lactose/Repositories/SettingsRepository.cs
REDCODE 9d9491253b refactor: clean up imports, simplify checks, and add SearchDropdown component
- Remove unused using directives across C# and Razor files
- Remove unused IServiceProvider from SettingsRepository
- Simplify null/empty string checks in StatsRepository
- Add null-safe navigation for Albums/Tags in stats queries
- Initialize Asset.Hash default to prevent null refs
- Deduplicate AssetIds in AssetPicker
- Add OnStartedWaiting/OnFinishedWaiting/OnProgressChanged to Job
- Add global SearchDropdown component with keyboard nav
- Fix XML doc param mismatches
2026-07-12 20:47:24 +02:00

94 lines
3.7 KiB
C#

using Lactose.Context;
using Lactose.Models;
using System.Data;
namespace Lactose.Repositories;
/// <inheritdoc />
public class SettingsRepository(LactoseDbContext context) : ISettingsRepository {
/// <inheritdoc />
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);
context.SaveChanges();
}
/// <inheritdoc />
public IEnumerable<Setting> Get() => context.Settings.AsEnumerable();
/// <inheritdoc />
public Setting? Get(string name) => context.Settings.FirstOrDefault(s => s.Name == name);
/// <inheritdoc />
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);
context.SaveChanges();
} else {
// If the settings entry does not exist
throw new KeyNotFoundException("The setting with the specified name does not exist: " + setting.Name);
}
}
/// <inheritdoc />
public void Delete() {
context.Settings.RemoveRange(context.Settings);
context.SaveChanges();
}
/// <inheritdoc />
public void Delete(Setting setting) {
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == setting.Name);
if (existingSettings != null) {
context.Settings.Remove(existingSettings);
context.SaveChanges();
} else {
// If the settings entry does not exist
throw new KeyNotFoundException("The setting with the specified name does not exist: " + setting.Name);
}
}
/// <inheritdoc />
public void Delete(string name) {
var existingSettings = context.Settings.FirstOrDefault(s => s.Name == name);
if (existingSettings == null) throw new KeyNotFoundException("The setting with the specified name does not exist: " + name);
Delete(existingSettings);
context.SaveChanges();
}
/// <summary>
/// Occurs when a setting value changes.
/// </summary>
public static event EventHandler<Setting>? SettingChanged;
static void OnSettingChanged(Setting e) => SettingChanged?.Invoke(null, e);
//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) {
OnSettingChanged(setting);
/*switch (setting.Name) {
case var name when name == Settings.FolderScanEnabled.AsString():
using (var scannerService = serviceProvider.GetService<FileSystemScannerService>()) {
if (setting.Value == "false") scannerService?.Disable();
else scannerService?.Enable();
}
return;
case var name when name == Settings.FolderScanInterval.AsString():
using (var scannerService = serviceProvider.GetService<FileSystemScannerService>()) {
if (int.TryParse(setting.Value, out var interval)) scannerService?.SetTimerInterval(TimeSpan.FromMinutes(interval));
}
return;
}*/
}
/// <inheritdoc />
public void Dispose() => context.Dispose();
}