-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontexts.gno
184 lines (155 loc) · 4.04 KB
/
contexts.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// base implementation
package zentasktic
import (
"gno.land/p/demo/avl"
)
type Context struct {
Id string `json:"contextId"`
Name string `json:"contextName"`
}
type ZContextManager struct {
Contexts *avl.Tree
}
func NewZContextManager() *ZContextManager {
return &ZContextManager{
Contexts: avl.NewTree(),
}
}
// Actions
func (zcm *ZContextManager) AddContext(c Context) error {
if zcm.Contexts.Size() != 0 {
_, exist := zcm.Contexts.Get(c.Id)
if exist {
return ErrContextIdAlreadyExists
}
}
zcm.Contexts.Set(c.Id, c)
return nil
}
func (zcm *ZContextManager) EditContext(c Context) error {
if zcm.Contexts.Size() != 0 {
_, exist := zcm.Contexts.Get(c.Id)
if !exist {
return ErrContextIdNotFound
}
}
zcm.Contexts.Set(c.Id, c)
return nil
}
func (zcm *ZContextManager) RemoveContext(c Context) error {
if zcm.Contexts.Size() != 0 {
context, exist := zcm.Contexts.Get(c.Id)
if !exist {
return ErrContextIdNotFound
}
_, removed := zcm.Contexts.Remove(context.(Context).Id)
if !removed {
return ErrContextNotRemoved
}
}
return nil
}
func (zcm *ZContextManager) AddContextToTask(ztm *ZTaskManager, c Context, t Task) error {
taskInterface, exist := ztm.Tasks.Get(t.Id)
if !exist {
return ErrTaskIdNotFound
}
_, cexist := zcm.Contexts.Get(c.Id)
if !cexist {
return ErrContextIdNotFound
}
if t.RealmId == "2" {
task := taskInterface.(Task)
task.ContextId = c.Id
ztm.Tasks.Set(t.Id, task)
} else {
return ErrTaskNotEditable
}
return nil
}
func (zcm *ZContextManager) AddContextToProject(zpm *ZProjectManager, c Context, p Project) error {
projectInterface, exist := zpm.Projects.Get(p.Id)
if !exist {
return ErrProjectIdNotFound
}
_, cexist := zcm.Contexts.Get(c.Id)
if !cexist {
return ErrContextIdNotFound
}
if p.RealmId == "2" {
project := projectInterface.(Project)
project.ContextId = c.Id
zpm.Projects.Set(p.Id, project)
} else {
return ErrProjectNotEditable
}
return nil
}
func (zcm *ZContextManager) AddContextToProjectTask(zpm *ZProjectManager, c Context, p Project, projectTaskId string) error {
_, cexist := zcm.Contexts.Get(c.Id)
if !cexist {
return ErrContextIdNotFound
}
existingProjectInterface, exist := zpm.Projects.Get(p.Id)
if !exist {
return ErrProjectIdNotFound
}
existingProject := existingProjectInterface.(Project)
if existingProject.RealmId != "2" {
return ErrProjectNotEditable
}
existingProjectTasksInterface, texist := zpm.ProjectTasks.Get(p.Id)
if !texist {
return ErrProjectTasksNotFound
}
tasks, ok := existingProjectTasksInterface.([]Task)
if !ok {
return ErrProjectTasksNotFound
}
existingProject.Tasks = tasks
var index int = -1
for i, task := range existingProject.Tasks {
if task.Id == projectTaskId {
index = i
break
}
}
if index != -1 {
existingProject.Tasks[index].ContextId = c.Id
} else {
return ErrTaskByIdNotFound
}
zpm.ProjectTasks.Set(p.Id, existingProject.Tasks)
return nil
}
// getters
func (zcm *ZContextManager) GetContextById(contextId string) (Context, error) {
if zcm.Contexts.Size() != 0 {
cInterface, exist := zcm.Contexts.Get(contextId)
if exist {
return cInterface.(Context), nil
}
return Context{}, ErrContextIdNotFound
}
return Context{}, ErrContextIdNotFound
}
func (zcm *ZContextManager) GetAllContexts() (string) {
var allContexts []Context
// Iterate over the Contexts AVL tree to collect all Context objects.
zcm.Contexts.Iterate("", "", func(key string, value interface{}) bool {
if context, ok := value.(Context); ok {
allContexts = append(allContexts, context)
}
return false // Continue iteration until all nodes have been visited.
})
// Create a ContextsObject with all collected contexts.
contextsObject := &ContextsObject{
Contexts: allContexts,
}
// Use the custom MarshalJSON method to marshal the contexts into JSON.
marshalledContexts, merr := contextsObject.MarshalJSON()
if merr != nil {
return ""
}
return string(marshalledContexts)
}