oc-lib/models/utils/abstracts.go
2024-07-18 17:55:27 +02:00

44 lines
1.2 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/rs/zerolog"
)
type AbstractAccessor struct {
Logger zerolog.Logger
Type string
}
func (dma *AbstractAccessor) GetType() string {
return dma.Type
}
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)
}