This repository has been archived by the owner on Sep 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
userdata.go
110 lines (95 loc) · 2.71 KB
/
userdata.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
package database
import (
"encoding/json"
"github.com/caarlos0/watchub/shared/model"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)
// Userdatastore in database
type Userdatastore struct {
*sqlx.DB
}
// NewUserdatastore datastore
func NewUserdatastore(db *sqlx.DB) *Userdatastore {
return &Userdatastore{db}
}
// GetFollowers of a given userID
func (db *Userdatastore) GetFollowers(userID int64) ([]string, error) {
var logins []string
return logins, db.QueryRow(
"SELECT followers FROM tokens WHERE user_id = $1",
userID,
).Scan(pq.Array(&logins))
}
// SaveFollowers for a given userID
func (db *Userdatastore) SaveFollowers(userID int64, followers []string) error {
_, err := db.Exec(
"UPDATE tokens SET followers = $2 WHERE user_id = $1",
userID,
pq.Array(followers),
)
return err
}
// GetStars of a given userID
func (db *Userdatastore) GetStars(userID int64) (result []model.Star, err error) {
var stars json.RawMessage
if err := db.QueryRow(
"SELECT stars FROM tokens WHERE user_id = $1",
userID,
).Scan(&stars); err != nil {
return result, err
}
return result, json.Unmarshal(stars, &result)
}
// SaveStars for a given userID
func (db *Userdatastore) SaveStars(userID int64, stars []model.Star) error {
data, err := json.Marshal(stars)
if err != nil {
return err
}
_, err = db.Exec(
"UPDATE tokens SET stars = $2 WHERE user_id = $1",
userID,
data,
)
return err
}
const followerCountQuery = `
SELECT COALESCE(array_length(followers, 1), 0)
FROM tokens
WHERE user_id = $1
`
// FollowerCount returns the amount of followers stored for a given userID
func (db *Userdatastore) FollowerCount(userID int64) (count int, err error) {
err = db.QueryRow(followerCountQuery, userID).Scan(&count)
return
}
const starCountQuery = `
SELECT COALESCE(SUM(json_array_length((repo->>'stargazers')::json)), 0)
FROM tokens t
CROSS JOIN json_array_elements(t.stars) repo
WHERE t.user_id = $1
`
// StarCount returns the amount of stargazers of all the user's repositories
func (db *Userdatastore) StarCount(userID int64) (count int, err error) {
err = db.QueryRow(starCountQuery, userID).Scan(&count)
return
}
const repositoryCountQuery = `
SELECT COALESCE(json_array_length(stars), 0)
FROM tokens
WHERE user_id = $1
`
// RepositoryCount returns the amount of followers stored for a given userID
func (db *Userdatastore) RepositoryCount(userID int64) (count int, err error) {
err = db.QueryRow(repositoryCountQuery, userID).Scan(&count)
return
}
// UserExist returns true if an user is already registered in the db
func (db *Userdatastore) UserExist(userID int64) (result bool, err error) {
err = db.QueryRow(
"SELECT EXISTS(SELECT 1 FROM tokens WHERE user_id = $1)",
userID,
).Scan(&result)
return
}