COMPLEX SEARCH

This commit is contained in:
mr
2024-08-01 09:13:10 +02:00
parent ad455e0e3a
commit 924a688a9d
14 changed files with 240 additions and 90 deletions

View File

@@ -3,6 +3,7 @@ package utils
import (
"errors"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/go-playground/validator/v10"
@@ -45,7 +46,19 @@ func (dma *AbstractAccessor) SetLogger(t DataType) {
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
data.GenerateID()
if cursor, _, _ := accessor.Search(data.GetName(), "abstractresource.abstractobject.name", "abstractobject.name"); len(cursor) > 0 {
f := dbs.Filters{
Or: map[string]dbs.Filter{
"abstractresource.abstractobject.name": {
Operator: dbs.LIKE.String(),
Value: data.GetName(),
},
"abstractobject.name": {
Operator: dbs.LIKE.String(),
Value: data.GetName(),
},
},
}
if cursor, _, _ := accessor.Search(&f, ""); len(cursor) > 0 {
return nil, 409, errors.New(accessor.GetType() + " with name " + data.GetName() + " already exists")
}
err := validate.Struct(data)

View File

@@ -1,5 +1,7 @@
package utils
import "cloud.o-forge.io/core/oc-lib/dbs"
type ShallowDBObject interface {
GenerateID()
GetID() string
@@ -18,7 +20,7 @@ type DBObject interface {
type Accessor interface {
SetLogger(t DataType)
GetType() string
Search(word string, options ...string) ([]ShallowDBObject, int, error)
Search(filters *dbs.Filters, search string) ([]ShallowDBObject, int, error)
LoadAll() ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error)