feat: save settings
This commit is contained in:
parent
a727016068
commit
c536ae205d
|
@ -1,18 +1,19 @@
|
||||||
import settings/[types, utils]
|
import settings/types
|
||||||
|
import settings/utils
|
||||||
import web3/conversions
|
import web3/conversions
|
||||||
import sqlcipher
|
import sqlcipher
|
||||||
import json
|
import json
|
||||||
import options
|
import options
|
||||||
|
|
||||||
export Settings, SettingsEnum, `$`, `toSettings`
|
|
||||||
|
|
||||||
proc newSettingsError(key, value: string): ref SettingsError =
|
export types
|
||||||
(ref SettingsError)(msg: "Invalid setting: " & key & ": " & value)
|
|
||||||
|
|
||||||
proc newSettingsError(key: SettingsEnum, value: string): ref SettingsError =
|
|
||||||
newSettingsError($SettingsEnum, value)
|
|
||||||
|
|
||||||
proc createSettings*(db: DbConn, s: Settings, nodecfg: JsonNode) = # TODO: replace JsonNode by a proper NodeConfig object
|
proc checkType(setting: SettingsEnum, value: auto, typeExpected: type) =
|
||||||
|
if not (value is typeExpected):
|
||||||
|
raise (ref SettingsError)(msg: "Invalid setting: '" & $setting & "', type " & "'" & $typeExpected & "' expected")
|
||||||
|
|
||||||
|
proc createSettings*(db: DbConn, s: Settings, nodecfg: JsonNode) = # TODO: replace JsonNode by a proper NodeConfig object?
|
||||||
let query = """INSERT INTO settings (
|
let query = """INSERT INTO settings (
|
||||||
address, currency, current_network, dapps_address,
|
address, currency, current_network, dapps_address,
|
||||||
eip1581_address, installation_id, key_uid,
|
eip1581_address, installation_id, key_uid,
|
||||||
|
@ -45,20 +46,31 @@ proc createSettings*(db: DbConn, s: Settings, nodecfg: JsonNode) = # TODO: repla
|
||||||
s.signingPhrase,
|
s.signingPhrase,
|
||||||
(if s.walletRootAddress.isSome(): $s.walletRootAddress.get() else: ""))
|
(if s.walletRootAddress.isSome(): $s.walletRootAddress.get() else: ""))
|
||||||
|
|
||||||
proc saveSetting*(db:DbConn, setting: SettingsEnum, value: string) =
|
proc saveSetting*(db:DbConn, setting: SettingsEnum, value: auto) =
|
||||||
discard
|
case setting
|
||||||
|
of SettingsEnum.ChaosMode:
|
||||||
|
checkType(setting, value, bool)
|
||||||
|
db.exec("UPDATE settings SET chaos_mode = ? WHERE synthetic_id = 'id'", value)
|
||||||
|
of SettingsEnum.Currency:
|
||||||
|
checkType(setting, value, string)
|
||||||
|
db.exec("UPDATE settings SET currency = ? WHERE synthetic_id = 'id'", $value)
|
||||||
|
else:
|
||||||
|
raise (ref SettingsError)(msg: "Setting: '" & $setting & "'cannot be updated")
|
||||||
|
|
||||||
proc saveSetting*(db:DbConn, setting: string, value: string) =
|
# TODO: add missing settings
|
||||||
discard
|
|
||||||
#[try:
|
|
||||||
parseEnum(setting)
|
proc saveSetting*(db:DbConn, setting: string, value: auto) =
|
||||||
catch
|
try:
|
||||||
newSettingsError(setting, value)]#
|
let s = parseEnum[SettingsEnum](setting)
|
||||||
|
saveSetting(db, s, value)
|
||||||
|
except:
|
||||||
|
raise (ref SettingsError)(msg: "Unknown setting: '" & $setting)
|
||||||
|
|
||||||
proc getNodeConfig*(db: DbConn): JsonNode =
|
proc getNodeConfig*(db: DbConn): JsonNode =
|
||||||
# TODO:
|
let query = "SELECT node_config FROM settings WHERE synthetic_id = 'id'"
|
||||||
# "SELECT node_config FROM settings WHERE synthetic_id = 'id'"
|
for r in rows(db, query):
|
||||||
discard
|
return r[0].strVal.parseJson
|
||||||
|
|
||||||
proc getSettings*(db: DbConn): Settings =
|
proc getSettings*(db: DbConn): Settings =
|
||||||
let query = """SELECT address, chaos_mode, currency, current_network, custom_bootnodes, custom_bootnodes_enabled, dapps_address, eip1581_address,
|
let query = """SELECT address, chaos_mode, currency, current_network, custom_bootnodes, custom_bootnodes_enabled, dapps_address, eip1581_address,
|
||||||
|
|
|
@ -29,6 +29,10 @@ createSettings(db, settingsObj, %* {})
|
||||||
|
|
||||||
let s = getSettings(db)
|
let s = getSettings(db)
|
||||||
|
|
||||||
|
saveSetting(db, SettingsEnum.ChaosMode, "A")
|
||||||
|
|
||||||
|
let s2 = getSettings(db)
|
||||||
|
echo $s2
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
removeFile(path)
|
removeFile(path)
|
||||||
|
|
Loading…
Reference in New Issue