implement new json_data model

This commit is contained in:
Adrian Zürcher
2025-04-29 08:31:06 +02:00
parent 0a137c9d86
commit 49d8d03d8a
25 changed files with 609 additions and 572 deletions

82
dbm/json_data.go Normal file
View File

@@ -0,0 +1,82 @@
package dbm
import (
"net/http"
"github.com/gin-gonic/gin"
json_data "github.com/tecamino/tecamino-json_data"
json_dataModels "github.com/tecamino/tecamino-json_data/models"
)
func (d *DBMHandler) Json_Data(c *gin.Context) {
var err error
payload, err := json_data.ParseRequest(c.Request.Body)
if err != nil {
r := json_data.NewResponse()
r.SendError(err.Error())
c.JSON(http.StatusBadRequest, r)
return
}
respond := json_dataModels.NewResponse()
if payload.Get != nil {
var depth uint = 1
for _, get := range payload.Get {
if get.Query != nil {
depth = get.Query.Depth
}
for _, res := range d.QueryDatapoints(depth, get.Path) {
respond.AddGet(json_dataModels.Get{
Uuid: res.Uuid,
Path: res.Path,
Type: res.Type,
Value: res.Value,
Rights: res.ReadWrite,
Drivers: &res.Drivers,
})
}
}
}
if payload.Set != nil {
respond.Set, err = d.CreateDatapoints(payload.Set...)
if err != nil {
r := json_data.NewResponse()
r.SendError(err.Error())
c.JSON(http.StatusBadRequest, r)
return
}
}
c.JSON(200, respond)
return
}
func (d *DBMHandler) Delete(c *gin.Context) {
var err error
payload, err := json_data.ParseRequest(c.Request.Body)
if err != nil {
r := json_data.NewResponse()
r.SendError(err.Error())
c.JSON(http.StatusBadRequest, r)
return
}
response := json_data.NewResponse()
if payload.Set != nil {
response.Set, err = d.RemoveDatapoint(payload.Set...)
if err != nil {
r := json_data.NewResponse()
r.SendError(err.Error())
c.JSON(http.StatusBadRequest, r)
return
}
}
c.JSON(200, response)
return
}