-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
53 lines (46 loc) · 1.23 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
package model
import (
"errors"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
)
type User struct {
gorm.Model
Username string `gorm:"unique_index;not null"`
Email string `gorm:"unique_index;not null"`
Password string `gorm:"not null"`
Bio *string
Image *string
Followers []Follow `gorm:"foreignkey:FollowingID"`
Followings []Follow `gorm:"foreignkey:FollowerID"`
Favorites []Article `gorm:"many2many:favorites;"`
}
type Follow struct {
Follower User
FollowerID uint `gorm:"primary_key" sql:"type:int not null"`
Following User
FollowingID uint `gorm:"primary_key" sql:"type:int not null"`
}
func (u *User) HashPassword(plain string) (string, error) {
if len(plain) == 0 {
return "", errors.New("password should not be empty")
}
h, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost)
return string(h), err
}
func (u *User) CheckPassword(plain string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(plain))
return err == nil
}
// FollowedBy Followings should be pre loaded
func (u *User) FollowedBy(id uint) bool {
if u.Followers == nil {
return false
}
for _, f := range u.Followers {
if f.FollowerID == id {
return true
}
}
return false
}