oc-lib/models/peer/peer_mongo_accessor.go

91 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
func NewShallowAccessor() *peerMongoAccessor {
2024-11-28 13:19:01 +01:00
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 NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
2024-11-28 13:19:01 +01:00
return &peerMongoAccessor{
utils.AbstractAccessor{
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
Request: request,
Type: tools.PEER,
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) {
2024-11-29 10:25:42 +01:00
return utils.GenericDeleteOne(id, wfa)
2024-08-12 12:03:58 +02:00
}
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
2024-11-29 10:25:42 +01:00
return utils.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
2024-08-12 12:03:58 +02:00
}
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
2024-11-29 10:25:42 +01:00
return utils.GenericStoreOne(data.(*Peer), wfa)
2024-08-12 12:03:58 +02:00
}
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
2024-11-29 10:25:42 +01:00
return utils.GenericStoreOne(data, wfa)
2024-08-12 12:03:58 +02:00
}
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
}
func (wfa *peerMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
2024-11-28 16:49:41 +01:00
return utils.GenericLoadAll[*Peer](func(d utils.DBObject) utils.ShallowDBObject {
return d
}, isDraft, wfa)
2024-08-12 12:03:58 +02:00
}
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]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
}, isDraft, wfa)
2024-11-28 16:49:41 +01:00
}
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
}
}
}