-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
169 lines (149 loc) · 3.43 KB
/
tasks.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
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
package goodidea
import (
"context"
"fmt"
"log"
"log/slog"
"time"
"github.com/jackc/pgx/v5"
)
type Task struct {
ID uint32 `json:"id"`
Status bool `json:"status"`
Title string `json:"title"`
Body *string `json:"body"`
Score int32 `json:"score"`
CompletedAt *time.Time `json:"completedAt"`
CreatedAt time.Time `json:"createdAt"`
DeletedAt time.Time `json:"deletedAt"`
Comments []Comment `json:"comments"`
Images []string `json:"images"` //paths to get all of the images associated with task
}
func getAllTasks(limit int16) ([]Task, error) {
ctx := context.Background()
query := `SELECT
id,
title,
-- Don't return the entire body, this is for a summary page
SUBSTRING(body, 1, 256) AS body,
score
FROM
tasks
WHERE
status = false AND
deleted_at IS NULL
ORDER BY
score DESC`
if limit > 0 {
query += fmt.Sprintf("\nLIMIT %d", limit)
}
tasks := make([]Task, 0, 8)
rows, err := DB.Query(ctx, query)
if err != nil {
if err == pgx.ErrNoRows {
return tasks, nil
}
return tasks, err
}
defer rows.Close()
for rows.Next() {
var task Task
err = rows.Scan(
&task.ID,
&task.Title,
&task.Body,
&task.Score,
)
if err != nil {
log.Println("Unable to marshal DB response into struct")
return tasks, err
}
tasks = append(tasks, task)
}
return tasks, nil
}
func getSomeTasks(clue string) ([]Task, error) {
ctx := context.Background()
query := `SELECT
id,
title,
-- Don't return the entire body, this is for a summary page
SUBSTRING(body, 1, 256) AS body,
score
FROM
tasks
WHERE
deleted_at IS NULL AND
title ilike '%' || $1 || '%'
ORDER BY
score DESC`
tasks := make([]Task, 0, 8)
rows, err := DB.Query(ctx, query, clue)
if err != nil {
if err == pgx.ErrNoRows {
return tasks, nil
}
return tasks, err
}
defer rows.Close()
for rows.Next() {
var task Task
err = rows.Scan(
&task.ID,
&task.Title,
&task.Body,
&task.Score,
)
if err != nil {
log.Println("Unable to marshal DB response into struct")
return tasks, err
}
tasks = append(tasks, task)
}
return tasks, nil
}
func getTasksByID(id uint32) (Task, error) {
ctx := context.Background()
query := "SELECT id, status, title, body, score, completed_at, created_at FROM tasks WHERE id = $1 and deleted_at IS NULL"
var task Task
if err := DB.QueryRow(ctx, query, id).Scan(
&task.ID,
&task.Status,
&task.Title,
&task.Body,
&task.Score,
&task.CompletedAt,
&task.CreatedAt,
); err != nil {
slog.Error("Could not query task from DB", "err", err)
return task, err
}
return task, nil
}
func addTask(title, details string) (uint32, error) {
ctx := context.Background()
query := "INSERT INTO tasks(title, body) VALUES($1, $2) RETURNING id"
var newID uint32
if err := DB.QueryRow(ctx, query, title, details).Scan(&newID); err != nil {
return 0, err
}
return newID, nil
}
func updateTaskScore(id uint32, val int8) (int32, error) {
var score int32
ctx := context.Background()
query := "UPDATE tasks SET score = score + $2 WHERE id = $1"
if _, err := DB.Exec(ctx, query, id, val); err != nil {
return 0, err
}
if err := DB.QueryRow(ctx, "SELECT score FROM tasks WHERE id = $1", id).Scan(&score); err != nil {
return 0, err
}
return score, nil
}
func toggleStatus(id uint32) error {
ctx := context.Background()
query := "UPDATE tasks SET status = NOT status WHERE id = $1"
_, err := DB.Exec(ctx, query, id)
return err
}