refactor(correction to folder sturcture): before we got to deep resturctures to best pactice folder
This commit is contained in:
41
backend/internal/system/settings/getSettings.go
Normal file
41
backend/internal/system/settings/getSettings.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
|
||||
}
|
||||
8
backend/internal/system/settings/inputs.go
Normal file
8
backend/internal/system/settings/inputs.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package settings
|
||||
|
||||
type SettingUpdateInput struct {
|
||||
Description *string `json:"description"`
|
||||
Value *string `json:"value"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
AppService *string `json:"app_service"`
|
||||
}
|
||||
88
backend/internal/system/settings/settings.go
Normal file
88
backend/internal/system/settings/settings.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lst.net/internal/db"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func RegisterSettingsRoutes(l *gin.Engine, baseUrl string) {
|
||||
// seed the db on start up
|
||||
db.SeedSettings(db.DB)
|
||||
|
||||
s := l.Group(baseUrl + "/api/v1")
|
||||
s.GET("/settings", getSettings)
|
||||
s.PATCH("/settings/:id", updateSettingById)
|
||||
}
|
||||
|
||||
func getSettings(c *gin.Context) {
|
||||
log := logger.New()
|
||||
configs, err := GetAllSettings(db.DB)
|
||||
log.Info("Current Settings", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log := logger.New()
|
||||
log.Error("Current Settings", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
"error": err,
|
||||
})
|
||||
c.JSON(500, gin.H{"message": "There was an error getting the settings", "error": err})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "Current settings", "data": configs})
|
||||
}
|
||||
|
||||
func updateSettingById(c *gin.Context) {
|
||||
log := logger.New()
|
||||
settingID := c.Param("id")
|
||||
|
||||
if settingID == "" {
|
||||
c.JSON(500, gin.H{"message": "Invalid data"})
|
||||
log.Error("Invalid data", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
})
|
||||
return
|
||||
}
|
||||
var setting SettingUpdateInput
|
||||
|
||||
//err := c.ShouldBindBodyWithJSON(&setting)
|
||||
|
||||
decoder := json.NewDecoder(c.Request.Body) // more strict and will force us to have correct data
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(&setting); err != nil {
|
||||
c.JSON(400, gin.H{"message": "Invalid request body", "error": err.Error()})
|
||||
log.Error("Invalid request body", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := UpdateSetting(db.DB, settingID, setting); err != nil {
|
||||
c.JSON(500, gin.H{"message": "Failed to update setting", "error": err.Error()})
|
||||
log.Error("Failed to update setting", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "Setting was just updated", "data": setting})
|
||||
|
||||
}
|
||||
34
backend/internal/system/settings/update_setting.go
Normal file
34
backend/internal/system/settings/update_setting.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
)
|
||||
|
||||
func UpdateSetting(db *gorm.DB, id string, input SettingUpdateInput) error {
|
||||
var cfg models.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
|
||||
}
|
||||
Reference in New Issue
Block a user