Feature/48 database #114
2 changed files with 37 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
}
|
37
src/helpers/SettingsHelper.ts
Normal file
37
src/helpers/SettingsHelper.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue