refactor(app): moved all db and log to one intialize spot
This commit is contained in:
41
backend/internal/system/settings/get_settings.go
Normal file
41
backend/internal/system/settings/get_settings.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
)
|
||||
|
||||
func GetAllSettings(db *gorm.DB) ([]map[string]interface{}, error) {
|
||||
var settings []models.Settings
|
||||
result := db.Find(&settings)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
// Function to convert struct to map with lowercase keys
|
||||
toLowercase := func(s models.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
|
||||
}
|
||||
Reference in New Issue
Block a user