-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
85 lines (58 loc) · 2.35 KB
/
database.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
package main
import (
"log"
"os"
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool")
var db *pgxpool.Pool
func initDb() {
var err error
db, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
logError("initDb", err)
}
//************************************ Task CRUD ****************************************
func createTask(name string, user_id int, category_id int) error {
_, err := db.Exec(context.Background(), "insert into tasks(name, user_id, category_id) values($1, $2, $3)", name, user_id, category_id)
return err
}
func readTasks(user_id int) (pgx.Rows, error) {
rows, err := db.Query(context.Background(), "select * from tasks where user_id=$1 and done=$2", user_id, false)
return rows, err
}
func updateTaskDone(task_id int, done bool) error {
log.Print(done)
_, err := db.Exec(context.Background(), "update tasks set done=$1 where task_id=$2", done, task_id)
return err
}
// func deleteTask(task_id int) error { //unused for now
// _, err := db.Exec(context.Background(), "delete from tasks where task_id=$1", task_id)
// return err
// }
func deleteDoneTasks(user_id int) error {
_, err := db.Exec(context.Background(), "delete from tasks where done=$1 and user_id=$2", true, user_id)
return err
}
//************************************ Category CRUD ****************************************
func createCategory(name string, color int, user_id int) error {
_, err := db.Exec(context.Background(), "insert into categories(name, color, user_id) values($1, $2, $3)", name, color, user_id)
return err
}
func readCategories(user_id int) (pgx.Rows, error) {
rows, err := db.Query(context.Background(), "select * from categories where user_id=$1", user_id)
return rows, err
}
func deleteCategory(category_id int) error {
_, err := db.Exec(context.Background(), "delete from categories where category_id=$1", category_id)
return err
}
//************************************ User CRUD ****************************************
func createUser(username string, password string) error {
_, err := db.Exec(context.Background(), "insert into users(username, password) values($1, $2)", username, hash(password))
return err
}
func readUser(username string) (pgx.Rows, error) {
log.Print(username)
rows, err := db.Query(context.Background(), "select * from users where username=$1", username)
return rows, err
}