oc-lib/models/discovery/discovery_mongo_accessor.go

84 lines
2.5 KiB
Go
Raw Normal View History

2024-08-22 13:11:21 +02:00
package discovery
2024-08-12 14:18:13 +02:00
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/models/utils"
)
2024-08-22 13:11:21 +02:00
type discoveryMongoAccessor struct {
2024-08-12 14:18:13 +02:00
utils.AbstractAccessor
}
2024-08-22 13:11:21 +02:00
func New() *discoveryMongoAccessor {
return &discoveryMongoAccessor{}
2024-08-12 14:18:13 +02:00
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
2024-08-12 14:18:13 +02:00
return wfa.GenericDeleteOne(id, wfa)
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &Discovery{})
2024-08-12 14:18:13 +02:00
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
2024-08-12 14:18:13 +02:00
return wfa.GenericStoreOne(data, wfa)
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
2024-08-12 14:18:13 +02:00
return wfa.GenericStoreOne(data, wfa)
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Discovery
2024-08-12 14:18:13 +02:00
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
2024-08-22 13:11:21 +02:00
func (wfa discoveryMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
2024-08-12 14:18:13 +02:00
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
}
2024-08-22 13:11:21 +02:00
var results []Discovery
2024-08-12 14:18:13 +02:00
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}
2024-08-22 13:11:21 +02:00
func (wfa *discoveryMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
2024-08-12 14:18:13 +02:00
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
2024-08-22 13:11:21 +02:00
var results []Discovery
2024-08-12 14:18:13 +02:00
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}