-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
109 lines (87 loc) · 2.26 KB
/
db.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
package main
import (
"log"
"fmt"
"database/sql"
_ "github.com/lib/pq"
)
type PostgresConnection struct {
Host string
Port int
User string
Password string
DBName string
}
type Assignment struct {
ID int
Name string
Due int64
}
func dbConnection() *sql.DB {
connectionString := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", ConnectionSecrets.Host, ConnectionSecrets.Port, ConnectionSecrets.User, ConnectionSecrets.Password, ConnectionSecrets.DBName)
/* validate connection */
db, err := sql.Open("postgres", connectionString)
if err != nil { log.Panic(err) }
err = db.Ping()
if err != nil { log.Panic(err) }
return db
}
/* initializes db with all the tables */
func DBInit() error {
db := dbConnection()
defer db.Close()
query := `
CREATE TYPE privledge_enum AS ENUM ('STUDENT', 'INSTRUCTOR');
CREATE TABLE users(
id serial primary key,
guildid varchar(64) not null,
discordid varchar(64) not null,
privledge privledge_enum not null
);
CREATE TABLE assignments(
id serial primary key,
guildid varchar(64) not null,
name varchar(256) not null,
due bigint not null
);
`
_, err := db.Exec(query)
return err
}
/* wipes the db */
func DBPurge() {
}
func DBNewStudent(guildID string, discordID string) (int, error) {
db := dbConnection()
defer db.Close()
query := `
INSERT INTO users (guildid, discordid, privledge)
VALUES ($1, $2)
RETURNING id
`
id := -1
err := db.QueryRow(query, guildID, discordID, "STUDENT").Scan(&id)
return id, err
}
func DBNewAssignment(guildID string, name string, due int64) (int, error) {
db := dbConnection()
defer db.Close()
query := `
INSERT INTO assignments (guildid, name, due)
VALUES ($1, $2)
RETURNING id
`
id := -1
err := db.QueryRow(query, guildID, name, due).Scan(&id)
return id, err
}
/* get all assignments from a guild */
// func DBGetAssignment(guildID int) ([]*Assignment, error) {
func DBGetAssignment(guildID string) {
db := dbConnection()
defer db.Close()
query := `
SELECT * FROM assignments WHERE guildid == $1
`
_, _ = db.Query(query, guildID)
}