2024-07-18 11:51:12 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-07-19 13:15:51 +02:00
|
|
|
"encoding/json"
|
|
|
|
|
2024-07-18 13:35:14 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
2024-07-18 18:14:12 +02:00
|
|
|
"github.com/go-playground/validator/v10"
|
2024-07-19 10:54:58 +02:00
|
|
|
"github.com/google/uuid"
|
2024-07-18 11:51:12 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
2024-07-19 10:54:58 +02:00
|
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
|
|
|
|
type AbstractObject struct {
|
|
|
|
UUID string `json:"id" required:"true" bson:"_id" validate:"required"`
|
|
|
|
Name string `json:"name" required:"true" bson:"name" validate:"required"`
|
|
|
|
}
|
|
|
|
|
2024-07-19 13:27:34 +02:00
|
|
|
func (ao *AbstractObject) GetID() string {
|
|
|
|
return ao.UUID
|
|
|
|
}
|
|
|
|
|
2024-07-19 13:15:51 +02:00
|
|
|
func (dma *AbstractObject) ObjDeserialize(j map[string]interface{}) *AbstractObject {
|
|
|
|
b, err := json.Marshal(j)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
json.Unmarshal(b, dma)
|
|
|
|
return dma
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dma *AbstractObject) ObjSerialize() map[string]interface{} {
|
|
|
|
var m map[string]interface{}
|
|
|
|
b, err := json.Marshal(dma)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
json.Unmarshal(b, dma)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2024-07-19 10:54:58 +02:00
|
|
|
func (r *AbstractObject) GenerateID() {
|
|
|
|
r.UUID = uuid.New().String()
|
|
|
|
}
|
2024-07-18 18:14:12 +02:00
|
|
|
|
2024-07-18 11:51:12 +02:00
|
|
|
type AbstractAccessor struct {
|
|
|
|
Logger zerolog.Logger
|
2024-07-18 16:46:54 +02:00
|
|
|
Type string
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 16:46:54 +02:00
|
|
|
func (dma *AbstractAccessor) GetType() string {
|
|
|
|
return dma.Type
|
|
|
|
}
|
|
|
|
|
2024-07-19 10:54:58 +02:00
|
|
|
func (dma *AbstractAccessor) SetLogger(t DataType) {
|
2024-07-18 17:55:27 +02:00
|
|
|
dma.Logger = logs.CreateLogger(t.String(), "")
|
2024-07-18 16:46:54 +02:00
|
|
|
dma.Type = t.String()
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-19 10:54:58 +02:00
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
|
2024-07-19 11:00:15 +02:00
|
|
|
data.GenerateID()
|
2024-07-19 10:54:58 +02:00
|
|
|
err := validate.Struct(data)
|
|
|
|
if err != nil {
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, 422, err
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
id, code, err := mongo.MONGOService.StoreOne(data, wfa.GetType())
|
2024-07-19 10:54:58 +02:00
|
|
|
if err != nil {
|
|
|
|
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
|
|
|
return accessor.LoadOne(id)
|
|
|
|
}
|
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
|
|
|
|
res, code, err := accessor.LoadOne(id)
|
2024-07-18 15:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 15:35:30 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
|
2024-07-18 11:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
return res, 200, nil
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-19 11:27:58 +02:00
|
|
|
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, int, error) {
|
|
|
|
id, code, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
|
2024-07-18 11:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
return accessor.LoadOne(id)
|
|
|
|
}
|