Shallow Get All

This commit is contained in:
mr 2024-07-23 11:22:50 +02:00
parent a687206a1b
commit 94cd62acfe
10 changed files with 119 additions and 14 deletions

View File

@ -197,3 +197,17 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
}
return res, 200, nil
}
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}

View File

@ -24,6 +24,12 @@ func (d LibDataEnum) EnumIndex() int {
return int(d)
}
type LibDataShallow struct {
Data []utils.ShallowDBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
type LibData struct {
Data utils.DBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
@ -40,6 +46,14 @@ func GetLogger() zerolog.Logger {
return logs.GetLogger()
}
func LoadAll(collection LibDataEnum) LibDataShallow {
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadAll()
if err != nil {
return LibDataShallow{Data: d, Code: code, Err: err.Error()}
}
return LibDataShallow{Data: d, Code: code}
}
func LoadOne(collection LibDataEnum, id string) LibData {
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
if err != nil {

View File

@ -2,6 +2,7 @@ package data
import (
mongo "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -36,3 +37,18 @@ func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
return &data, 200, nil
}
func (wfa DataMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
for res_mongo.Next(mongo.MngoCtx) {
var obj resources.AbstractResource
res_mongo.Decode(&obj)
objs = append(objs, &obj)
}
return objs, 200, nil
}

View File

@ -2,6 +2,7 @@ package processing
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -39,3 +40,18 @@ func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, int, err
return &processing, 200, nil
}
func (wfa ProcessingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
for res_mongo.Next(mongo.MngoCtx) {
var obj resources.AbstractResource
res_mongo.Decode(&obj)
objs = append(objs, &obj)
}
return objs, 200, nil
}

View File

@ -8,12 +8,7 @@ import (
// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior
//http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type Resource interface {
GetType() utils.DataType
}
// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type AbstractResource struct {
utils.AbstractObject
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"`
@ -23,11 +18,3 @@ type AbstractResource struct {
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"`
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"`
}
func (r *AbstractResource) GetID() string {
return r.UUID
}
func (r *AbstractResource) GetName() string {
return r.Name
}

View File

@ -2,6 +2,7 @@ package storage
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -39,3 +40,18 @@ func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, int, error)
return &storage, 200, nil
}
func (wfa StorageMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
for res_mongo.Next(mongo.MngoCtx) {
var obj resources.AbstractResource
res_mongo.Decode(&obj)
objs = append(objs, &obj)
}
return objs, 200, nil
}

View File

@ -2,6 +2,7 @@ package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -42,3 +43,18 @@ func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, in
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa WorkflowResourceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
for res_mongo.Next(mongo.MngoCtx) {
var obj resources.AbstractResource
res_mongo.Decode(&obj)
objs = append(objs, &obj)
}
return objs, 200, nil
}

View File

@ -19,6 +19,10 @@ func (ao *AbstractObject) GetID() string {
return ao.UUID
}
func (ao *AbstractObject) GetName() string {
return ao.Name
}
func (r *AbstractObject) GenerateID() {
r.UUID = uuid.New().String()
}

View File

@ -1,5 +1,11 @@
package utils
type ShallowDBObject interface {
GenerateID()
GetID() string
GetName() string
}
type DBObject interface {
GenerateID()
GetID() string
@ -12,6 +18,7 @@ type DBObject interface {
type Accessor interface {
SetLogger(t DataType)
GetType() string
LoadAll() ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error)
CopyOne(data DBObject) (DBObject, int, error)

View File

@ -35,3 +35,18 @@ func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa WorkflowMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
for res_mongo.Next(mongo.MngoCtx) {
var obj utils.AbstractObject
res_mongo.Decode(&obj)
objs = append(objs, &obj)
}
return objs, 200, nil
}