-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
64 lines (55 loc) · 1.48 KB
/
users.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
package main
import (
"context"
)
// addUser to database.
func (srv *Shoutyface) addUser(name, email string) error {
sql := "insert into users(name,email) values($1,$2)"
_, err := srv.dbp.Exec(context.Background(), sql, name, email)
return err
}
// rmUser deletes a user from the database.
func (srv *Shoutyface) rmUser(name string) error {
_, err := srv.dbp.Exec(context.Background(), "delete from users where name=$1;", name)
return err
}
// User structure.
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
func (srv *Shoutyface) listUsers() []User {
rows, err := srv.dbp.Query(context.Background(), "select name,email from users;")
if err != nil {
return nil
}
defer rows.Close()
var users []User
for rows.Next() {
u := User{}
rows.Scan(&u.Name, &u.Email)
users = append(users, u)
}
return users
}
// listSubscribers retuns the e-mail addresses of all subscribers to a channel.
func (srv *Shoutyface) listSubscribers(channel string) []string {
emails := []string{}
sql := "select u.email from users u inner join channels c on c.name=$1 inner join subs s on s.cid=c.id and s.uid=u.id;"
rows, err := srv.dbp.Query(context.Background(), sql, channel)
if err != nil {
srv.E("Error getting subscribers: %s", err.Error())
return nil
}
defer rows.Close()
var m string
for rows.Next() {
err = rows.Scan(&m)
if err != nil {
srv.E("Error getting subscribers: %s", err.Error())
return emails
}
emails = append(emails, m)
}
return emails
}