51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
type AbstractAccessor struct {
|
|
Logger zerolog.Logger
|
|
Type string
|
|
}
|
|
|
|
func (dma *AbstractAccessor) GetType() string {
|
|
return dma.Type
|
|
}
|
|
|
|
func (dma *AbstractAccessor) Validate(d DBObject) error {
|
|
return validate.Struct(d)
|
|
}
|
|
|
|
func (dma *AbstractAccessor) SetLogger(t resources.ResourceType) {
|
|
dma.Logger = logs.CreateLogger(t.String(), "")
|
|
dma.Type = t.String()
|
|
}
|
|
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
|
|
res, err := accessor.LoadOne(id)
|
|
if err != nil {
|
|
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
|
return nil, err
|
|
}
|
|
_, 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, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, error) {
|
|
id, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
|
|
if err != nil {
|
|
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
|
}
|
|
return accessor.LoadOne(id)
|
|
}
|