oc-catalog/controllers/data.go
2024-08-01 09:27:38 +02:00

83 lines
2.3 KiB
Go

package controllers
import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about data
type DataController struct {
beego.Controller
}
// @Title Update
// @Description create datas
// @Param id path string true "the data id you want to get"
// @Param body body models.data true "The data content"
// @Success 200 {data} models.data
// @router /:id [put]
func (o *DataController) Put() {
// store and return Id or post with UUID
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(oclib.DATA_RESOURCE), res, id)
o.ServeJSON()
}
// @Title Create
// @Description create data
// @Param data body json true "body for data content (Json format)"
// @Success 200 {data} models.data
// @router / [post]
func (o *DataController) Post() {
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(oclib.DATA_RESOURCE), res)
o.ServeJSON()
}
// @Title GetAll
// @Description find data by id
// @Success 200 {data} models.data
// @router / [get]
func (o *DataController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.DATA_RESOURCE))
o.ServeJSON()
}
// @Title Get
// @Description find data by key word
// @Param search path string true "the search you want to get"
// @Success 200 {data} models.data
// @router /search/:search [get]
func (o *DataController) Search() {
search := o.Ctx.Input.Param(":search")
o.Data["json"] = oclib.Search(nil, search, oclib.LibDataEnum(oclib.DATA_RESOURCE))
o.ServeJSON()
}
// @Title Get
// @Description find data by id
// @Param id path string true "the id you want to get"
// @Success 200 {data} models.data
// @router /:id [get]
func (o *DataController) Get() {
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.DATA_RESOURCE), id)
o.ServeJSON()
}
// @Title Delete
// @Description delete the data
// @Param id path string true "The id you want to delete"
// @Success 200 {data} delete success!
// @router /:id [delete]
func (o *DataController) Delete() {
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.DATA_RESOURCE), id)
o.ServeJSON()
}