2024-07-18 14:11:13 +02:00
|
|
|
package oclib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
2024-07-18 17:55:27 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
2024-07-18 14:11:13 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models"
|
2024-07-18 15:35:30 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
2024-07-18 18:14:12 +02:00
|
|
|
"github.com/rs/zerolog"
|
2024-07-18 14:39:54 +02:00
|
|
|
)
|
|
|
|
|
2024-07-18 15:35:30 +02:00
|
|
|
type LibDataEnum int
|
2024-07-18 15:15:01 +02:00
|
|
|
|
2024-07-18 15:02:39 +02:00
|
|
|
const (
|
2024-07-19 10:54:58 +02:00
|
|
|
INVALID LibDataEnum = iota
|
|
|
|
DATA_RESOURCE = utils.DATA_RESOURCE
|
|
|
|
PROCESSING_RESOURCE = utils.PROCESSING_RESOURCE
|
|
|
|
STORAGE_RESOURCE = utils.STORAGE_RESOURCE
|
|
|
|
DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
|
|
|
|
WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
|
|
|
|
WORKFLOW = utils.WORKFLOW
|
2024-07-18 15:02:39 +02:00
|
|
|
)
|
2024-07-18 14:39:54 +02:00
|
|
|
|
2024-07-18 15:35:30 +02:00
|
|
|
func (d LibDataEnum) EnumIndex() int {
|
2024-07-18 15:15:01 +02:00
|
|
|
return int(d)
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:35:30 +02:00
|
|
|
type LibData struct {
|
2024-07-19 10:54:58 +02:00
|
|
|
DataResource utils.DBObject `bson:"data" json:"data"`
|
2024-07-19 11:27:58 +02:00
|
|
|
Code int `bson:"code" json:"code"`
|
|
|
|
Err string `bson:"error" json:"error"`
|
2024-07-18 15:35:30 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
func Init(appName string) {
|
|
|
|
logs.SetAppName(appName)
|
|
|
|
logs.SetLogger(logs.CreateLogger("main", ""))
|
2024-07-18 14:11:13 +02:00
|
|
|
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:14:12 +02:00
|
|
|
func GetLogger() zerolog.Logger {
|
2024-07-19 09:32:58 +02:00
|
|
|
return logs.GetLogger()
|
2024-07-18 17:59:25 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
func LoadOne(collection LibDataEnum, id string) LibData {
|
2024-07-19 11:27:58 +02:00
|
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
|
|
|
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
2024-07-18 14:39:54 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
|
2024-07-19 09:32:58 +02:00
|
|
|
model := models.Model(collection.EnumIndex())
|
|
|
|
set = model.Deserialize(set).Serialize()
|
2024-07-19 11:27:58 +02:00
|
|
|
d, code, err := model.GetAccessor().UpdateOne(set, id)
|
|
|
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
2024-07-18 14:39:54 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
func DeleteOne(collection LibDataEnum, id string) LibData {
|
2024-07-19 11:27:58 +02:00
|
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
|
|
|
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
2024-07-18 14:39:54 +02:00
|
|
|
}
|
|
|
|
|
2024-07-19 13:41:10 +02:00
|
|
|
func CopyOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
|
|
|
model := models.Model(collection.EnumIndex())
|
|
|
|
d, code, err := model.GetAccessor().CopyOne(model.Deserialize(object))
|
|
|
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
|
|
|
}
|
|
|
|
|
2024-07-18 17:55:27 +02:00
|
|
|
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
|
|
|
model := models.Model(collection.EnumIndex())
|
2024-07-19 11:27:58 +02:00
|
|
|
d, code, err := model.GetAccessor().StoreOne(model.Deserialize(object))
|
|
|
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
2024-07-18 14:11:13 +02:00
|
|
|
}
|