-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
106 lines (89 loc) · 2.57 KB
/
data.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
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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/charmbracelet/bubbles/list"
"github.com/redis/go-redis/v9"
)
// Provides the mock data to fill the kanban board
func (b *Board) initLists() {
b.cols = []column{
newColumn(todo),
newColumn(inProgress),
newColumn(done),
}
// Init todos
todos, err := getTasksByStatus(todo)
if err != nil {
fmt.Printf("Error getting todo tasks: %v", err)
}
todoTaskItems := tasksToItems(todos)
board.cols[todo].list.Title = "To Do"
board.cols[todo].list.SetItems(todoTaskItems)
// Init in progress
inProgressItems, err := getTasksByStatus(inProgress)
if err != nil {
fmt.Printf("Error getting todo tasks: %v", err)
}
inProgressTaskItems := tasksToItems(inProgressItems)
board.cols[inProgress].list.Title = "In Progress"
board.cols[inProgress].list.SetItems(inProgressTaskItems)
// Init done
doneItems, err := getTasksByStatus(done)
if err != nil {
fmt.Printf("Error getting todo tasks: %v", err)
}
doneTaskItems := tasksToItems(doneItems)
board.cols[done].list.Title = "Done"
board.cols[done].list.SetItems(doneTaskItems)
}
func getTasksByStatus(status status) ([]Task, error) {
var filteredTasks []Task
// Fetch task IDs from the sorted set
taskIDs, err := client.ZRange(ctx, "tasks:sorted", 0, -1).Result()
if err != nil {
return nil, fmt.Errorf("error fetching task IDs from sorted set: %v", err)
}
// Fetch task details from the hash
commands, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
for _, taskID := range taskIDs {
// hashKey := fmt.Sprintf("task:%s", taskID) this step isn't required
pipe.HGet(ctx, "tasks", taskID)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error executing Redis pipeline: %v", err)
}
// Deserialize task data
for _, cmd := range commands {
data, err := cmd.(*redis.StringCmd).Bytes()
if err != nil {
if err == redis.Nil {
// Handle the case where the task does not exist in the hash
fmt.Println("Task data not found in Redis")
continue
}
return nil, fmt.Errorf("error getting task data from Redis: %v", err)
}
var task Task
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
if err := decoder.Decode(&task); err != nil {
return nil, fmt.Errorf("error decoding task data: %v", err)
}
// Filter tasks by the specified status
if task.Taskstatus == status {
filteredTasks = append(filteredTasks, task)
}
}
return filteredTasks, nil
}
func tasksToItems(tasks []Task) []list.Item {
var items []list.Item
for _, t := range tasks {
items = append(items, t)
}
return items
}