Skip to content

Commit

Permalink
[extapi] added devices
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Oct 21, 2024
1 parent cff71df commit c636fb7
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.32",
"version": "v0.0.33",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down Expand Up @@ -51,6 +51,7 @@ func main() {
adapter.RaRoute(g, "eurovisionvotes", model.EurovisionVote{})
adapter.RaRoute(g, "finderusers", model.FinderUser{})
adapter.RaRoute(g, "wishlist", model.Wishlist{})
adapter.RaRoute(g, "devices", model.Device{})

// jwt := middleware.Jwt("http://sol.dictummortuum.com:3567/.well-known/jwks.json")

Expand Down
81 changes: 81 additions & 0 deletions pkg/model/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package model

import (
"encoding/json"

"gorm.io/gorm"
)

type Device struct {
Id int64 `json:"id"`
Mac string `json:"mac"`
Alias string `json:"alias"`
}

func (Device) TableName() string {
return "tdevices"
}

func (Device) DefaultFilter(db *gorm.DB) *gorm.DB {
return db
}

func (Device) List(db *gorm.DB, scopes ...func(*gorm.DB) *gorm.DB) (any, error) {
var data []Device
rs := db.Scopes(scopes...).Find(&data)
return data, rs.Error
}

func (Device) Get(db *gorm.DB, id int64) (any, error) {
var data Device
rs := db.First(&data, id)
return data, rs.Error
}

func (obj Device) Update(db *gorm.DB, id int64, body []byte) (any, error) {
model := Device{
Id: id,
}

var payload Device
err := json.Unmarshal(body, &payload)
if err != nil {
return nil, err
}

rs := db.Model(&model).Updates(payload)
if rs.Error != nil {
return nil, err
}

return obj.Get(db, id)
}

func (Device) Create(db *gorm.DB, body []byte) (any, error) {
var payload Device
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 Device) Delete(db *gorm.DB, id int64) (any, error) {
data, err := obj.Get(db, id)
if err != nil {
return nil, err
}

rs := db.Delete(&Device{}, id)
if rs.Error != nil {
return nil, rs.Error
}

return data, nil
}

0 comments on commit c636fb7

Please sign in to comment.