94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package peer
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
)
|
|
|
|
type peerMongoAccessor struct {
|
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
|
}
|
|
|
|
// New creates a new instance of the peerMongoAccessor
|
|
func NewShallow() *peerMongoAccessor {
|
|
return &peerMongoAccessor{
|
|
utils.AbstractAccessor{
|
|
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
|
Type: tools.PEER,
|
|
},
|
|
}
|
|
}
|
|
|
|
func New(t tools.DataType, username string, peerID string, groups []string, caller *tools.HTTPCaller) *peerMongoAccessor {
|
|
return &peerMongoAccessor{
|
|
utils.AbstractAccessor{
|
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
|
Caller: caller,
|
|
PeerID: peerID,
|
|
User: username,
|
|
Groups: groups, // Set the caller
|
|
Type: t,
|
|
},
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Nothing special here, just the basic CRUD operations
|
|
*/
|
|
|
|
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
return utils.GenericDeleteOne(id, wfa)
|
|
}
|
|
|
|
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
return utils.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
|
|
}
|
|
|
|
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
return utils.GenericStoreOne(data.(*Peer), wfa)
|
|
}
|
|
|
|
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
return utils.GenericStoreOne(data, wfa)
|
|
}
|
|
|
|
func (dca *peerMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
return utils.GenericLoadOne[*Peer](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
return d, 200, nil
|
|
}, dca)
|
|
}
|
|
|
|
func (wfa *peerMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
|
return utils.GenericLoadAll[*Peer](func(d utils.DBObject) utils.ShallowDBObject {
|
|
return d
|
|
}, wfa)
|
|
}
|
|
|
|
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
|
return utils.GenericSearch[*Peer](filters, search, wfa.getDefaultFilter(search),
|
|
func(d utils.DBObject) utils.ShallowDBObject {
|
|
return d
|
|
}, wfa)
|
|
}
|
|
func (a *peerMongoAccessor) getDefaultFilter(search string) *dbs.Filters {
|
|
s, err := strconv.Atoi(search)
|
|
if err == nil {
|
|
return &dbs.Filters{
|
|
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
|
"state": {{Operator: dbs.EQUAL.String(), Value: s}},
|
|
},
|
|
}
|
|
} else {
|
|
return &dbs.Filters{
|
|
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"url": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
},
|
|
}
|
|
}
|
|
}
|