oc-lib/models/booking/booking_mongo_accessor.go

77 lines
2.6 KiB
Go
Raw Normal View History

2024-08-12 14:18:13 +02:00
package booking
import (
2024-11-12 14:44:41 +01:00
"time"
2024-08-12 14:18:13 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
2024-11-28 13:19:01 +01:00
"cloud.o-forge.io/core/oc-lib/logs"
2024-08-12 14:18:13 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-11-12 14:44:41 +01:00
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
2024-11-28 13:19:01 +01:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 14:18:13 +02:00
)
2024-08-22 13:11:21 +02:00
type bookingMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
2024-08-12 14:18:13 +02:00
}
// New creates a new instance of the bookingMongoAccessor
2024-12-04 11:33:08 +01:00
func New(t tools.DataType, username string, peerID string, groups []string, caller *tools.HTTPCaller) *bookingMongoAccessor {
2024-11-28 13:19:01 +01:00
return &bookingMongoAccessor{
AbstractAccessor: utils.AbstractAccessor{
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
Caller: caller,
PeerID: peerID,
2024-12-04 11:33:08 +01:00
Groups: groups,
User: username, // Set the caller
2024-11-28 16:49:41 +01:00
Type: t,
2024-11-28 13:19:01 +01:00
},
}
2024-08-12 14:18:13 +02:00
}
/*
* Nothing special here, just the basic CRUD operations
*/
2024-11-29 10:25:42 +01:00
func (a *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return utils.GenericDeleteOne(id, a)
2024-08-12 14:18:13 +02:00
}
2024-11-29 10:25:42 +01:00
func (a *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return utils.GenericUpdateOne(set, id, a, &Booking{})
2024-08-12 14:18:13 +02:00
}
2024-11-29 10:25:42 +01:00
func (a *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return utils.GenericStoreOne(data, a)
2024-08-12 14:18:13 +02:00
}
2024-11-29 10:25:42 +01:00
func (a *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return utils.GenericStoreOne(data, a)
2024-08-12 14:18:13 +02:00
}
2024-11-28 16:49:41 +01:00
func (a *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
if d.(*Booking).State == workflow_execution.SCHEDULED && time.Now().UTC().After(*d.(*Booking).ExecDate) {
d.(*Booking).State = workflow_execution.FORGOTTEN
2024-11-29 10:25:42 +01:00
utils.GenericRawUpdateOne(d, id, a)
2024-11-28 16:49:41 +01:00
}
return d, 200, nil
}, a)
2024-08-12 14:18:13 +02:00
}
2024-11-28 16:49:41 +01:00
func (a *bookingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
return utils.GenericLoadAll[*Booking](a.getExec(), a)
2024-08-12 14:18:13 +02:00
}
2024-11-28 16:49:41 +01:00
func (a *bookingMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
return utils.GenericSearch[*Booking](filters, search, (&Booking{}).GetObjectFilters(search), a.getExec(), a)
}
func (a *bookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
return func(d utils.DBObject) utils.ShallowDBObject {
if d.(*Booking).State == workflow_execution.SCHEDULED && time.Now().UTC().After(*d.(*Booking).ExecDate) {
d.(*Booking).State = workflow_execution.FORGOTTEN
2024-11-29 10:25:42 +01:00
utils.GenericRawUpdateOne(d, d.GetID(), a)
2024-11-12 15:10:26 +01:00
}
2024-11-28 16:49:41 +01:00
return d
2024-08-12 14:18:13 +02:00
}
}