package controllers import ( "encoding/json" "errors" "time" oclib "cloud.o-forge.io/core/oc-lib" w "cloud.o-forge.io/core/oc-lib/models/workflow" tools "cloud.o-forge.io/core/oc-lib/tools" beego "github.com/beego/beego/v2/server/web" "github.com/nats-io/nats.go" ) // Operations about workflow type WorkflowController 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 *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() } // @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() { // 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) caller := tools.NewHTTPCaller("", "", "", oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING))+"/oc/booking") data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller) data.Err += o.SetNATSPub(res, data) o.Data["json"] = data o.ServeJSON() } func (o *WorkflowController) SetNATSPub(res map[string]interface{}, data oclib.LibData) string { if sched, ok := res["schedule"]; ok && sched != nil && data.Code == 200 { workflow := data.ToWorkflow() if workflow != nil && workflow.Schedule != nil { // HERE NATS nc, err := nats.Connect(nats.DefaultURL) if err != nil { return " -> Could not reach NATS server" } start := "" if workflow.Schedule.Start != nil { start = workflow.Schedule.Start.String() } end := "" if workflow.Schedule.End != nil { end = workflow.Schedule.End.String() } str_description, _ := json.Marshal(map[string]string{ "workflow_id": workflow.UUID, "start_date": start, "stop_date": end, }) err = nc.Publish("workflowsUpdate", str_description) time.Sleep(time.Second * 2) if err != nil { return " -> " + err.Error() } } } return "" } // @Title Create // @Description create workflows // @Param data body json true "body for data content (Json format)" // @Success 200 {object} models.workflow // @router / [post] func (o *WorkflowController) Post() { var res map[string]interface{} json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) caller := tools.NewHTTPCaller("", "", "", oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING))+"/oc/booking") data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller) data.Err += o.SetNATSPub(res, data) o.Data["json"] = data o.ServeJSON() } // @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 { o.Data["json"] = oclib.CopyOne(oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE), data.Data.Serialize()) } else { o.Data["json"] = data } o.ServeJSON() } // @Title GetAll // @Description find workflow by workflowid // @Success 200 {workflow} models.workflow // @router / [get] func (o *WorkflowController) GetAll() { o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.WORKFLOW)) o.ServeJSON() } // @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) o.ServeJSON() } // @Title Delete // @Description delete the workflow // @Param id path string true "The workflowId you want to delete" // @Success 200 {string} delete success! // @router /:id [delete] func (o *WorkflowController) Delete() { id := o.Ctx.Input.Param(":id") o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKFLOW), id) o.ServeJSON() } // @Title Check // @Description check booking // @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" // @Success 200 {object} models.object // @router /check/:start_date/:end_date [get] func (o *WorkflowController) Check() { // store and return Id or post with UUID date, err := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":start_date")) date2, err2 := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":end_date")) if err != nil || err2 != nil { o.Data["json"] = map[string]interface{}{ "data": map[string]interface{}{ "is_available": false, }, "code": 400, "error": errors.New("invalid date format"), } } else { workflow := &w.Workflow{} workflow.Schedule = &w.WorkflowSchedule{Start: &date, End: &date2} isAvailable, err := workflow.CheckBooking(oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/check/:start_date/:end_date") code := 200 if !isAvailable { code = 409 } o.Data["json"] = map[string]interface{}{ "data": map[string]interface{}{ "is_available": isAvailable, }, "code": code, "error": err, } } o.ServeJSON() }