Add ability to save a setting to the database

This commit is contained in:
Ethan Lane 2022-03-03 18:59:45 +00:00
parent db9f639766
commit 8b2da347b1
Signed by: Vylpes
GPG key ID: EED233CC06D12504
2 changed files with 37 additions and 14 deletions

View file

@ -31,18 +31,4 @@ export default class Setting extends BaseEntity {
return single;
}
public static async FetchValueByKeyOrDefault(key: string): Promise<string | undefined> {
const connection = getConnection();
const repository = connection.getRepository(Setting);
const single = await repository.findOne({ Key: key });
if (!single) {
return DefaultValues.GetValue(key);
}
return single.Value;
}
}

View file

@ -0,0 +1,37 @@
import { getConnection } from "typeorm";
import DefaultValues from "../constants/DefaultValues";
import Setting from "../entity/Setting";
export default class SettingsHelper {
public static async GetSetting(key: string): Promise<string | undefined> {
const connection = getConnection();
const repository = connection.getRepository(Setting);
const single = await repository.findOne({ Key: key });
if (!single) {
return DefaultValues.GetValue(key);
}
return single.Value;
}
public static async SetSetting(key: string, value: string): Promise<void> {
const connection = getConnection();
const repository = connection.getRepository(Setting);
const setting = await repository.findOne({ Key: key });
if (setting) {
setting.UpdateBasicDetails(key, value);
await setting.Save(Setting, setting);
} else {
const newSetting = new Setting(key, value);
await newSetting.Save(Setting, newSetting);
}
}
}