/
datamodel_param.go
135 lines (126 loc) · 4.41 KB
/
datamodel_param.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package omc
import (
"net/http"
"github.com/netdoop/netdoop/models/omc"
"github.com/netdoop/netdoop/store"
"github.com/heypkg/store/echohandler"
"github.com/heypkg/store/jsontype"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
// HandleListDataModelParameters godoc
// @Summary List the parameters of a datamodel
// @Tags OMC DataModel
// @ID list-datamodel-parameters
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path string true "Device ID"
// @Success 200 {object} omc.ParameterValues
// @Failure 401 {object} echo.HTTPError "Unauthorized"
// @Failure 500 {object} echo.HTTPError "Internal server error"
// @Router /omc/datamodels/{id}/parameters [get]
func HandleListDataModelParameters(c echo.Context) error {
c.Set("preload", "DataModelParameter")
obj := echohandler.GetObjectFromEchoContext[omc.DataModel](c)
data := obj.Parameters
if data == nil {
data = []*omc.DataModelParameter{}
}
return c.JSON(http.StatusOK, data)
}
type createDataModelParameterBody struct {
Name string
Type string
Writable bool
Description string
DefaultValue string
MetaData *jsontype.Tags
}
// HandleCreateDataModelParameter creates a new data model parameter.
// @Summary Create data model parameter
// @ID create-datamodel-parameter
// @Produce json
// @Security Bearer
// @Param body body createDataModelParameterBody true "DataModelParameter"
// @Success 200 {object} omc.DataModelParameter
// @Failure 400 {object} echo.HTTPError "Bad Request: invalid input parameter"
// @Failure 401 {object} echo.HTTPError "Unauthorized"
// @Failure 500 {object} echo.HTTPError "Internal Server error"
// @Router /omc/datamodels/{id}/parameters [post]
// @Tags OMC DataModel Parameter
func HandleCreateDataModelParameter(c echo.Context) error {
obj := echohandler.GetObjectFromEchoContext[omc.DataModel](c)
var data createDataModelParameterBody
if err := c.Bind(&data); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, errors.Wrap(err, "invalid input parameter").Error())
}
db := store.GetDB()
param := omc.DataModelParameter{
DataModelID: obj.ID,
Default: false,
Name: data.Name,
Type: data.Type,
Description: data.Description,
DefaultValue: data.DefaultValue,
MetaData: data.MetaData,
}
result := db.Create(¶m)
if result.Error != nil {
return echo.NewHTTPError(http.StatusInternalServerError, result.Error)
}
return c.JSON(http.StatusOK, obj)
}
// HandleGetDataModelParameter retrieves a single data model parameter.
// @Summary Get data model parameter
// @ID get-datamodel-parameter
// @Produce json
// @Security Bearer
// @Param id path int true "ID"
// @Param parameter_id path string true "Parameter ID"
// @Success 200 {object} omc.DataModelParameter
// @Failure 401 {object} echo.HTTPError "Unauthorized"
// @Failure 500 {object} echo.HTTPError "Internal Server error"
// @Router /omc/datamodels/{id}/parameters/{parameter_id} [get]
// @Tags OMC DataModel Parameter
func HandleGetDataModelParameter(c echo.Context) error {
obj := echohandler.GetObjectFromEchoContext[omc.DataModel](c)
if obj == nil {
return echo.NewHTTPError(http.StatusNotFound)
}
parameterId := cast.ToUint(c.Param("parameter_id"))
db := store.GetDB()
param := omc.GetDataModelParameterByID(db, obj, parameterId)
if param == nil {
return echo.NewHTTPError(http.StatusNotFound)
}
return c.JSON(http.StatusOK, param)
}
// HandleDeleteDataModelParameter deletes the data model parameter.
// @Summary Delete data model parameter
// @Tags OMC DataModel Parameter
// @ID delete-datamodel-parameter
// @Security Bearer
// @Param id path int true "ID"
// @Param parameter_id path string true "Parameter ID"
// @Success 204
// @Failure 401 {object} echo.HTTPError "Unauthorized"
// @Failure 500 {object} echo.HTTPError "Internal Server error"
// @Router /omc/datamodels/{id}/parameter/{parameter_id} [delete]
func HandleDeleteDataModelParameter(c echo.Context) error {
obj := echohandler.GetObjectFromEchoContext[omc.DataModel](c)
if obj == nil {
return echo.NewHTTPError(http.StatusNotFound)
}
parameterId := cast.ToUint(c.Param("parameter_id"))
db := store.GetDB()
param := omc.GetDataModelParameterByID(db, obj, parameterId)
if param == nil {
return echo.NewHTTPError(http.StatusNotFound)
}
if result := db.Delete(param); result.Error != nil {
return echo.NewHTTPError(http.StatusInternalServerError, result.Error)
}
return c.NoContent(http.StatusNoContent)
}