-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.go
110 lines (90 loc) · 1.96 KB
/
queries.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 repository
import (
"context"
"github.com/e-politica/api/pkg/database"
"github.com/jackc/pgx/v4"
)
func isUserRegistered(db *database.Db, email string) (bool, error) {
query := `
SELECT COUNT(1)
FROM user_account
WHERE user_email=$1
`
row, err := db.QueryRow(query, email)
if err != nil {
return false, err
}
var count int
err = row.Scan(&count)
return count == 1, err
}
func getUserId(db *database.Db, email string) (userId string, err error) {
query := `
SELECT user_id
FROM user_account
WHERE user_email=$1
`
row, err := db.QueryRow(query, email)
if err != nil {
return
}
err = row.Scan(&userId)
return
}
func getDefaultAccountPassword(db *database.Db, userId string) (password string, err error) {
query := `
SELECT df_account_password
FROM default_account
WHERE df_account_user_id=$1
`
row, err := db.QueryRow(query, userId)
if err != nil {
return
}
err = row.Scan(&password)
return
}
func insertUser(db *database.Db, name, email, pictureUrl, accountType string) (userId string, err error) {
query := `
INSERT INTO user_account (
user_name,
user_email,
user_picture_url,
user_account_type
)
VALUES ($1, $2, $3, $4)
RETURNING user_id
`
row, err := db.QueryRow(query, name, email, pictureUrl, accountType)
if err != nil {
return
}
err = row.Scan(&userId)
return
}
func insertUserTx(ctx context.Context, tx pgx.Tx, name, email, pictureUrl, accountType string) (userId string, err error) {
query := `
INSERT INTO user_account (
user_name,
user_email,
user_picture_url,
user_account_type
)
VALUES ($1, $2, $3, $4)
RETURNING user_id
`
row := tx.QueryRow(ctx, query, name, email, pictureUrl, accountType)
err = row.Scan(&userId)
return
}
func insertDefaultAccountTx(ctx context.Context, tx pgx.Tx, userId, password string) error {
query := `
INSERT INTO default_account (
df_account_user_id,
df_account_password
)
VALUES ($1, $2)
`
_, err := tx.Exec(ctx, query, userId, password)
return err
}