-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
49 lines (42 loc) · 1.22 KB
/
store.go
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
package main
import (
"context"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
type ToDoStore struct {
Tasks map[int]*Task
IDCounter int // (ID given to a task before incrementing by 1)
Uncompleted int // number of tasks not yet completed
Completed int // number of tasks that have been completed
Deleted int // number of tasks deleted
}
func NewToDoStore() *ToDoStore {
return &ToDoStore{Tasks: map[int]*Task{}}
}
func (store *ToDoStore) Add(ctx context.Context, task *Task) {
span, ctx := tracer.StartSpanFromContext(ctx, "add_task")
defer span.Finish()
store.IDCounter += 1
store.Uncompleted += 1
task.ID = store.IDCounter
store.Tasks[task.ID] = task
}
func (store *ToDoStore) CompleteTask(ctx context.Context, taskID int) bool {
span, ctx := tracer.StartSpanFromContext(ctx, "complete_task")
defer span.Finish()
if task, ok := store.Tasks[taskID]; ok {
task.Completed = true
store.Completed += 1
store.Uncompleted -= 1
return true
}
return false
}
func (store *ToDoStore) DeleteTask(ctx context.Context, taskID int) {
span, ctx := tracer.StartSpanFromContext(ctx, "delete_task")
defer span.Finish()
if _, ok := store.Tasks[taskID]; ok {
delete(store.Tasks, taskID)
store.Deleted += 1
}
}