-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
90 lines (68 loc) · 1.91 KB
/
user.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
package main
import (
"crypto/sha1"
"fmt"
"log"
"time"
)
type User struct {
Id string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
func (user *User) CreateNewUser(dbConnection *DBConnection) *User {
sha1Hash := sha1.New()
sha1Hash.Write([]byte(time.Now().String() + user.Username + user.Password + user.Email))
sha1HashString := sha1Hash.Sum(nil)
userID := fmt.Sprintf("%x", sha1HashString)
query := "INSERT INTO users(id, username, password, email, date_created) VALUES('" + userID + "','" + user.Username + "','" + user.Password + "','" + user.Email + "', date('now'))"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return nil
}
newUser := &User{
Id: userID,
Username: user.Username,
Password: user.Password,
Email: user.Email}
return newUser
}
func (user *User) CheckUserCredentials(dbConnection *DBConnection) *User {
query := "SELECT id, username, password, email FROM users WHERE username='" + user.Username + "' AND password='" + user.Password + "'"
newUser := new(User)
err := dbConnection.db.QueryRow(query).Scan(
&newUser.Id,
&newUser.Username,
&newUser.Password,
&newUser.Email)
if err != nil {
log.Fatal(err)
return nil
}
return newUser
}
func (user *User) CheckUserByID(dbConnection *DBConnection) *User {
query := "SELECT id, username, password, email FROM users WHERE id='" + user.Id + "'"
newUser := new(User)
err := dbConnection.db.QueryRow(query).Scan(
&newUser.Id,
&newUser.Username,
&newUser.Password,
&newUser.Email)
if err != nil {
log.Fatal(err)
return nil
}
return newUser
}
func (user *User) UpdateUserPassword(dbConnection *DBConnection) bool {
query := "UPDATE users SET password='" + user.Password + "' WHERE id='" + user.Id + "'"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}