156 lines
4.9 KiB
Go
Executable File
156 lines
4.9 KiB
Go
Executable File
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"oc-catalog/infrastructure"
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// Operations about workflow
|
|
type WorkflowController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
var workflow_collection = oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)
|
|
var workflow_dt = tools.WORKFLOW_RESOURCE
|
|
|
|
// @Title Update
|
|
// @Description create workflows
|
|
// @Param id path string true "the workflow id you want to get"
|
|
// @Param body body models.workflow true "The workflow content"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router /:id [put]
|
|
func (o *WorkflowController) Put() {
|
|
// store and return Id or post with UUID
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
var res map[string]interface{}
|
|
id := o.Ctx.Input.Param(":id")
|
|
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
|
|
data := oclib.NewRequest(workflow_collection, user, peerID, groups, nil).UpdateOne(res, id)
|
|
if data.Err == "" {
|
|
data, _ := json.Marshal(data.Data.Serialize(data.Data))
|
|
infrastructure.EmitNATS(user, tools.PropalgationMessage{
|
|
Action: tools.PB_UPDATE,
|
|
Payload: data,
|
|
DataType: workflow_dt.EnumIndex(),
|
|
})
|
|
}
|
|
o.Data["json"] = data
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Create
|
|
// @Description create workflow
|
|
// @Param workflow body json true "body for workflow content (Json format)"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router / [post]
|
|
func (o *WorkflowController) Post() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
var res map[string]interface{}
|
|
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
|
|
data := oclib.NewRequest(workflow_collection, user, peerID, groups, nil).StoreOne(res)
|
|
if data.Err == "" {
|
|
data, _ := json.Marshal(data.Data.Serialize(data.Data))
|
|
infrastructure.EmitNATS(user, tools.PropalgationMessage{
|
|
Action: tools.PB_CREATE,
|
|
DataType: workflow_dt.EnumIndex(),
|
|
Payload: data,
|
|
})
|
|
}
|
|
o.Data["json"] = data
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title GetAll
|
|
// @Description find workflow by id
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router / [get]
|
|
func (o *WorkflowController) GetAll() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
o.Data["json"] = oclib.NewRequest(workflow_collection, user, peerID, groups, nil).LoadAll(isDraft == "true")
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Search
|
|
// @Description find workflow by key word
|
|
// @Param search path string true "the search you want to get"
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router /search/:search [get]
|
|
func (o *WorkflowController) Search() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
search := o.Ctx.Input.Param(":search")
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
o.Data["json"] = oclib.NewRequest(workflow_collection, user, peerID, groups, nil).Search(nil, search, isDraft == "true")
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Search Decentralized
|
|
// @Description find workflow by key word
|
|
// @Param search path string true "the search you want to get"
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router /search/:search/decentralized/:type [get]
|
|
func (o *WorkflowController) SearchDecentralized() {
|
|
user, _, _ := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
search := o.Ctx.Input.Param(":search")
|
|
t := o.Ctx.Input.Param(":type")
|
|
b, err := json.Marshal(map[string]string{
|
|
"search": search,
|
|
"type": t,
|
|
})
|
|
if err != nil {
|
|
o.Data["json"] = map[string]interface{}{
|
|
"data": nil,
|
|
"code": 400,
|
|
"error": err,
|
|
}
|
|
o.ServeJSON()
|
|
return
|
|
}
|
|
infrastructure.EmitNATS(user, tools.PropalgationMessage{
|
|
Action: tools.PB_SEARCH,
|
|
DataType: workflow_dt.EnumIndex(),
|
|
Payload: b,
|
|
})
|
|
Websocket(o.Ctx.Request.Context(), user, o.Ctx.ResponseWriter, o.Ctx.Request)
|
|
}
|
|
|
|
// @Title Get
|
|
// @Description find workflow by id
|
|
// @Param id path string true "the id you want to get"
|
|
// @Success 200 {workflow} models.workflow
|
|
// @router /:id [get]
|
|
func (o *WorkflowController) Get() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
id := o.Ctx.Input.Param(":id")
|
|
o.Data["json"] = oclib.NewRequest(workflow_collection, user, peerID, groups, nil).LoadOne(id)
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Delete
|
|
// @Description delete the workflow
|
|
// @Param id path string true "The id you want to delete"
|
|
// @Success 200 {workflow} delete success!
|
|
// @router /:id [delete]
|
|
func (o *WorkflowController) Delete() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
id := o.Ctx.Input.Param(":id")
|
|
data := oclib.NewRequest(workflow_collection, user, peerID, groups, nil).DeleteOne(id)
|
|
if data.Err == "" {
|
|
data, _ := json.Marshal(data.Data.Serialize(data.Data))
|
|
infrastructure.EmitNATS(user, tools.PropalgationMessage{
|
|
DataType: workflow_dt.EnumIndex(),
|
|
Action: tools.PB_DELETE,
|
|
Payload: data,
|
|
})
|
|
}
|
|
o.Data["json"] = data
|
|
o.ServeJSON()
|
|
}
|