set up unfonctionnal rework, TODO -> pricing separation
This commit is contained in:
30
models/resources/purchase_resource/purchase_resource.go
Normal file
30
models/resources/purchase_resource/purchase_resource.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package purchase_resource
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type PurchaseResource struct {
|
||||
utils.AbstractObject
|
||||
EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"`
|
||||
ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"`
|
||||
ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"`
|
||||
}
|
||||
|
||||
func (d *PurchaseResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (r *PurchaseResource) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
return r.IsDraft, set // only draft buying can be updated
|
||||
}
|
||||
|
||||
func (r *PurchaseResource) CanDelete() bool { // ENDBuyingDate is passed
|
||||
if r.EndDate != nil {
|
||||
return time.Now().UTC().After(*r.EndDate)
|
||||
}
|
||||
return false // only draft bookings can be deleted
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
package purchase_resource
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type purchaseResourceMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the bookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *purchaseResourceMongoAccessor {
|
||||
return &purchaseResourceMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.BUYING_STATUS.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
Type: tools.BUYING_STATUS,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *purchaseResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericUpdateOne(set, id, a, &PurchaseResource{})
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*PurchaseResource](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||
utils.GenericDeleteOne(id, a)
|
||||
return nil, 404, nil
|
||||
}
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*PurchaseResource](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*PurchaseResource](filters, search, (&PurchaseResource{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *purchaseResourceMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||
utils.GenericDeleteOne(d.GetID(), a)
|
||||
return nil
|
||||
}
|
||||
return d
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user