2022-05-11 22:47:31 +00:00
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"npm/internal/database"
|
2023-07-24 03:42:50 +00:00
|
|
|
"npm/internal/model"
|
2022-05-11 22:47:31 +00:00
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
"gorm.io/datatypes"
|
2022-05-11 22:47:31 +00:00
|
|
|
)
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Model is the model
|
2022-05-11 22:47:31 +00:00
|
|
|
type Model struct {
|
2023-07-24 03:42:50 +00:00
|
|
|
model.ModelBase
|
2023-05-26 01:04:43 +00:00
|
|
|
Name string `json:"name" gorm:"column:name" filter:"name,string"`
|
|
|
|
Description string `json:"description" gorm:"column:description" filter:"description,string"`
|
|
|
|
Value datatypes.JSON `json:"value" gorm:"column:value"`
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// TableName overrides the table name used by gorm
|
|
|
|
func (Model) TableName() string {
|
|
|
|
return "setting"
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByID will load from an ID
|
|
|
|
func (m *Model) LoadByID(id int) error {
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.First(&m, id)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByName will load from a Name
|
|
|
|
func (m *Model) LoadByName(name string) error {
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Where("name = ?", strings.ToLower(name)).First(&m)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save will save this model to the DB
|
|
|
|
func (m *Model) Save() error {
|
2023-01-10 02:50:46 +00:00
|
|
|
// ensure name is trimmed of whitespace
|
2023-05-29 04:45:00 +00:00
|
|
|
m.Name = strings.ToLower(strings.TrimSpace(m.Name))
|
2023-01-10 02:50:46 +00:00
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
if result := db.Save(m); result.Error != nil {
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
2023-07-20 05:19:42 +00:00
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
return nil
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|