Completed
Push — main ( c7f647...178139 )
by Yume
14s queued 13s
created

models.*PublicUser.Set   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
package models
2
3
import (
4
	"gorm.io/gorm"
5
)
6
7
// User structure
8
type User struct {
9
	gorm.Model  `swaggerignore:"true"`
10
	Username    string     `json:"user_name" example:"Yume"`     // This should be unique
11
	Permissions Permission `json:"user_permissions" example:"0"` // 0: None 1: User; 2: Mod; 3: Admin
12
	Avatar      string     `json:"user_avatar" example:"avatar url"`
13
	Bio         string     `json:"user_bio" example:"A simple demo bio"`
14
	Email       string     `json:"email" gorm:"unique"`
15
	Password    []byte     `json:"-" swaggerignore:"true"`
16
}
17
18
type LoginStruct struct {
19
	Email    string `json:"email"`
20
	Password string `json:"password"`
21
}
22
23
type LoginResponse struct {
24
	Token   string `json:"token"`
25
	Message string `json:"message"`
26
}
27
28
type RegisterStruct struct {
29
	Username string `json:"username"`
30
	Email    string `json:"email"`
31
	Password string `json:"password"`
32
}
33
34
type PasswordResetConfirm struct {
35
	Email string `json:"email"`
36
	Code  string `json:"code"`
37
	Pass  string `json:"password"`
38
}
39
40
type PublicUser struct {
41
	ID          uint       `json:"user_id"`
42
	Username    string     `json:"user_name"`
43
	Permissions Permission `json:"user_permissions" example:"0"` // 0: User; 1: Mod; 2: Admin
44
	Avatar      string     `json:"user_avatar" example:"avatar url"`
45
	Bio         string     `json:"user_bio" example:"A simple demo bio"`
46
}
47
48
func (publicUser *PublicUser) Set(user *User) {
49
	publicUser.ID = user.ID
50
	publicUser.Username = user.Username
51
	publicUser.Permissions = user.Permissions
52
	publicUser.Avatar = user.Avatar
53
	publicUser.Bio = user.Bio
54
}
55
56
// Permission enum type
57
type Permission int64
58
59
const (
60
	PermNone Permission = iota
61
	PermUser
62
	PermMod
63
	PermAdmin
64
)
65
66
// ToString returns Permission value as a string
67
func (s Permission) ToString() string {
68
	switch s {
69
	case PermUser:
70
		return "PermUser"
71
	case PermMod:
72
		return "PermMod"
73
	case PermAdmin:
74
		return "PermAdmin"
75
	default:
76
		return "Unknown"
77
	}
78
}
79