Passed
Push — main ( 0795ea...57a23e )
by Yume
02:19 queued 01:06
created

domain.*User.TableName   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package domain
2
3
import (
4
	"github.com/go-playground/validator/v10"
5
	"gorm.io/gorm"
6
)
7
8
// User is the domain model for a user
9
type User struct {
10
	gorm.Model    `swaggerignore:"true"`
11
	Username      string     `json:"username"`
12
	Email         string     `json:"email" validate:"email" gorm:"unique"`
13
	Password      string     `json:"-"`
14
	Avatar        string     `json:"avatar"`
15
	OauthProvider string     `json:"oauth_provider" `
16
	OauthID       string     `json:"oauth_id" gorm:"unique"`
17
	Learning      []*Deck    `json:"learning" gorm:"many2many:user_decks;"`
18
	OwnDecks      []Deck     `json:"own_decks" gorm:"foreignKey:OwnerID"`
19
	Permission    Permission `json:"permission"`
20
	Oauth         bool       `json:"oauth" gorm:"default:false"`
21
}
22
23
// TableName returns the table name for the user model
24
func (u *User) TableName() string {
25
	return "users"
26
}
27
28
// ToPublicUser converts the user to a public user
29
func (u *User) ToPublicUser() PublicUser {
30
	return PublicUser{
31
		ID:         u.ID,
32
		Username:   u.Username,
33
		Email:      u.Email,
34
		Avatar:     u.Avatar,
35
		Permission: u.Permission,
36
	}
37
}
38
39
// Validate validates the user
40
func (u *User) Validate() error {
41
	validate := validator.New()
42
	return validate.Struct(u)
43
}
44
45
// HasPermission checks if the user has the given permission
46
func (u *User) HasPermission(permission Permission) bool {
47
	return u.Permission >= permission
48
}
49
50
// PublicUser is the public user model
51
type PublicUser struct {
52
	Username   string     `json:"username"`   // Username of the user
53
	Email      string     `json:"email"`      // Email of the user
54
	Avatar     string     `json:"avatar"`     // Avatar of the user
55
	ID         uint       `json:"id"`         // ID of the user
56
	Permission Permission `json:"permission"` // Permission of the user
57
}
58
59
// Login is the login model
60
type Login struct {
61
	Email    string `json:"email" validate:"email"` // Email of the user
62
	Password string `json:"password"`               // Password of the user
63
}
64
65
// Register is the register model
66
type Register struct {
67
	Username string `json:"username" validate:"required"` // Username of the user
68
	Email    string `json:"email" validate:"email"`       // Email of the user
69
	Password string `json:"password" validate:"required"` // Password of the user
70
}
71
72
// Validate validates the register model
73
func (r *Register) Validate() error {
74
	validate := validator.New()
75
	return validate.Struct(r)
76
}
77
78
// ToUser converts the register model to a user
79
func (r *Register) ToUser() User {
80
	return User{
81
		Username:   r.Username,
82
		Email:      r.Email,
83
		Password:   r.Password,
84
		Permission: PermissionUser,
85
	}
86
}
87