some
This commit is contained in:
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
// single instance of the validator used in every model Struct to validate the fields
|
||||
@@ -100,15 +100,6 @@ func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
||||
return dma.Caller
|
||||
}
|
||||
|
||||
// Init initializes the accessor with the data type and the http caller
|
||||
func (dma *AbstractAccessor) Init(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) {
|
||||
dma.Logger = logs.CreateLogger(t.String()) // Create a logger with the data type
|
||||
dma.Caller = caller
|
||||
dma.PeerID = peerID
|
||||
dma.Groups = groups // Set the caller
|
||||
dma.Type = t.String() // Set the data type
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
|
||||
data.GenerateID()
|
||||
@@ -189,48 +180,37 @@ func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, erro
|
||||
return f(data)
|
||||
}
|
||||
|
||||
func GenericLoadAll[T DBObject](f func(DBObject, []ShallowDBObject) []ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
results := []T{}
|
||||
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
objs := []ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
results := []T{}
|
||||
if err != nil {
|
||||
wfa.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) {
|
||||
continue
|
||||
}
|
||||
objs = f(r, objs)
|
||||
objs = append(objs, f(r))
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
|
||||
f func(DBObject, []ShallowDBObject) []ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
results := []T{}
|
||||
objs := []ShallowDBObject{}
|
||||
f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = defaultFilters
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.GetLogger().Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) {
|
||||
continue
|
||||
}
|
||||
objs = f(r, objs)
|
||||
}
|
||||
return objs, 200, nil
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
|
@@ -29,7 +29,6 @@ type DBObject interface {
|
||||
|
||||
// Accessor is an interface that defines the basic methods for an Accessor
|
||||
type Accessor interface {
|
||||
Init(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller)
|
||||
GetType() string
|
||||
GetPeerID() string
|
||||
GetGroups() []string
|
||||
|
Reference in New Issue
Block a user