This commit is contained in:
mr
2024-11-29 10:25:42 +01:00
parent 8d701e67d8
commit b591533ba4
15 changed files with 320 additions and 387 deletions

View File

@@ -32,56 +32,56 @@ func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCal
// DeleteOne deletes a workspace from the database, given its ID, it automatically share to peers if the workspace is shared
// it checks if a workspace with the same name already exists
func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
res, code, err := wfa.GenericDeleteOne(id, wfa)
func (a *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
res, code, err := utils.GenericDeleteOne(id, a)
if code == 200 && res != nil {
wfa.share(res.(*Workspace), tools.DELETE, wfa.Caller) // Share the deletion to the peers
a.share(res.(*Workspace), tools.DELETE, a.Caller) // Share the deletion to the peers
}
return res, code, err
}
// UpdateOne updates a workspace in the database, given its ID, it automatically share to peers if the workspace is shared
func (wfa *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
func (a *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
d := set.(*Workspace) // Get the workspace from the set
d.Clear()
if d.Active { // If the workspace is active, deactivate all the other workspaces
res, _, err := wfa.LoadAll()
res, _, err := a.LoadAll()
if err == nil {
for _, r := range res {
if r.GetID() != id {
r.(*Workspace).Active = false
wfa.UpdateOne(r.(*Workspace), r.GetID())
a.UpdateOne(r.(*Workspace), r.GetID())
}
}
}
}
res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
res, code, err := utils.GenericUpdateOne(set, id, a, &Workspace{})
if code == 200 && res != nil {
wfa.share(res.(*Workspace), tools.PUT, wfa.Caller)
a.share(res.(*Workspace), tools.PUT, a.Caller)
}
return res, code, err
}
// StoreOne stores a workspace in the database, it checks if a workspace with the same name already exists
func (wfa *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
func (a *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: data.GetName() + "_workspace"}},
},
}
res, _, err := wfa.Search(filters, "") // Search for the workspace
if err == nil && len(res) > 0 { // If the workspace already exists, return an error
res, _, err := a.Search(filters, "") // Search for the workspace
if err == nil && len(res) > 0 { // If the workspace already exists, return an error
return nil, 409, errors.New("A workspace with the same name already exists")
}
// reset the resources
d := data.(*Workspace)
d.Clear()
return wfa.GenericStoreOne(d, wfa)
return utils.GenericStoreOne(d, a)
}
// CopyOne copies a workspace in the database
func (wfa *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
func (a *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return utils.GenericStoreOne(data, a)
}
func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
@@ -91,30 +91,30 @@ func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error)
}, a)
}
func (wfa *workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
func (a *workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
return utils.GenericLoadAll[*Workspace](func(d utils.DBObject) utils.ShallowDBObject {
d.(*Workspace).Fill(wfa.PeerID, wfa.Groups)
d.(*Workspace).Fill(a.PeerID, a.Groups)
return d
}, wfa)
}, a)
}
func (wfa *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
func (a *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
return utils.GenericSearch[*Workspace](filters, search, (&Workspace{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject {
d.(*Workspace).Fill(wfa.PeerID, wfa.Groups)
d.(*Workspace).Fill(a.PeerID, a.Groups)
return d
}, wfa)
}, a)
}
/*
This function is used to share the workspace with the peers
*/
func (wfa *workspaceMongoAccessor) share(realData *Workspace, method tools.METHOD, caller *tools.HTTPCaller) {
func (a *workspaceMongoAccessor) share(realData *Workspace, method tools.METHOD, caller *tools.HTTPCaller) {
fmt.Println("Sharing workspace", realData, caller)
if realData == nil || realData.Shared == "" || caller == nil || caller.Disabled {
return
}
shallow := &shallow_collaborative_area.ShallowCollaborativeArea{}
access := (shallow).GetAccessor(wfa.PeerID, wfa.Groups, nil)
access := (shallow).GetAccessor(a.PeerID, a.Groups, nil)
res, code, _ := access.LoadOne(realData.Shared)
if code != 200 {
return
@@ -135,6 +135,6 @@ func (wfa *workspaceMongoAccessor) share(realData *Workspace, method tools.METHO
}
}
if err != nil {
wfa.Logger.Error().Msg(err.Error())
a.Logger.Error().Msg(err.Error())
}
}