lightest + clearest code
This commit is contained in:
@@ -30,13 +30,20 @@ type AbstractObject struct {
|
||||
LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"`
|
||||
}
|
||||
|
||||
// GetID returns the id of the object (abstract)
|
||||
func (ao *AbstractObject) GetID() string {
|
||||
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
|
||||
return &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
}}
|
||||
}
|
||||
|
||||
// GetID implements ShallowDBObject.
|
||||
func (ao AbstractObject) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
// GetName returns the name of the object (abstract)
|
||||
func (ao *AbstractObject) GetName() string {
|
||||
// GetName implements ShallowDBObject.
|
||||
func (ao AbstractObject) GetName() string {
|
||||
return ao.Name
|
||||
}
|
||||
|
||||
@@ -81,7 +88,7 @@ func (r *AbstractObject) GenerateID() {
|
||||
|
||||
type AbstractAccessor struct {
|
||||
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
||||
Type string // Type is the data type of the accessor
|
||||
Type tools.DataType // Type is the data type of the accessor
|
||||
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
||||
PeerID string // PeerID is the id of the peer
|
||||
Groups []string // Groups is the list of groups that can access the accessor
|
||||
@@ -93,7 +100,7 @@ func (dma *AbstractAccessor) VerifyAuth() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) GetType() string {
|
||||
func (dma *AbstractAccessor) GetType() tools.DataType {
|
||||
return dma.Type
|
||||
}
|
||||
|
||||
@@ -117,13 +124,13 @@ func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (
|
||||
},
|
||||
}
|
||||
if cursor, _, _ := accessor.Search(&f, ""); len(cursor) > 0 {
|
||||
return nil, 409, errors.New(accessor.GetType() + " with name " + data.GetName() + " already exists")
|
||||
return nil, 409, errors.New(accessor.GetType().String() + " with name " + data.GetName() + " already exists")
|
||||
}
|
||||
err := validate.Struct(data)
|
||||
if err != nil {
|
||||
return nil, 422, err
|
||||
}
|
||||
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType())
|
||||
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
@@ -138,7 +145,7 @@ func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBO
|
||||
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
|
||||
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType().String())
|
||||
if err != nil {
|
||||
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
@@ -159,7 +166,7 @@ func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor
|
||||
for k, v := range change { // apply the changes, with a flatten method
|
||||
loaded[k] = v
|
||||
}
|
||||
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, accessor.GetType())
|
||||
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, accessor.GetType().String())
|
||||
if err != nil {
|
||||
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
@@ -169,7 +176,7 @@ func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor
|
||||
|
||||
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), wfa Accessor) (DBObject, int, error) {
|
||||
var data T
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType().String())
|
||||
if !data.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) {
|
||||
return nil, 403, errors.New("You are not allowed to access this collaborative area")
|
||||
}
|
||||
@@ -202,7 +209,7 @@ func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBO
|
||||
}
|
||||
|
||||
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
@@ -211,14 +218,14 @@ func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilte
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = defaultFilters
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||
func (dma *AbstractAccessor) GenericRawUpdateOne(set DBObject, id string, accessor Accessor) (DBObject, int, error) {
|
||||
id, code, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
|
||||
id, code, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType().String())
|
||||
if err != nil {
|
||||
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
|
@@ -29,7 +29,7 @@ type DBObject interface {
|
||||
|
||||
// Accessor is an interface that defines the basic methods for an Accessor
|
||||
type Accessor interface {
|
||||
GetType() string
|
||||
GetType() tools.DataType
|
||||
GetPeerID() string
|
||||
GetGroups() []string
|
||||
GetLogger() *zerolog.Logger
|
||||
|
Reference in New Issue
Block a user