package controllers
|
|
import (
|
"basic.com/dbapi.git"
|
"basic.com/pubsub/protomsg.git"
|
"bytes"
|
"encoding/json"
|
"github.com/gin-gonic/gin"
|
"github.com/gogo/protobuf/jsonpb"
|
"webserver/extend/code"
|
"webserver/extend/util"
|
)
|
|
type RuleTemplateController struct {
|
|
}
|
|
var jsonpbMarshaler = &jsonpb.Marshaler{
|
EnumsAsInts: true,
|
EmitDefaults: true,
|
OrigName: true,
|
}
|
|
// @Summary 保存场景模板
|
// @Description 保存场景模板
|
// @Accept json
|
// @Produce json
|
// @Tags 场景模板
|
// @Param reqBody body models.RuleTemplate true "模板保存参数"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
|
// @Router /data/api-v/template/rule/save [post]
|
func (rtc RuleTemplateController) Save(c *gin.Context) {
|
var reqBody map[string]interface{}
|
c.BindJSON(&reqBody)
|
var api dbapi.RuleTemplateApi
|
if api.Save(reqBody) {
|
util.ResponseFormat(c, code.Success, "保存成功")
|
} else {
|
util.ResponseFormat(c, code.ComError, "保存失败")
|
}
|
}
|
|
// @Summary 编辑
|
// @Description 编辑
|
// @Accept json
|
// @Produce json
|
// @Tags 场景模板
|
// @Param id path string true "模板id"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
|
// @Router /data/api-v/template/rule/{id} [get]
|
func (rtc RuleTemplateController) Show(c *gin.Context) {
|
id := c.Param("id")
|
if id == "" {
|
util.ResponseFormat(c,code.RequestParamError, "参数有误")
|
return
|
}
|
var api dbapi.RuleTemplateApi
|
b, d := api.Show(id)
|
if b {
|
util.ResponseFormat(c,code.Success, d)
|
} else {
|
util.ResponseFormat(c,code.ComError, "")
|
}
|
}
|
|
// @Summary 根据id删除场景模板
|
// @Description 根据id删除场景模板
|
// @Accept json
|
// @Produce json
|
// @Tags 场景模板
|
// @Param id path string true "模板id"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
|
// @Router /data/api-v/template/rule/{id} [delete]
|
func (rtc RuleTemplateController) Delete(c *gin.Context) {
|
id := c.Param("id")
|
if id == "" {
|
util.ResponseFormat(c,code.RequestParamError, "参数有误")
|
return
|
}
|
var api dbapi.RuleTemplateApi
|
if api.Delete(id) {
|
util.ResponseFormat(c,code.Success, "删除成功")
|
} else {
|
util.ResponseFormat(c,code.ComError, "")
|
}
|
}
|
|
// @Summary 查找所有场景模板
|
// @Description 查找所有场景模板
|
// @Accept json
|
// @Produce json
|
// @Tags 场景模板
|
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
|
// @Router /data/api-v/template/rule [get]
|
func (rtc RuleTemplateController) FindAll(c *gin.Context) {
|
var api dbapi.RuleTemplateApi
|
b, templates := api.FindAll()
|
if b {
|
var result protomsg.RuleTemplateList
|
for _,r := range templates {
|
result.List = append(result.List, &r)
|
}
|
|
var _buffer bytes.Buffer
|
err := jsonpbMarshaler.Marshal(&_buffer, &result)
|
if err == nil {
|
jsonB := _buffer.Bytes()
|
var m map[string]interface{}
|
json.Unmarshal(jsonB, &m)
|
util.ResponseFormat(c,code.Success, m)
|
return
|
}
|
} else {
|
util.ResponseFormat(c, code.ComError, "")
|
}
|
}
|