add shared workspace logic
This commit is contained in:
		
							
								
								
									
										52
									
								
								models/workspace/shared/rules/rule/rule.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								models/workspace/shared/rules/rule/rule.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| package rule  | ||||
|  | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"cloud.o-forge.io/core/oc-lib/models/utils" | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| type Rule struct { | ||||
| 	utils.AbstractObject | ||||
| 	Description string `json:"description,omitempty" bson:"description,omitempty"` | ||||
| 	Condition string `json:"condition,omitempty" bson:"condition,omitempty"` | ||||
| 	Actions []string `json:"actions,omitempty" bson:"actions,omitempty"` | ||||
| } | ||||
|  | ||||
| func (ao *Rule) GetID() string { | ||||
| 	return ao.UUID | ||||
| } | ||||
|  | ||||
| func (r *Rule) GenerateID() { | ||||
| 	r.UUID = uuid.New().String() | ||||
| } | ||||
|  | ||||
| func (d *Rule) GetName() string { | ||||
| 	return d.Name | ||||
| } | ||||
|  | ||||
| func (d *Rule) GetAccessor() utils.Accessor { | ||||
| 	data := New() | ||||
| 	data.SetLogger(utils.RULE) | ||||
| 	return data | ||||
| } | ||||
|  | ||||
| func (dma *Rule) Deserialize(j map[string]interface{}) utils.DBObject { | ||||
| 	b, err := json.Marshal(j) | ||||
| 	if err != nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	json.Unmarshal(b, dma) | ||||
| 	return dma | ||||
| } | ||||
|  | ||||
| func (dma *Rule) Serialize() map[string]interface{} { | ||||
| 	var m map[string]interface{} | ||||
| 	b, err := json.Marshal(dma) | ||||
| 	if err != nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	json.Unmarshal(b, &m) | ||||
| 	return m | ||||
| } | ||||
							
								
								
									
										83
									
								
								models/workspace/shared/rules/rule/rule_mongo_accessor.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								models/workspace/shared/rules/rule/rule_mongo_accessor.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,83 @@ | ||||
| package rule  | ||||
|  | ||||
| import ( | ||||
| 	"cloud.o-forge.io/core/oc-lib/dbs" | ||||
| 	"cloud.o-forge.io/core/oc-lib/dbs/mongo" | ||||
| 	"cloud.o-forge.io/core/oc-lib/models/utils" | ||||
| ) | ||||
|  | ||||
| type ruleMongoAccessor struct { | ||||
| 	utils.AbstractAccessor | ||||
| } | ||||
|  | ||||
| func New() *ruleMongoAccessor { | ||||
| 	return &ruleMongoAccessor{} | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) { | ||||
| 	return wfa.GenericDeleteOne(id, wfa) | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) { | ||||
| 	return wfa.GenericUpdateOne(set.(*Rule), id, wfa, &Rule{}) | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) { | ||||
| 	return wfa.GenericStoreOne(data.(*Rule), wfa) | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) { | ||||
| 	return wfa.GenericStoreOne(data, wfa) | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) { | ||||
| 	var rule Rule | ||||
| 	res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType()) | ||||
| 	if err != nil { | ||||
| 		wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) | ||||
| 		return nil, code, err | ||||
| 	} | ||||
| 	res_mongo.Decode(&rule) | ||||
| 	return &rule, 200, nil | ||||
| } | ||||
|  | ||||
| func (wfa ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) { | ||||
| 	objs := []utils.ShallowDBObject{} | ||||
| 	res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType()) | ||||
| 	if err != nil { | ||||
| 		wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error()) | ||||
| 		return nil, code, err | ||||
| 	} | ||||
| 	var results []Rule | ||||
| 	if err = res_mongo.All(mongo.MngoCtx, &results); err != nil { | ||||
| 		return nil, 404, err | ||||
| 	} | ||||
| 	for _, r := range results { | ||||
| 		objs = append(objs, &r) | ||||
| 	} | ||||
| 	return objs, 200, nil | ||||
| } | ||||
|  | ||||
| func (wfa *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) { | ||||
| 	objs := []utils.ShallowDBObject{} | ||||
| 	if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" { | ||||
| 		filters = &dbs.Filters{ | ||||
| 			Or: map[string][]dbs.Filter{ | ||||
| 				"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}}, | ||||
| 			}, | ||||
| 		} | ||||
| 	} | ||||
| 	res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType()) | ||||
| 	if err != nil { | ||||
| 		wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error()) | ||||
| 		return nil, code, err | ||||
| 	} | ||||
| 	var results []Rule | ||||
| 	if err = res_mongo.All(mongo.MngoCtx, &results); err != nil { | ||||
| 		return nil, 404, err | ||||
| 	} | ||||
| 	for _, r := range results { | ||||
| 		objs = append(objs, &r) | ||||
| 	} | ||||
| 	return objs, 200, nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user