96 lines
3.8 KiB
Go
96 lines
3.8 KiB
Go
package resources
|
|
|
|
import (
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
|
"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 New[T ResourceInterface](t tools.DataType, username string, peerID string, groups []string, caller *tools.HTTPCaller, g func() utils.DBObject) *resourceMongoAccessor[T] {
|
|
return &resourceMongoAccessor[T]{
|
|
AbstractAccessor: utils.AbstractAccessor{
|
|
ResourceModelAccessor: resource_model.New(),
|
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
|
Caller: caller,
|
|
PeerID: peerID,
|
|
User: username, // Set the caller
|
|
Groups: groups, // Set the caller
|
|
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).SetResourceModel(nil)
|
|
return utils.GenericUpdateOne(set.(T).Trim(), id, dca, dca.generateData()) // TODO CHANGE
|
|
}
|
|
|
|
func (dca *resourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
data.(T).SetResourceModel(nil)
|
|
return utils.GenericStoreOne(data.(T).Trim(), 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) {
|
|
resources, _, err := dca.ResourceModelAccessor.Search(nil, dca.GetType().String())
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
return d, 200, nil
|
|
}, dca)
|
|
}
|
|
|
|
func (wfa *resourceMongoAccessor[T]) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String())
|
|
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
return d
|
|
}, wfa)
|
|
}
|
|
|
|
func (wfa *resourceMongoAccessor[T]) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String())
|
|
return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search),
|
|
func(d utils.DBObject) utils.ShallowDBObject {
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
return d
|
|
}, wfa)
|
|
}
|
|
|
|
func (abs *resourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
|
return &dbs.Filters{
|
|
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
|
|
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
},
|
|
}
|
|
}
|