diff --git a/cmd/servus-extapi/main.go b/cmd/servus-extapi/main.go index d27f128..0163751 100644 --- a/cmd/servus-extapi/main.go +++ b/cmd/servus-extapi/main.go @@ -12,7 +12,7 @@ import ( func Version(c *gin.Context) { rs := map[string]any{ - "version": "v0.0.34", + "version": "v0.0.35", } c.AbortWithStatusJSON(200, rs) } @@ -52,6 +52,7 @@ func main() { adapter.RaRoute(g, "finderusers", model.FinderUser{}) adapter.RaRoute(g, "wishlist", model.Wishlist{}) adapter.RaRoute(g, "devices", model.Device{}) + adapter.RaRoute(g, "configurations", model.Configuration{}) // jwt := middleware.Jwt("http://sol.dictummortuum.com:3567/.well-known/jwks.json") diff --git a/pkg/model/configurations.go b/pkg/model/configurations.go new file mode 100644 index 0000000..07e8dc3 --- /dev/null +++ b/pkg/model/configurations.go @@ -0,0 +1,81 @@ +package model + +import ( + "encoding/json" + + "gorm.io/gorm" +) + +type Configuration struct { + Id int64 `gorm:"primaryKey" json:"id,omitempty"` + Config string `json:"config,omitempty"` + Value int `json:"value,omitempty"` +} + +func (Configuration) TableName() string { + return "tconfig" +} + +func (Configuration) DefaultFilter(db *gorm.DB) *gorm.DB { + return db +} + +func (c Configuration) List(db *gorm.DB, scopes ...func(*gorm.DB) *gorm.DB) (any, error) { + var data []Configuration + rs := db.Scopes(scopes...).Scopes(c.DefaultFilter).Find(&data) + return data, rs.Error +} + +func (Configuration) Get(db *gorm.DB, id int64) (any, error) { + var data Configuration + rs := db.First(&data, id) + return data, rs.Error +} + +func (obj Configuration) Update(db *gorm.DB, id int64, body []byte) (any, error) { + model := Configuration{ + Id: id, + } + + var payload map[string]any + err := json.Unmarshal(body, &payload) + if err != nil { + return nil, err + } + + rs := db.Model(&model).Save(payload) + if rs.Error != nil { + return nil, err + } + + return obj.Get(db, id) +} + +func (Configuration) Create(db *gorm.DB, body []byte) (any, error) { + var payload Configuration + err := json.Unmarshal(body, &payload) + if err != nil { + return nil, err + } + + rs := db.Create(&payload) + if rs.Error != nil { + return nil, err + } + + return payload, nil +} + +func (obj Configuration) Delete(db *gorm.DB, id int64) (any, error) { + data, err := obj.Get(db, id) + if err != nil { + return nil, err + } + + rs := db.Delete(&IgnoredName{}, id) + if rs.Error != nil { + return nil, rs.Error + } + + return data, nil +}