new oclib + comments

This commit is contained in:
mr
2024-08-30 10:55:35 +02:00
parent a07db36d99
commit 413a5284bb
5 changed files with 29 additions and 11 deletions

View File

@@ -4,10 +4,9 @@ import (
"encoding/json"
"errors"
"time"
"fmt"
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"
@@ -58,7 +57,13 @@ func (o *BookingController) Get() {
// @Success 200 {object} models.object
// @router /check/:id/:start_date/:end_date [get]
func (o *BookingController) Check() {
// store and return Id or post with UUID
/*
* 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
*/
id := o.Ctx.Input.Param(":id")
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"))
@@ -71,8 +76,8 @@ func (o *BookingController) Check() {
"error": errors.New("invalid date format"),
}
} else {
booking := &b.Booking{}
isAvailable, err2 := booking.CheckBooking(id, date, &date2)
booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.CheckBooking(id, date, &date2) // check if the booking is available
code := 200
err := ""
if !isAvailable {
@@ -99,10 +104,15 @@ func (o *BookingController) Check() {
// @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
/*
* This function is used to create a booking.
* It takes the following parameters:
* - booking: the booking you want to post
*/
var resp workflow_execution.WorkflowExecutions
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp)
dc_id := resp.ResourceID
// delete all previous bookings
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}},
@@ -116,13 +126,13 @@ func (o *BookingController) Post() {
o.ServeJSON()
return
}
for _, b := range res.Data {
for _, b := range res.Data { // delete all previous bookings
oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID())
}
books := make([]interface{}, 0)
errormsg := ""
for _, exec := range resp.Executions {
if ok, _ := (&booking.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok {
for _, exec := range resp.Executions { // create new bookings
if ok, _ := (&b.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok {
res.Err += " -> the booking from " + exec.ExecDate.String() + " is already taken."
o.Data["json"] = map[string]interface{}{
"data": nil,
@@ -132,10 +142,11 @@ func (o *BookingController) Post() {
o.ServeJSON()
return
}
new := &booking.Booking{
new := &b.Booking{
DatacenterResourceID: dc_id,
WorkflowExecution: *exec,
}
// store the booking
b := oclib.StoreOne(oclib.LibDataEnum(oclib.BOOKING), new.Serialize())
if b.Code == 200 {
books = append(books, b.Data)