-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_postgres.go
298 lines (255 loc) · 7.51 KB
/
repository_postgres.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"log"
"strings"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
type PostgresRepository struct {
db *sqlx.DB
}
func (r *PostgresRepository) CreateOrUpdateUser(user User) error {
query := `
insert into users (user_id, firstname, lastname, email)
values ($1, $2, $3, $4)
on conflict(user_id) do update
set firstname = excluded.firstname,
lastname = excluded.lastname,
email = excluded.email;`
_, err := r.db.Exec(query, user.UserID, user.FirstName, user.LastName, user.Email)
if err != nil {
log.Fatal(err)
}
return err
}
func (r *PostgresRepository) GetUserByID(userID string) *User {
query := `
select user_id, firstname, lastname, email
from users where user_id=$1;
`
user := &User{}
err := r.db.QueryRow(query, userID).Scan(&user.UserID, &user.FirstName, &user.LastName, &user.Email)
if err != nil {
log.Fatal("failed to find user", err)
}
return user
}
func (r *PostgresRepository) InsertLink(link Link) int64 {
query := `
insert into links (url, video_id, title, channel_name, duration,
submitted_by, dedicated_to, is_expired, created_at)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)
returning link_id;
`
var linkId int64
err := r.db.QueryRow(query, link.URL, link.VideoID, link.Title, link.ChannelName,
link.Duration, link.SubmittedBy, link.DedicatedTo, link.IsExpired, link.CreatedAt,
).Scan(&linkId)
if err != nil {
log.Fatal(err)
}
return linkId
}
func (r *PostgresRepository) GetLinkByID(id int64) (*Link, error) {
query := `
select l.link_id, l.video_id, l.url, l.title, l.channel_name, l.duration,
l.submitted_by, l.dedicated_to, l.is_expired, l.created_at,
(select coalesce(sum(score), 0) from votes as v where v.link_id = l.link_id)
from links as l
where l.link_id=$1;`
l := Link{LinkID: id}
err := r.db.QueryRow(query, l.LinkID).Scan(&l.LinkID, &l.VideoID, &l.URL, &l.Title, &l.ChannelName,
&l.Duration, &l.SubmittedBy, &l.DedicatedTo, &l.IsExpired, &l.CreatedAt, &l.TotalVotes)
return &l, err
}
func (r *PostgresRepository) GetLinksByUser(userID string) []Link {
query := `
select l.link_id, l.video_id, l.url, l.title, l.channel_name, l.duration,
l.submitted_by, l.dedicated_to, l.is_expired, l.created_at,
(select coalesce(sum(score), 0) from votes as v1 where v1.link_id = l.link_id),
(select coalesce(sum(score), 0) from votes as v2 where v2.link_id = l.link_id and v2.user_id = l.submitted_by)
from links as l
where l.is_expired=false and l.submitted_by=$1;
`
rows, err := r.db.Query(query, userID)
if err != nil {
log.Fatal(err)
}
links := make([]Link, 0)
for rows.Next() {
l := Link{}
err = rows.Scan(&l.LinkID, &l.VideoID, &l.URL, &l.Title, &l.ChannelName,
&l.Duration, &l.SubmittedBy, &l.DedicatedTo, &l.IsExpired, &l.CreatedAt,
&l.TotalVotes, &l.MyVote)
if err != nil {
log.Fatal(err)
}
links = append(links, l)
}
return links
}
func (r *PostgresRepository) UpdateLink(link Link) error {
query := `
update links
set url=$1, title=$2, channel_name=$3, duration=$4,
submitted_by=$5, dedicated_to=$6, is_expired=$7, created_at=$8
where link_id=$9;`
_, err := r.db.Exec(query, link.URL, link.Title, link.ChannelName, link.Duration,
link.SubmittedBy, link.DedicatedTo, link.IsExpired, link.CreatedAt, link.LinkID)
if err != nil {
log.Fatal(err)
}
return nil
}
func (r *PostgresRepository) GetAllLinks(limit int64) []Link {
query := `
select l.link_id, l.video_id, l.url, l.title, l.channel_name, l.duration,
l.submitted_by, l.dedicated_to, l.is_expired, l.created_at,
(select coalesce(sum(score), 0) from votes as v where v.link_id = l.link_id)
from links as l
where l.is_expired=false
limit $1;`
rows, err := r.db.Query(query, limit)
if err != nil {
log.Println("links by user failed here")
log.Fatal(err)
}
links := make([]Link, 0)
for rows.Next() {
l := Link{}
err = rows.Scan(&l.LinkID, &l.VideoID, &l.URL, &l.Title, &l.ChannelName,
&l.Duration, &l.SubmittedBy, &l.DedicatedTo, &l.IsExpired, &l.CreatedAt,
&l.TotalVotes)
if err != nil {
log.Fatal(err)
}
links = append(links, l)
}
return links
}
func (r *PostgresRepository) GetVotesForUser(linkIds []int64, userID string) map[int64]int64 {
if len(linkIds) == 0 {
return make(map[int64]int64)
}
query, args, err := sqlx.In(
`select link_id, score from votes where user_id=$1 and link_id IN (?);`,
userID, linkIds)
query = r.db.Rebind(query)
rows, err := r.db.Query(query, args...)
if err != nil {
log.Fatal(err)
}
result := make(map[int64]int64)
for rows.Next() {
var linkid, score int64
rows.Scan(&linkid, &score)
result[linkid] = score
}
return result
}
func (r *PostgresRepository) MarkVote(linkID int64, userID string, score int64) error {
query := `
insert into votes(link_id, user_id, score)
values ($1, $2, $3)
on conflict(link_id, user_id) do update
set user_id=excluded.user_id,
link_id=excluded.link_id,
score=excluded.score;
`
_, err := r.db.Exec(query, linkID, userID, score)
if err != nil {
log.Fatal(err)
}
return nil
}
func (r *PostgresRepository) TotalVoteForLinks(linkIDs []int64) map[int64]int64 {
var query string
if len(linkIDs) > 0 {
query = "select link_id, sum(score) from votes where link_id in (?" +
strings.Repeat(",?", len(linkIDs)-1) +
") group by link_id;"
} else {
return make(map[int64]int64)
}
query = r.db.Rebind(query)
args := make([]interface{}, 0)
for _, lid := range linkIDs {
var tmp interface{}
tmp = lid
args = append(args, tmp)
}
rows, err := r.db.Query(query, args...)
if err != nil {
log.Fatal(err)
}
result := make(map[int64]int64)
for rows.Next() {
var linkid, score int64
rows.Scan(&linkid, &score)
result[linkid] = score
}
return result
}
func (r *PostgresRepository) NewTest(message string) error {
query := `INSERT INTO test (message) values ($1)`
res, err := r.db.Exec(query, message)
naff, _ := res.RowsAffected()
log.Println("new test ", naff, " rows affected")
return err
}
func (r *PostgresRepository) close() {
r.db.Close()
}
// func NewPostgresRepository(host, port, user, pass, dbname string) *PostgresRepository {
func NewPostgresRepository(dbUrl string) *PostgresRepository {
// psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
// host, port, user, pass, dbname)
// db, err := sqlx.Open("postgres", psqlInfo)
db, err := sqlx.Open("postgres", dbUrl)
if err != nil {
return nil
}
log.Println("connected to db. creating new tables")
// make sure the required tables exist
// if not then create them
testTable := `
create table if not exists test (
message text
);`
usersTable := `
create table if not exists users (
user_id text primary key,
firstname text,
lastname text,
email text
);`
linksTable := `
create table if not exists links (
link_id serial primary key,
url text not null,
video_id text not null,
title text,
channel_name text,
duration int,
submitted_by text,
dedicated_to text,
is_expired bool,
created_at int
);`
votesTable := `
create table if not exists votes (
link_id integer not null,
user_id text not null,
score integer not null,
constraint unq UNIQUE(link_id, user_id)
);`
tables := []string{testTable, usersTable, linksTable, votesTable}
for _, t := range tables {
if _, err = db.Exec(t); err != nil {
log.Fatal("failed to exec stmt ", err)
}
}
// check for possible errors and traps
log.Println("Connected to database", db)
return &PostgresRepository{db: db}
}