oc-workflow/controllers/workflow.go

196 lines
6.5 KiB
Go
Raw Normal View History

2024-07-11 11:40:11 +02:00
package controllers
import (
2024-07-24 10:25:08 +02:00
"encoding/json"
2024-08-12 16:58:24 +02:00
"errors"
2024-07-11 11:40:11 +02:00
2024-07-24 10:25:08 +02:00
oclib "cloud.o-forge.io/core/oc-lib"
2024-08-13 11:36:53 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-08-13 14:35:53 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-11 11:40:11 +02:00
beego "github.com/beego/beego/v2/server/web"
)
// Operations about workflow
type WorkflowController struct {
beego.Controller
}
2024-08-06 11:09:38 +02:00
// @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 *WorkflowController) 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.WORKFLOW))
o.ServeJSON()
}
2024-07-24 10:25:08 +02:00
// @Title Update
// @Description create workflows
// @Param id path string true "the workflowid you want to get"
// @Param body body models.workflow true "The workflow content"
// @Success 200 {object} models.workflow
// @router /:id [put]
func (o *WorkflowController) Put() {
var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
2024-08-13 11:36:53 +02:00
utils.BOOKING.String(): {
2024-08-13 14:35:53 +02:00
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
2024-08-13 11:36:53 +02:00
},
2024-08-26 11:18:26 +02:00
utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.WORKFLOW.String(): {
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
},
2024-08-13 11:36:53 +02:00
}
2024-07-24 10:25:08 +02:00
// 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(10000000), &res)
caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
2024-08-13 08:35:01 +02:00
data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller)
o.Data["json"] = data
2024-07-24 10:25:08 +02:00
o.ServeJSON()
}
2024-07-11 11:40:11 +02:00
// @Title Create
// @Description create workflows
2024-07-24 10:25:08 +02:00
// @Param data body json true "body for data content (Json format)"
// @Success 200 {object} models.workflow
2024-07-11 11:40:11 +02:00
// @router / [post]
func (o *WorkflowController) Post() {
var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
2024-08-13 11:36:53 +02:00
utils.BOOKING.String(): {
2024-08-13 14:35:53 +02:00
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
2024-08-13 11:36:53 +02:00
},
2024-08-26 11:18:26 +02:00
utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
2024-08-13 11:36:53 +02:00
}
2024-07-24 10:25:08 +02:00
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res)
caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
2024-08-13 08:35:01 +02:00
data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller)
o.Data["json"] = data
2024-07-11 11:40:11 +02:00
o.ServeJSON()
}
2024-07-24 10:25:08 +02:00
// @Title Publish
// @Description create workflows
// @Param id path string true "the workflowid you want to get"
// @Success 200 {object} models.workflow
// @router /publish/:id [post]
func (o *WorkflowController) Publish() {
id := o.Ctx.Input.Param(":id")
data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id)
if data.Data != nil { // copy existing workflow as a resource
2024-07-24 10:25:08 +02:00
o.Data["json"] = oclib.CopyOne(oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE), data.Data.Serialize())
} else {
o.Data["json"] = data
}
o.ServeJSON()
}
// @Title GetAll
2024-07-11 11:40:11 +02:00
// @Description find workflow by workflowid
// @Success 200 {workflow} models.workflow
2024-07-24 10:25:08 +02:00
// @router / [get]
func (o *WorkflowController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.WORKFLOW))
2024-07-11 11:40:11 +02:00
o.ServeJSON()
}
2024-07-24 10:25:08 +02:00
// @Title Get
// @Description find workflow by workflowid
// @Param id path string true "the workflowid you want to get"
// @Success 200 {workflow} models.workflow
// @router /:id [get]
func (o *WorkflowController) Get() {
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id)
2024-07-11 11:40:11 +02:00
o.ServeJSON()
}
// @Title Delete
// @Description delete the workflow
2024-07-24 10:25:08 +02:00
// @Param id path string true "The workflowId you want to delete"
2024-07-11 11:40:11 +02:00
// @Success 200 {string} delete success!
2024-07-24 10:25:08 +02:00
// @router /:id [delete]
2024-07-11 11:40:11 +02:00
func (o *WorkflowController) Delete() {
var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
2024-08-13 14:35:53 +02:00
utils.BOOKING.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
},
2024-08-26 11:18:26 +02:00
utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.WORKFLOW.String(): {
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
},
2024-08-13 14:35:53 +02:00
}
2024-07-24 10:25:08 +02:00
id := o.Ctx.Input.Param(":id")
caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
2024-08-13 14:35:53 +02:00
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKFLOW), id, caller)
2024-07-11 11:40:11 +02:00
o.ServeJSON()
}
2024-08-12 16:58:24 +02:00
// @Title Check
// @Description check booking
2024-08-26 11:18:26 +02:00
// @Param id path string "the booking workflow id"
// @Param start_date path string "the booking start date format "2006-01-02T15:04:05"
// @Param end_date path string "the booking end date" format 2006-01-02T15:04:05"
2024-08-12 16:58:24 +02:00
// @Success 200 {object} models.object
2024-08-26 11:18:26 +02:00
// @router /check/:id/:start_date/:end_date [get]
2024-08-12 16:58:24 +02:00
func (o *WorkflowController) Check() {
// store and return Id or post with UUID
/*
* Check if the booking is available
*/
2024-08-26 11:18:26 +02:00
id := o.Ctx.Input.Param(":id")
if id == "" {
2024-08-12 16:58:24 +02:00
o.Data["json"] = map[string]interface{}{
"data": map[string]interface{}{
"is_available": false,
},
"code": 400,
"error": errors.New("invalid date format"),
}
} else {
2024-08-26 11:18:26 +02:00
res := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id)
if res.Code == 200 {
workflow := res.ToWorkflow()
caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{ // paths to call other OC services
2024-08-26 11:18:26 +02:00
utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
},
utils.BOOKING.String(): {
tools.GET: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/check/:id/" + o.Ctx.Input.Param(":start_date") + "/" + o.Ctx.Input.Param(":end_date"),
},
})
isAvailable, err := workflow.CheckBooking(caller) // check booking
2024-08-26 11:18:26 +02:00
code := 200
if !isAvailable { // if not available then its a conflict
2024-08-26 11:18:26 +02:00
code = 409
}
o.Data["json"] = map[string]interface{}{
"data": map[string]interface{}{
"is_available": isAvailable,
},
"code": code,
"error": err,
}
} else {
o.Data["json"] = res
2024-08-12 16:58:24 +02:00
}
2024-08-26 11:18:26 +02:00
2024-08-12 16:58:24 +02:00
}
o.ServeJSON()
}