-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathuser.services.go
100 lines (79 loc) · 1.68 KB
/
user.services.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
package services
import (
"github.com/emarifer/go-echo-templ-htmx/db"
"golang.org/x/crypto/bcrypt"
)
func NewUserServices(u User, uStore db.Store) *UserServices {
return &UserServices{
User: u,
UserStore: uStore,
}
}
type User struct {
ID int `json:"id"`
Email string `json:"email"`
Password string `json:"password"`
Username string `json:"username"`
}
type UserServices struct {
User User
UserStore db.Store
}
func (us *UserServices) CreateUser(u User) error {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), 8)
if err != nil {
return err
}
stmt := `INSERT INTO users(email, password, username) VALUES($1, $2, $3)`
_, err = us.UserStore.Db.Exec(
stmt,
u.Email,
string(hashedPassword),
u.Username,
)
return err
}
func (us *UserServices) CheckEmail(email string) (User, error) {
query := `SELECT id, email, password, username FROM users
WHERE email = ?`
stmt, err := us.UserStore.Db.Prepare(query)
if err != nil {
return User{}, err
}
defer stmt.Close()
us.User.Email = email
err = stmt.QueryRow(
us.User.Email,
).Scan(
&us.User.ID,
&us.User.Email,
&us.User.Password,
&us.User.Username,
)
if err != nil {
return User{}, err
}
return us.User, nil
}
/* func (us *UserServices) GetUserById(id int) (User, error) {
query := `SELECT id, email, password, username FROM users
WHERE id = ?`
stmt, err := us.UserStore.Db.Prepare(query)
if err != nil {
return User{}, err
}
defer stmt.Close()
us.User.ID = id
err = stmt.QueryRow(
us.User.ID,
).Scan(
&us.User.ID,
&us.User.Email,
&us.User.Password,
&us.User.Username,
)
if err != nil {
return User{}, err
}
return us.User, nil
} */