package models

var CustomConfigs []Config

type Config struct {
	ID        uint   `gorm:"primaryKey" json:"id"`
	ConfName  string `gorm:"varchar(255)" json:"conf_name"`
	ConfKey   string `gorm:"varchar(255) index:uidx_conf_key,unique" json:"conf_key"`
	ConfValue string `json:"conf_value"`
}

func UpdateConfig(key string, value string) {
	c := &Config{
		ConfValue: value,
	}
	DB.Model(c).Where("conf_key = ?", key).Updates(c)
	InitConfig()
}

func FindConfigs() []Config {
	var config []Config
	DB.Find(&config)
	return config
}

func InitConfig() {
	CustomConfigs = FindConfigs()
}

func FindConfig(key string) string {
	for _, config := range CustomConfigs {
		if key == config.ConfKey {
			return config.ConfValue
		}
	}
	return ""
}
