-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
57 lines (44 loc) · 1.22 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
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
var DB *sql.DB
func ConnectDB(host, port, user, password, dbname string) {
psqlConn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlConn)
if err != nil {
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
DB = db
}
func CreateUser(username, email, password string) (string, error) {
var uuid string
err := DB.QueryRow("SELECT uuid_generate_v4()").Scan(&uuid)
if err != nil {
return "", err
}
hashedPassword, err := HashPassword(password)
if err != nil {
return "", err
}
_, err = DB.Exec("INSERT INTO users (id, username, email, password) VALUES (($1), $2, $3, $4)", uuid, username, email, hashedPassword)
if err != nil {
return "", err
}
return uuid, nil
}
func GetUserByEmail(email string) (*User, error) {
query := "SELECT id, username, email, password, createdat, updatedat FROM users WHERE Email = $1"
row := DB.QueryRow(query, email)
user := &User{}
err := row.Scan(&user.ID, &user.Username, &user.Email, &user.Password, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
return nil, err
}
return user, nil
}