oc-lib/models/utils/abstracts.go
2024-07-26 16:41:08 +02:00

115 lines
3.9 KiB
Go

package utils
import (
"fmt"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
var validate = validator.New(validator.WithRequiredStructEnabled())
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
}
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()
}
type AbstractAccessor struct {
Logger zerolog.Logger
Type string
}
func (dma *AbstractAccessor) GetType() string {
return dma.Type
}
func (dma *AbstractAccessor) SetLogger(t DataType) {
dma.Logger = logs.CreateLogger(t.String(), "")
dma.Type = t.String()
}
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
data.GenerateID()
fmt.Println(data.Serialize())
err := validate.Struct(data)
if err != nil {
return nil, 422, err
}
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
res, code, err := accessor.LoadOne(id)
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, code, err
}
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return res, 200, nil
}
func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor Accessor, new DBObject) (DBObject, int, error) {
r, c, err := accessor.LoadOne(id)
if err != nil {
return nil, c, err
}
change := set.Serialize()
loaded := r.Serialize()
for k, v := range change {
loaded[k] = v
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded), id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}
type AbstractResource struct {
AbstractObject
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"`
Owner string `json:"owner,omitempty" bson:"owner,omitempty" validate:"required"`
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"`
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"`
Price string `json:"price,omitempty" bson:"price,omitempty"`
License string `json:"license,omitempty" bson:"license,omitempty"`
Inputs []interface{} `json:"inputs,omitempty" bson:"inputs,omitempty"`
Outputs []interface{} `json:"outputs,omitempty" bson:"outputs,omitempty"`
Proxy *ResourceProxy `json:"proxy,omitempty" bson:"proxy,omitempty"`
}
type ResourceProxy struct {
Host string `json:"host,omitempty" bson:"host,omitempty"`
Port int `json:"port,omitempty" bson:"port,omitempty"`
Command string `json:"command,omitempty" bson:"command,omitempty"`
Args []string `json:"args,omitempty" bson:"args,omitempty"`
EnvArgs map[string]interface{} `json:"env_args,omitempty" bson:"env_args,omitempty"`
}