Skip to content

Commit

Permalink
[extapi] added configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Dec 13, 2024
1 parent 7d6b183 commit 50845af
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/servus-extapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")

Expand Down
81 changes: 81 additions & 0 deletions pkg/model/configurations.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 50845af

Please sign in to comment.