package controllers import ( "encoding/json" "errors" "time" oclib "cloud.o-forge.io/core/oc-lib" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/booking" b "cloud.o-forge.io/core/oc-lib/models/booking" "cloud.o-forge.io/core/oc-lib/models/workflow_execution" beego "github.com/beego/beego/v2/server/web" ) // Operations about workspace type BookingController struct { beego.Controller } // @Title Search // @Description search booking // @Param search path string true "the word search you want to get" // @Success 200 {booking} models.booking // @router /search/:search [get] func (o *BookingController) 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.BOOKING)) o.ServeJSON() } // @Title GetAll // @Description find booking by id // @Success 200 {booking} models.booking // @router / [get] func (o *BookingController) GetAll() { o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.BOOKING)) o.ServeJSON() } // @Title Get // @Description find booking by id // @Param id path string true "the id you want to get" // @Success 200 {booking} models.booking // @router /:id [get] func (o *BookingController) Get() { id := o.Ctx.Input.Param(":id") o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.BOOKING), 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 *BookingController) 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 { booking := &b.Booking{} isAvailable, err2 := booking.CheckBooking(date, &date2) code := 200 err := "" if !isAvailable { code = 409 err = "booking not available" if err2 != nil { err += " - " + err2.Error() } } o.Data["json"] = map[string]interface{}{ "data": map[string]interface{}{ "is_available": isAvailable, }, "code": code, "error": err, } } o.ServeJSON() } // @Title Post // @Description create booking // @Param booking body string true "the booking you want to post" // @Success 200 {object} models.object // @router / [post] func (o *BookingController) Post() { // TODO retrieve objects in body DON'T FORGET TO CHECK IF THE OBJECT IS VALID var resp workflow_execution.WorkflowExecutions json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp) dc_id := resp.ResourceID res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{ "workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}}, "datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}}, }}, "", oclib.LibDataEnum(oclib.BOOKING)) if res.Code != 200 { o.Data["json"] = map[string]interface{}{ "data": nil, "code": res.Code, "error": res.Err, } o.ServeJSON() return } for _, b := range res.Data { oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID()) } books := make([]interface{}, 0) errormsg := "" for _, exec := range resp.Executions { if ok, _ := (&booking.Booking{}).CheckBooking(*exec.ExecDate, exec.EndDate); !ok { res.Err += " -> the booking from " + exec.ExecDate.String() + " is already taken." o.Data["json"] = map[string]interface{}{ "data": nil, "code": res.Code, "error": res.Err + " -> the booking from " + exec.ExecDate.String() + " is already taken.", } o.ServeJSON() return } new := &booking.Booking{ DatacenterResourceID: dc_id, WorkflowExecution: *exec, } b := oclib.StoreOne(oclib.LibDataEnum(oclib.BOOKING), new.Serialize()) if b.Code == 200 { books = append(books, b.Data) } else { errormsg += " -> " + b.Err } } o.Data["json"] = map[string]interface{}{ "data": books, "code": 200, "error": errormsg, } o.ServeJSON() }