oc-workspace/controllers/workspace.go
2024-08-30 16:00:49 +02:00

116 lines
4.2 KiB
Go

package controllers
import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about workspace
type WorkspaceController struct {
beego.Controller
}
// @Title Search
// @Description search workspace
// @Param search path string true "the word search you want to get"
// @Success 200 {workspace} models.workspace
// @router /search/:search [get]
func (o *WorkspaceController) Search() {
// store and return Id or post with UUID
search := o.Ctx.Input.Param(":search")
o.Data["json"] = oclib.Search(nil, search, oclib.LibDataEnum(oclib.WORKSPACE))
o.ServeJSON()
}
// @Title Update
// @Description create workspaces
// @Param id path string true "the workspace id you want to get"
// @Param body body models.workspace true "The workspace content"
// @Success 200 {workspace} models.workspace
// @router /:id [put]
func (o *WorkspaceController) Put() {
// store and return Id or post with UUID
var paths = map[string]map[tools.METHOD]string{
utils.PEER.String(): { // paths to call to status of peers
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.WORKSPACE.String(): { // paths to call to delete/update workspace on peer destination
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
},
}
caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
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.WORKSPACE), res, id, caller)
o.ServeJSON()
}
// @Title Create
// @Description create workspace
// @Param data body json true "body for data content (Json format)"
// @Success 200 {workspace} models.workspace
// @router / [post]
func (o *WorkspaceController) Post() {
var paths = map[string]map[tools.METHOD]string{
utils.PEER.String(): { // paths to call to status of peers
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.WORKSPACE.String(): { // paths to call to delete/update workspace on peer destination
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
},
}
caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(oclib.WORKSPACE), res, caller)
o.ServeJSON()
}
// @Title GetAll
// @Description find workspace by id
// @Success 200 {workspace} models.workspace
// @router / [get]
func (o *WorkspaceController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.WORKSPACE))
o.ServeJSON()
}
// @Title Get
// @Description find workflow by id
// @Param id path string true "the id you want to get"
// @Success 200 {workspace} models.workspace
// @router /:id [get]
func (o *WorkspaceController) Get() {
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.WORKSPACE), id)
o.ServeJSON()
}
// @Title Delete
// @Description delete the workspace
// @Param id path string true "The id you want to delete"
// @Success 200 {workspace} delete success!
// @router /:id [delete]
func (o *WorkspaceController) Delete() {
id := o.Ctx.Input.Param(":id")
var paths = map[string]map[tools.METHOD]string{
utils.PEER.String(): { // paths to call to status of peers
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.WORKSPACE.String(): { // paths to call to delete/update workspace on peer destination
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workspace/:id",
},
}
caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKSPACE), id, caller)
o.ServeJSON()
}