oc-lib/models/peer/peer_mongo_accessor.go

93 lines
2.7 KiB
Go
Raw Normal View History

2024-08-12 12:03:58 +02:00
package peer
import (
2024-10-30 10:16:13 +01:00
"strconv"
2024-08-12 12:03:58 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
2024-11-28 13:19:01 +01:00
"cloud.o-forge.io/core/oc-lib/logs"
2024-08-12 12:03:58 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-11-28 13:19:01 +01:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 12:03:58 +02:00
)
type peerMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
2024-08-12 12:03:58 +02:00
}
// New creates a new instance of the peerMongoAccessor
2024-11-28 13:19:01 +01:00
func NewShallow() *peerMongoAccessor {
return &peerMongoAccessor{
utils.AbstractAccessor{
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
2024-11-28 16:49:41 +01:00
Type: tools.PEER,
2024-11-28 13:19:01 +01:00
},
}
}
func New(t tools.DataType, 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,
Groups: groups, // Set the caller
2024-11-28 16:49:41 +01:00
Type: t,
2024-11-28 13:19:01 +01:00
},
}
2024-08-12 12:03:58 +02:00
}
/*
* Nothing special here, just the basic CRUD operations
*/
2024-08-12 12:03:58 +02:00
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
}
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data.(*Peer), wfa)
}
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
2024-11-28 16:49:41 +01:00
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)
2024-08-12 12:03:58 +02:00
}
2024-11-28 16:49:41 +01:00
func (wfa *peerMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
return utils.GenericLoadAll[*Peer](func(d utils.DBObject) utils.ShallowDBObject {
return d
}, wfa)
2024-08-12 12:03:58 +02:00
}
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
2024-11-28 16:49:41 +01:00
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}},
},
2024-08-12 12:03:58 +02:00
}
}
}