-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
102 lines (81 loc) · 1.95 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
package word
import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
type DB struct {
*sqlx.DB
}
func (db *DB) Open() error {
d, err := sqlx.Open("postgres", "timezone=UTC sslmode=disable")
if err != nil {
return err
}
db.DB = d
return nil
}
func (db *DB) GetRandomWord() (*Word, error) {
word := &Word{}
err := db.Get(word, "select * from words offset random()*(select max(word_id) from words) limit 1")
if err != nil {
return nil, err
}
_, err = db.Exec("insert into word_history (word_id) values ($1)", word.Id)
if err != nil {
return nil, err
}
return word, nil
}
func (db *DB) GetRandom10() ([]*Word, error) {
words := []*Word{}
err := db.Select(&words, "select * from words order by random() limit 10")
if err != nil {
return nil, err
}
return words, nil
}
func (db *DB) GetHistory() ([]*Word, error) {
words := []*Word{}
err := db.Select(&words, "select w.* from words w inner join word_history wh on w.word_id = wh.word_id order by wh.created_at desc limit 10")
if err != nil {
return nil, err
}
return words, nil
}
func (db *DB) GetTop10() ([]*Word, error) {
words := []*Word{}
err := db.Select(&words, "select * from words order by rating desc limit 10")
if err != nil {
return nil, err
}
return words, nil
}
func (db *DB) GetBottom10() ([]*Word, error) {
words := []*Word{}
err := db.Select(&words, "select * from words order by rating asc limit 10")
if err != nil {
return nil, err
}
return words, nil
}
func (db *DB) UpVoteWord(word string, ip string) error {
err := db.updateRating(word, 1, ip)
if err != nil {
return err
}
return nil
}
func (db *DB) DownVoteWord(word string, ip string) error {
err := db.updateRating(word, -1, ip)
if err != nil {
return err
}
return nil
}
func (db *DB) updateRating(word string, vote int64, ip string) error {
_, err := db.Exec("insert into votes (word, vote, ipaddress) values ($1, $2, $3)", word, vote, ip)
if err != nil {
return err
}
return nil
}