refactor(settings): changed config to settings and added in the update method for this as well
strict fields on the updates so we can only change what we want in here
This commit is contained in:
@@ -2,14 +2,17 @@ package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"lst.net/utils/inputs"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
ConfigID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
|
||||
SettingID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
|
||||
Name string `gorm:"uniqueIndex;not null"`
|
||||
Description string `gorm:"type:text"`
|
||||
Value string `gorm:"not null"`
|
||||
@@ -89,11 +92,76 @@ func SeedConfigs(db *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllConfigs(db *gorm.DB) ([]Settings, error) {
|
||||
func GetAllConfigs(db *gorm.DB) ([]map[string]interface{}, error) {
|
||||
var settings []Settings
|
||||
|
||||
result := db.Find(&settings)
|
||||
|
||||
return settings, result.Error
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
// Function to convert struct to map with lowercase keys
|
||||
toLowercase := func(s Settings) map[string]interface{} {
|
||||
t := reflect.TypeOf(s)
|
||||
v := reflect.ValueOf(s)
|
||||
|
||||
data := make(map[string]interface{})
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := strings.ToLower(t.Field(i).Name)
|
||||
data[field] = v.Field(i).Interface()
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Convert each struct in settings slice to a map with lowercase keys
|
||||
var lowercaseSettings []map[string]interface{}
|
||||
for _, setting := range settings {
|
||||
lowercaseSettings = append(lowercaseSettings, toLowercase(setting))
|
||||
}
|
||||
|
||||
return lowercaseSettings, nil
|
||||
}
|
||||
|
||||
func UpdateConfig(db *gorm.DB, id string, input inputs.SettingUpdateInput) error {
|
||||
var cfg Settings
|
||||
if err := db.Where("setting_id =?", id).First(&cfg).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{}
|
||||
|
||||
if input.Description != nil {
|
||||
updates["description"] = *input.Description
|
||||
}
|
||||
if input.Value != nil {
|
||||
updates["value"] = *input.Value
|
||||
}
|
||||
if input.Enabled != nil {
|
||||
updates["enabled"] = *input.Enabled
|
||||
}
|
||||
if input.AppService != nil {
|
||||
updates["app_service"] = *input.AppService
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
return nil // nothing to update
|
||||
}
|
||||
|
||||
return db.Model(&cfg).Updates(updates).Error
|
||||
}
|
||||
|
||||
func DeleteConfig(db *gorm.DB, id uint) error {
|
||||
// Soft delete by ID
|
||||
return db.Delete(&Settings{}, id).Error
|
||||
}
|
||||
|
||||
func RestoreConfig(db *gorm.DB, id uint) error {
|
||||
var cfg Settings
|
||||
if err := db.Unscoped().First(&cfg, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.DeletedAt = gorm.DeletedAt{}
|
||||
return db.Unscoped().Save(&cfg).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user