Stricter datetime input

This commit is contained in:
Magnus Åhall 2024-06-27 13:14:37 +02:00
parent 3109124a88
commit ab87da256c
4 changed files with 51 additions and 8 deletions

View file

@ -6,9 +6,11 @@ import (
// Standard
"database/sql"
"time"
)
type Configuration struct {
timezoneLocation *time.Location
Settings map[string]string
}
@ -36,6 +38,7 @@ func SmonConfigInit() (cfg Configuration, err error) {
cfg.Settings[setting] = value
}
err = cfg.LoadTimezone()
return
}
@ -49,6 +52,15 @@ func (cfg *Configuration) Validate() (err error) {
return
}
func (cfg *Configuration) LoadTimezone() (err error) {
cfg.timezoneLocation, err = time.LoadLocation(cfg.Settings["TIMEZONE"])
return
}
func (cfg *Configuration) Timezone() *time.Location {
return cfg.timezoneLocation
}
func (cfg *Configuration) SetTheme(theme string) (err error) {
cfg.Settings["THEME"] = theme
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
@ -57,6 +69,15 @@ func (cfg *Configuration) SetTheme(theme string) (err error) {
func (cfg *Configuration) SetTimezone(tz string) (err error) {
cfg.Settings["TIMEZONE"] = tz
err = cfg.LoadTimezone()
if err != nil {
return werr.Wrap(err).WithData(tz)
}
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
if err != nil {
return werr.Wrap(err).WithData(tz)
}
return
}