package resources import ( "slices" "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 resourceMongoAccessor[T ResourceInterface] struct { utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller) generateData func() utils.DBObject } // New creates a new instance of the computeMongoAccessor func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest, g func() utils.DBObject) *resourceMongoAccessor[T] { if !slices.Contains([]tools.DataType{tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE}, t) { return nil } return &resourceMongoAccessor[T]{ AbstractAccessor: utils.AbstractAccessor{ Logger: logs.CreateLogger(t.String()), // Create a logger with the data type Request: request, Type: t, }, generateData: g, } } /* * Nothing special here, just the basic CRUD operations */ func (dca *resourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) { return utils.GenericDeleteOne(id, dca) } func (dca *resourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) { set.(T).Trim() return utils.GenericUpdateOne(set, id, dca, dca.generateData()) } func (dca *resourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) { data.(T).Trim() return utils.GenericStoreOne(data, dca) } func (dca *resourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) { return dca.StoreOne(data) } func (dca *resourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) { return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) { d.(T).SetAllowedInstances(dca.Request) return d, 200, nil }, dca) } func (wfa *resourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) { return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject { d.(T).SetAllowedInstances(wfa.Request) return d }, isDraft, wfa) } func (wfa *resourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) { return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search), func(d utils.DBObject) utils.ShallowDBObject { d.(T).SetAllowedInstances(wfa.Request) return d }, isDraft, wfa) } func (abs *resourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters { if search == "*" { search = "" } return &dbs.Filters{ Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided "abstractintanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractintanciatedresource.abstractresource.type": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractintanciatedresource.abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractintanciatedresource.abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractintanciatedresource.abstractresource.owners.name": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractintanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: search}}, }, } }