-
Notifications
You must be signed in to change notification settings - Fork 0
/
workduration.gno
123 lines (100 loc) · 2.87 KB
/
workduration.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
package zentasktic_user
import (
"strconv"
"errors"
"time"
"gno.land/p/demo/avl"
"gno.land/p/demo/zentasktic"
)
type Workable interface {
// restrict implementation of Workable to this realm
assertWorkable()
}
type isWorkable struct {}
type WorkableTask struct {
zentasktic.Task
}
func (wt *WorkableTask) assertWorkable() {}
type WorkableProject struct {
zentasktic.Project
}
func (wp *WorkableProject) assertWorkable() {}
var _ Workable = &WorkableTask{}
var _ Workable = &WorkableProject{}
type WorkDuration struct {
Id string `json:"workDurationId"`
ObjectId string `json:"objectId"`
ObjectType string `json:"objectType"` // Task, Project
Duration time.Duration `json:"workDuration"`
Workable Workable `json:"-"`
}
var (
WorkDurations avl.Tree // wd.Id -> wd
)
func (wd WorkDuration) AddWorkDuration() (err error) {
if WorkDurations.Size() != 0 {
existingWD, exist := WorkDurations.Get(wd.Id)
if exist {
return ErrWorkDurationIdAlreadyExists
}
}
WorkDurations.Set(wd.Id, wd)
return nil
}
func (wd WorkDuration) EditWorkDuration() (err error) {
existingWorkDuration := WorkDuration{}
if WorkDurations.Size() != 0 {
existingWorkDuration, exist := WorkDurations.Get(wd.Id)
if !exist {
return ErrWorkDurationNotFound
}
}
WorkDurations.Set(wd.Id, wd)
return nil
}
func (wd WorkDuration) RemoveWorkDuration() (err error) {
// implementation
existingWorkDuration := WorkDuration{}
if WorkDurations.Size() != 0 {
existingWorkDuration, exist := WorkDurations.Get(wd.Id)
if !exist {
return ErrWorkDurationNotFound
}
}
_, removed := WorkDurations.Remove(wd.Id)
if !removed {
return ErrWorkDurationNotRemoved
}
return nil
}
// getters
func GetWorkDurationByOjectId(objectId string, objectType string) (wd WorkDuration, err error) {
// implementation
workDuration := WorkDuration{}
// Iterate over the WorkDuration AVL tree to see if we have a matching ObjectId.
WorkDurations.Iterate("", "", func(key string, value interface{}) bool {
if workDurationItem, ok := value.(WorkDuration); ok {
if workDurationItem.ObjectId == objectId && workDurationItem.ObjectType == objectType {
workDuration = workDurationItem
}
}
return false // Continue iteration until all nodes have been visited.
})
return workDuration, nil
}
func GetAllWorkDurations() (workDurations []WorkDuration, err error){
// implementation
var allWorkDurations []WorkDuration
// Iterate over the WorkDurations AVL tree to collect all WorkDurations objects.
if WorkDurations.Size() == 0 {
return nil, ErrWorkDurationsEmpty
} else {
WorkDurations.Iterate("", "", func(key string, value interface{}) bool {
if workDuration, ok := value.(WorkDuration); ok {
allWorkDurations = append(allWorkDurations, workDuration)
}
return false // Continue iteration until all nodes have been visited.
})
}
return allWorkDurations, nil
}