oc-datacenter/controllers/booking.go

205 lines
7.3 KiB
Go
Raw Normal View History

2024-08-13 11:17:09 +02:00
package controllers
import (
"encoding/json"
"errors"
2024-10-15 09:58:32 +02:00
"fmt"
2024-08-13 11:17:09 +02:00
"time"
2024-08-30 10:55:35 +02:00
2024-08-13 11:17:09 +02:00
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
2025-01-17 17:23:29 +01:00
"cloud.o-forge.io/core/oc-lib/models/booking"
2024-08-13 11:17:09 +02:00
b "cloud.o-forge.io/core/oc-lib/models/booking"
beego "github.com/beego/beego/v2/server/web"
2024-11-13 08:10:20 +01:00
"go.mongodb.org/mongo-driver/bson/primitive"
2024-08-13 11:17:09 +02:00
)
// Operations about workspace
type BookingController struct {
beego.Controller
}
2025-01-17 17:23:29 +01:00
// @Title Search
// @Description search bookings by execution
// @Param id path string true "id execution"
// @Param is_draft query string false "draft wished"
// @Success 200 {workspace} models.workspace
// @router /search/execution/:id [get]
func (o *BookingController) ExecutionSearch() {
/*
* This is a sample of how to use the search function
* The search function is used to search for data in the database
* The search function takes in a filter and a data type
* The filter is a struct that contains the search parameters
* The data type is an enum that specifies the type of data to search for
* The search function returns a list of data that matches the filter
* The data is then returned as a json object
*/
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
isDraft := o.Ctx.Input.Query("is_draft")
f := dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"execution_id": {{Operator: dbs.EQUAL.String(), Value: id}},
},
}
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&f, "", isDraft == "true")
o.ServeJSON()
}
2024-08-13 11:17:09 +02:00
// @Title Search
2024-11-13 08:10:20 +01:00
// @Description search bookings
// @Param start_date path string true "the word search you want to get"
// @Param end_date path string true "the word search you want to get"
2025-01-17 17:23:29 +01:00
// @Param is_draft query string false "draft wished"
2024-11-13 08:10:20 +01:00
// @Success 200 {workspace} models.workspace
// @router /search/:start_date/:end_date [get]
2024-08-13 11:17:09 +02:00
func (o *BookingController) Search() {
2024-11-13 08:10:20 +01:00
/*
* This is a sample of how to use the search function
* The search function is used to search for data in the database
* The search function takes in a filter and a data type
* The filter is a struct that contains the search parameters
* The data type is an enum that specifies the type of data to search for
* The search function returns a list of data that matches the filter
* The data is then returned as a json object
*/
2024-08-13 11:17:09 +02:00
// store and return Id or post with UUID
2025-01-17 17:23:29 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-11-13 08:10:20 +01:00
start_date, _ := time.Parse("2006-01-02", o.Ctx.Input.Param(":start_date"))
end_date, _ := time.Parse("2006-01-02", o.Ctx.Input.Param(":end_date"))
2025-01-17 17:23:29 +01:00
isDraft := o.Ctx.Input.Query("is_draft")
2024-11-13 08:10:20 +01:00
sd := primitive.NewDateTimeFromTime(start_date)
ed := primitive.NewDateTimeFromTime(end_date)
f := dbs.Filters{
And: map[string][]dbs.Filter{
2025-01-17 17:23:29 +01:00
"execution_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
2024-11-13 08:10:20 +01:00
},
}
2025-01-17 17:23:29 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&f, "", isDraft == "true")
2024-08-13 11:17:09 +02:00
o.ServeJSON()
}
// @Title GetAll
// @Description find booking by id
2025-01-17 17:23:29 +01:00
// @Param is_draft query string false "draft wished"
2024-08-13 11:17:09 +02:00
// @Success 200 {booking} models.booking
// @router / [get]
func (o *BookingController) GetAll() {
2025-01-17 17:23:29 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadAll(isDraft == "true")
2024-08-13 11:17:09 +02:00
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() {
2025-01-17 17:23:29 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-13 11:17:09 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:23:29 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadOne(id)
2024-08-13 11:17:09 +02:00
o.ServeJSON()
}
// @Title Check
// @Description check booking
2024-08-26 12:08:43 +02:00
// @Param id path string "id of the datacenter"
2024-08-13 11:17:09 +02:00
// @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"
2025-01-17 17:23:29 +01:00
// @Param is_draft query string false "draft wished"
2024-08-13 11:17:09 +02:00
// @Success 200 {object} models.object
2024-08-26 12:08:43 +02:00
// @router /check/:id/:start_date/:end_date [get]
2024-08-13 11:17:09 +02:00
func (o *BookingController) Check() {
2024-08-30 10:55:35 +02:00
/*
* This function is used to check if a booking is available for a specific datacenter.
* It takes the following parameters:
* - id: the id of the datacenter
2025-01-17 17:23:29 +01:00
* - start_date: the start date of the booking/search/execution/:id
2024-08-30 10:55:35 +02:00
* - end_date: the end date of the booking
*/
2024-08-26 12:08:43 +02:00
id := o.Ctx.Input.Param(":id")
2024-08-13 11:17:09 +02:00
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 {
2025-01-17 17:23:29 +01:00
booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.Check(id, date, &date2, 1) // check if the booking is available
2024-10-15 09:58:32 +02:00
fmt.Println(isAvailable, err2)
2024-08-13 11:17:09 +02:00
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()
}
2025-01-17 17:23:29 +01:00
// @Title Poststatic.
2024-08-13 11:17:09 +02:00
// @Description create booking
// @Param booking body string true "the booking you want to post"
2025-01-17 17:23:29 +01:00
// @Param is_draft query string false "draft wished"
2024-08-13 11:17:09 +02:00
// @Success 200 {object} models.object
// @router / [post]
func (o *BookingController) Post() {
2024-08-30 10:55:35 +02:00
/*
* This function is used to create a booking.
* It takes the following parameters:
* - booking: the booking you want to post
2024-08-30 15:57:26 +02:00
* The booking is a JSON object that contains the following fields:
* - datacenter_resource_id: the id of the datacenter
* - workflow_execution: the workflow execution
2024-08-30 10:55:35 +02:00
*/
2025-01-17 17:23:29 +01:00
var resp booking.Booking
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &resp)
2024-08-13 11:17:09 +02:00
dc_id := resp.ResourceID
2024-08-30 10:55:35 +02:00
// delete all previous bookings
2025-01-17 17:23:29 +01:00
isDraft := o.Ctx.Input.Query("is_draft")
res := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&dbs.Filters{And: map[string][]dbs.Filter{
"workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}},
"resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
}}, "", isDraft == "true")
2024-08-13 11:17:09 +02:00
if res.Code != 200 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": res.Code,
"error": res.Err,
}
o.ServeJSON()
return
}
2024-08-30 10:55:35 +02:00
for _, b := range res.Data { // delete all previous bookings
2025-01-17 17:23:29 +01:00
oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).DeleteOne(b.GetID())
2024-08-13 11:17:09 +02:00
}
2025-01-17 17:23:29 +01:00
b := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).StoreOne(resp.Serialize(&resp))
2024-08-13 11:17:09 +02:00
errormsg := ""
o.Data["json"] = map[string]interface{}{
2025-01-17 17:23:29 +01:00
"data": []interface{}{b},
2024-08-13 11:17:09 +02:00
"code": 200,
"error": errormsg,
}
o.ServeJSON()
}