-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
43 lines (35 loc) · 818 Bytes
/
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
package common
import (
"strings"
"git.iptq.io/nso/common/models"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
)
type DB struct {
*gorm.DB
}
func ConnectDB(provider string, connection string) (db *DB, err error) {
h, err := gorm.Open(provider, connection)
h.AutoMigrate(
models.Beatmap{},
models.BeatmapSet{},
models.Score{},
models.User{},
)
if err != nil {
return
}
return &DB{h}, nil
}
func (db *DB) Authenticate(username, password string) (err error) {
user, err := db.GetUserByName(username)
if err != nil {
return
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
return
}
func (db *DB) GetUserByName(username string) (user models.User, err error) {
err = db.Where("username = ?", strings.ToLower(username)).First(&user).Error
return
}