oc-datacenter/controllers/booking.go

188 lines
5.9 KiB
Go
Raw Permalink 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"
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"
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
}
// @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"
// @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
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"))
sd := primitive.NewDateTimeFromTime(start_date)
ed := primitive.NewDateTimeFromTime(end_date)
f := dbs.Filters{
And: map[string][]dbs.Filter{
"workflowexecution.execution_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
},
}
o.Data["json"] = oclib.Search(&f, "", oclib.LibDataEnum(oclib.BOOKING))
2024-08-13 11:17:09 +02:00
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
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"
// @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
* - start_date: the start date of the booking
* - 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 {
2024-08-30 10:55:35 +02:00
booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.CheckBooking(id, date, &date2) // 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()
}
// @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() {
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
*/
2024-08-13 11:17:09 +02:00
var resp workflow_execution.WorkflowExecutions
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp)
dc_id := resp.ResourceID
2024-08-30 10:55:35 +02:00
// delete all previous bookings
2024-08-13 11:17:09 +02:00
res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{
2024-08-13 14:43:52 +02:00
"workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}},
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
2024-08-13 11:17:09 +02:00
}}, "", 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
}
2024-08-30 10:55:35 +02:00
for _, b := range res.Data { // delete all previous bookings
2024-08-13 11:17:09 +02:00
oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID())
}
books := make([]interface{}, 0)
errormsg := ""
2024-08-30 10:55:35 +02:00
for _, exec := range resp.Executions { // create new bookings
if ok, _ := (&b.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok {
2024-08-13 11:17:09 +02:00
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
}
2024-08-30 10:55:35 +02:00
new := &b.Booking{
2024-11-13 08:10:20 +01:00
ComputeResourceID: dc_id,
WorkflowExecution: *exec,
2024-08-13 11:17:09 +02:00
}
2024-08-30 10:55:35 +02:00
// store the booking
2024-08-13 11:17:09 +02:00
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()
}