-
Notifications
You must be signed in to change notification settings - Fork 1
/
collections.gno
324 lines (282 loc) · 9.03 KB
/
collections.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// base implementation
package zentasktic
import (
"gno.land/p/demo/avl"
)
type Collection struct {
Id string `json:"collectionId"`
RealmId string `json:"collectionRealmId"`
Name string `json:"collectionName"`
Tasks []Task `json:"collectionTasks"`
Projects []Project `json:"collectionProjects"`
}
type ZCollectionManager struct {
Collections *avl.Tree
CollectionTasks *avl.Tree
CollectionProjects *avl.Tree
}
func NewZCollectionManager() *ZCollectionManager {
return &ZCollectionManager{
Collections: avl.NewTree(),
CollectionTasks: avl.NewTree(),
CollectionProjects: avl.NewTree(),
}
}
// actions
func (zcolm *ZCollectionManager) AddCollection(c Collection) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if exist {
return ErrCollectionIdAlreadyExists
}
}
zcolm.Collections.Set(c.Id, c)
return nil
}
func (zcolm *ZCollectionManager) EditCollection(c Collection) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
}
zcolm.Collections.Set(c.Id, c)
return nil
}
func (zcolm *ZCollectionManager) RemoveCollection(c Collection) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
collectionInterface, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
collection := collectionInterface.(Collection)
_, removed := zcolm.Collections.Remove(collection.Id)
if !removed {
return ErrCollectionNotRemoved
}
if zcolm.CollectionTasks.Size() != 0 {
_, removedTasks := zcolm.CollectionTasks.Remove(collection.Id)
if !removedTasks {
return ErrCollectionNotRemoved
}
}
if zcolm.CollectionProjects.Size() != 0 {
_, removedProjects := zcolm.CollectionProjects.Remove(collection.Id)
if !removedProjects {
return ErrCollectionNotRemoved
}
}
}
return nil
}
func (zcolm *ZCollectionManager) AddProjectToCollection(zpm *ZProjectManager, c Collection, p Project) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
}
existingCollectionProjects, texist := zcolm.CollectionProjects.Get(c.Id)
if !texist {
// If the collections has no projects yet, initialize the slice.
existingCollectionProjects = []Project{}
} else {
projects, ok := existingCollectionProjects.([]Project)
if !ok {
return ErrCollectionsProjectsNotFound
}
existingCollectionProjects = projects
}
p.RealmId = "4"
if err := zpm.EditProject(p); err != nil {
return err
}
updatedProjects := append(existingCollectionProjects.([]Project), p)
zcolm.CollectionProjects.Set(c.Id, updatedProjects)
return nil
}
func (zcolm *ZCollectionManager) AddTaskToCollection(ztm *ZTaskManager, c Collection, t Task) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
}
existingCollectionTasks, texist := zcolm.CollectionTasks.Get(c.Id)
if !texist {
// If the collections has no tasks yet, initialize the slice.
existingCollectionTasks = []Task{}
} else {
tasks, ok := existingCollectionTasks.([]Task)
if !ok {
return ErrCollectionsTasksNotFound
}
existingCollectionTasks = tasks
}
t.RealmId = "4"
if err := ztm.EditTask(t); err != nil {
return err
}
updatedTasks := append(existingCollectionTasks.([]Task), t)
zcolm.CollectionTasks.Set(c.Id, updatedTasks)
return nil
}
func (zcolm *ZCollectionManager) RemoveProjectFromCollection(zpm *ZProjectManager, c Collection, p Project) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
}
existingCollectionProjects, texist := zcolm.CollectionProjects.Get(c.Id)
if !texist {
// If the collection has no projects yet, return appropriate error
return ErrCollectionsProjectsNotFound
}
// Find the index of the project to be removed.
var index int = -1
for i, project := range existingCollectionProjects.([]Project) {
if project.Id == p.Id {
index = i
break
}
}
// If the project was found, we remove it from the slice.
if index != -1 {
// by default we send it back to Assess
p.RealmId = "1"
zpm.EditProject(p)
existingCollectionProjects = append(existingCollectionProjects.([]Project)[:index], existingCollectionProjects.([]Project)[index+1:]...)
} else {
// Project not found in the collection
return ErrProjectByIdNotFound
}
zcolm.CollectionProjects.Set(c.Id, existingCollectionProjects)
return nil
}
func (zcolm *ZCollectionManager) RemoveTaskFromCollection(ztm *ZTaskManager, c Collection, t Task) (err error) {
// implementation
if zcolm.Collections.Size() != 0 {
_, exist := zcolm.Collections.Get(c.Id)
if !exist {
return ErrCollectionIdNotFound
}
}
existingCollectionTasks, texist := zcolm.CollectionTasks.Get(c.Id)
if !texist {
// If the collection has no tasks yet, return appropriate error
return ErrCollectionsTasksNotFound
}
// Find the index of the task to be removed.
var index int = -1
for i, task := range existingCollectionTasks.([]Task) {
if task.Id == t.Id {
index = i
break
}
}
// If the task was found, we remove it from the slice.
if index != -1 {
// by default, we send the task to Assess
t.RealmId = "1"
ztm.EditTask(t)
existingCollectionTasks = append(existingCollectionTasks.([]Task)[:index], existingCollectionTasks.([]Task)[index+1:]...)
} else {
// Task not found in the collection
return ErrTaskByIdNotFound
}
zcolm.CollectionTasks.Set(c.Id, existingCollectionTasks)
return nil
}
// getters
func (zcolm *ZCollectionManager) GetCollectionById(collectionId string) (Collection, error) {
if zcolm.Collections.Size() != 0 {
cInterface, exist := zcolm.Collections.Get(collectionId)
if exist {
collection := cInterface.(Collection)
// look for collection Tasks, Projects
existingCollectionTasks, texist := zcolm.CollectionTasks.Get(collectionId)
if texist {
collection.Tasks = existingCollectionTasks.([]Task)
}
existingCollectionProjects, pexist := zcolm.CollectionProjects.Get(collectionId)
if pexist {
collection.Projects = existingCollectionProjects.([]Project)
}
return collection, nil
}
return Collection{}, ErrCollectionByIdNotFound
}
return Collection{}, ErrCollectionByIdNotFound
}
func (zcolm *ZCollectionManager) GetCollectionTasks(c Collection) (tasks []Task, err error) {
if zcolm.CollectionTasks.Size() != 0 {
task, exist := zcolm.CollectionTasks.Get(c.Id)
if !exist {
// if there's no record in CollectionTasks, we don't have to return anything
return nil, ErrCollectionsTasksNotFound
} else {
// type assertion to convert interface{} to []Task
existingCollectionTasks, ok := task.([]Task)
if !ok {
return nil, ErrTaskFailedToAssert
}
return existingCollectionTasks, nil
}
}
return nil, nil
}
func (zcolm *ZCollectionManager) GetCollectionProjects(c Collection) (projects []Project, err error) {
if zcolm.CollectionProjects.Size() != 0 {
project, exist := zcolm.CollectionProjects.Get(c.Id)
if !exist {
// if there's no record in CollectionProjets, we don't have to return anything
return nil, ErrCollectionsProjectsNotFound
} else {
// type assertion to convert interface{} to []Projet
existingCollectionProjects, ok := project.([]Project)
if !ok {
return nil, ErrProjectFailedToAssert
}
return existingCollectionProjects, nil
}
}
return nil, nil
}
func (zcolm *ZCollectionManager) GetAllCollections() (collections string, err error) {
// implementation
var allCollections []Collection
// Iterate over the Collections AVL tree to collect all Project objects.
zcolm.Collections.Iterate("", "", func(key string, value interface{}) bool {
if collection, ok := value.(Collection); ok {
// get collection tasks, if any
collectionTasks, _ := zcolm.GetCollectionTasks(collection)
if collectionTasks != nil {
collection.Tasks = collectionTasks
}
// get collection prokects, if any
collectionProjects, _ := zcolm.GetCollectionProjects(collection)
if collectionProjects != nil {
collection.Projects = collectionProjects
}
allCollections = append(allCollections, collection)
}
return false // Continue iteration until all nodes have been visited.
})
// Create a CollectionsObject with all collected tasks.
collectionsObject := CollectionsObject{
Collections: allCollections,
}
// Use the custom MarshalJSON method to marshal the collections into JSON.
marshalledCollections, merr := collectionsObject.MarshalJSON()
if merr != nil {
return "", merr
}
return string(marshalledCollections), nil
}