refactor(correction to folder sturcture): before we got to deep resturctures to best pactice folder
This commit is contained in:
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})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user