-
Notifications
You must be signed in to change notification settings - Fork 1
/
actor.gno
297 lines (248 loc) · 7.66 KB
/
actor.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
package zentasktic_project
import (
"errors"
"gno.land/p/demo/zentasktic"
)
// AddActor adds a new actor to the Actors tree
func (a *Actor) AddActor() error {
if _, err := GetActorById(a.Id); err == nil {
// actor found, err is nil
return errors.New("actor ID already exists")
}
Actors.Set(a.Id, a)
return nil
}
// RemoveActor removes an actor from the Actors tree and cleans up associated data
func (a *Actor) RemoveActor() error {
_, err := GetActorById(a.Id)
// if err != nil, actor was not found
if err != nil {
return errors.New("actor ID not found")
}
// Remove actor from Actors tree
Actors.Remove(a.Id)
// Remove actor from any teams they belong to
Teams.Iterate("", "", func(key string, value interface{}) bool {
team := value.(*Team)
for i, memberId := range team.Members {
if memberId == a.Id {
team.Members = append(team.Members[:i], team.Members[i+1:]...)
break
}
}
return false // Continue iteration
})
// Remove actor's tasks and projects
ActorTasks.Remove(a.Id)
ActorProjects.Remove(a.Id)
return nil
}
// EditActor updates an existing actor's details in the Actors tree
func (a *Actor) EditActor() error {
_, err := GetActorById(a.Id)
if err != nil {
return errors.New("actor ID not found")
}
Actors.Set(a.Id, a)
return nil
}
func (a *Actor) AddActorToTeam(t *Team) error {
// Check if the actor exists
_, err := GetActorById(a.Id)
if err != nil {
return errors.New("actor ID not found")
}
// Check if the team exists
existingTeam, terr := GetTeamById(t.Id)
if terr != nil {
return errors.New("team ID not found")
}
// Check if the actor is already a member of the team
for _, memberId := range existingTeam.Members {
if memberId == a.Id {
return nil // Actor is already a member of the team
}
}
// Add the actor's ID to the team
existingTeam.Members = append(existingTeam.Members, a.Id)
Teams.Set(existingTeam.Id, existingTeam) // Update the team in the tree
return nil
}
func (a *Actor) RemoveActorFromTeam(t *Team) error {
// Check if the team exists
existingTeam, terr := GetTeamById(t.Id)
if terr != nil {
return errors.New("team ID not found")
}
// Find and remove the actor's ID from the team's members list
for i, memberId := range existingTeam.Members {
if memberId == a.Id {
existingTeam.Members = append(existingTeam.Members[:i], existingTeam.Members[i+1:]...)
Teams.Set(existingTeam.Id, existingTeam) // Update the team in the tree
return nil
}
}
return errors.New("actor not found in team")
}
// AssignActorToTask assigns a task to an actor
func (a *Actor) AssignActorToTask(wt *WorkableTask) error {
// Add the task to the actor's task list
tasks, _ := ActorTasks.Get(a.Id)
var updatedTasks []*WorkableTask
if tasks != nil {
updatedTasks = tasks.([]*WorkableTask)
}
for _, task := range updatedTasks {
if task.Id == wt.Id {
return nil // Task already assigned to actor
}
}
updatedTasks = append(updatedTasks, wt)
ActorTasks.Set(a.Id, updatedTasks)
return nil
}
// UnassignActorFromTask unassigns a task from an actor
func (a *Actor) UnassignActorFromTask(wt *WorkableTask) error {
tasks, exist := ActorTasks.Get(a.Id)
if !exist {
return errors.New("actor task not found")
}
updatedTasks := tasks.([]*WorkableTask)
for i, task := range updatedTasks {
if task.Id == wt.Id {
updatedTasks = append(updatedTasks[:i], updatedTasks[i+1:]...)
if len(updatedTasks) == 0 {
ActorTasks.Remove(a.Id)
} else {
ActorTasks.Set(a.Id, updatedTasks)
}
return nil
}
}
return errors.New("actor task not found")
}
// AssignActorToProject assigns a project to an actor
func (a *Actor) AssignActorToProject(wp *WorkableProject) error {
// Add the project to the actor's project list
projects, _ := ActorProjects.Get(a.Id)
var updatedProjects []*WorkableProject
if projects != nil {
updatedProjects = projects.([]*WorkableProject)
}
for _, project := range updatedProjects {
if project.Id == wp.Id {
return nil // Project already assigned to actor
}
}
updatedProjects = append(updatedProjects, wp)
ActorProjects.Set(a.Id, updatedProjects)
return nil
}
// UnassignActorFromProject unassigns a project from an actor
func (a *Actor) UnassignActorFromProject(wp *WorkableProject) error {
projects, exist := ActorProjects.Get(a.Id)
if !exist {
return errors.New("actor project not found")
}
updatedProjects := projects.([]*WorkableProject)
for i, project := range updatedProjects {
if project.Id == wp.Id {
updatedProjects = append(updatedProjects[:i], updatedProjects[i+1:]...)
if len(updatedProjects) == 0 {
ActorProjects.Remove(a.Id)
} else {
ActorProjects.Set(a.Id, updatedProjects)
}
return nil
}
}
return errors.New("actor project not found")
}
// GetActorById retrieves an actor by ID
// GetActorById retrieves an actor by ID
func GetActorById(actorId string) (*Actor, error) {
actor, exist := Actors.Get(actorId)
if !exist {
return nil, errors.New("actor ID not found")
}
a, ok := actor.(*Actor)
if !ok {
return nil, errors.New("type assertion to *Actor failed")
}
return a, nil
}
// GetAllActors retrieves all actors and returns them as a JSON string
func GetAllActors() string {
actors := make([]*Actor, 0)
Actors.Iterate("", "", func(key string, value interface{}) bool {
if a, ok := value.(*Actor); ok {
actors = append(actors, a)
}
return false // Continue iteration
})
actorsObject := &ActorsObject{Actors: actors}
marshalledActors, err := actorsObject.MarshalJSON()
if err != nil {
return ""
}
return string(marshalledActors)
}
// GetActorTeams retrieves all teams associated with an actor
func (a *Actor) GetActorTeams() (string, error) {
var actorTeams []*Team
Teams.Iterate("", "", func(key string, value interface{}) bool {
team := value.(*Team)
for _, memberId := range team.Members {
if memberId == a.Id {
actorTeams = append(actorTeams, team)
break
}
}
return false // Continue iteration
})
if len(actorTeams) == 0 {
return "", errors.New("actor teams not found")
}
teamsObject := &TeamsObject{Teams: actorTeams}
marshalledTeams, err := teamsObject.MarshalJSON()
if err != nil {
return "", err
}
return string(marshalledTeams), nil
}
// GetActorTasks retrieves all tasks associated with an actor
func (a *Actor) GetActorTasks() (string, error) {
// Retrieve tasks associated with the actor's ID
tasks, exist := ActorTasks.Get(a.Id)
if !exist {
return "", errors.New("actor tasks not found")
}
// Create a TasksObject to hold the list of tasks
tasksObject := zentasktic.TasksObject{Tasks: make([]zentasktic.Task, 0)}
// Iterate through the slice of WorkableTask and append the Task to tasksObject.Tasks
for _, task := range tasks.([]*WorkableTask) {
tasksObject.Tasks = append(tasksObject.Tasks, task.Task)
}
// Marshal the tasks into JSON format
marshalledTasks, err := tasksObject.MarshalJSON()
if err != nil {
return "", err
}
return string(marshalledTasks), nil
}
// GetActorProjects retrieves all projects associated with an actor
func (a *Actor) GetActorProjects() (string, error) {
projects, exist := ActorProjects.Get(a.Id)
if !exist {
return "", errors.New("actor projects not found")
}
projectsObject := zentasktic.ProjectsObject{Projects: make([]zentasktic.Project, 0)}
for _, project := range projects.([]WorkableProject) {
projectsObject.Projects = append(projectsObject.Projects, project.Project)
}
marshalledProjects, err := projectsObject.MarshalJSON()
if err != nil {
return "", err
}
return string(marshalledProjects), nil
}