oc-lib/models/utils/abstracts.go
2024-07-18 15:35:30 +02:00

37 lines
1.0 KiB
Go

package utils
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog"
)
type AbstractAccessor struct {
Logger zerolog.Logger
}
func (dma *AbstractAccessor) SetLogger() {
dma.Logger = logs.CreateLogger("oclib", "")
}
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, "data")
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, "data")
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
}
return accessor.LoadOne(id)
}