66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package user
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/infraboard/mcube/v2/exception"
|
|
"github.com/infraboard/mcube/v2/ioc/config/validator"
|
|
"gitlab.com/go-course-project/go17/vblog/utils"
|
|
)
|
|
|
|
func New(in *RegistryRequest) (*User, error) {
|
|
if err := in.Validate(); err != nil {
|
|
return nil, exception.NewBadRequest("参数校验失败: %s", err)
|
|
}
|
|
return &User{
|
|
ResourceMeta: *utils.NewResourceMeta(),
|
|
RegistryRequest: *in,
|
|
}, nil
|
|
}
|
|
|
|
type User struct {
|
|
// 存放到数据里的对象的远数据信息
|
|
utils.ResourceMeta
|
|
// 具体参数
|
|
RegistryRequest
|
|
}
|
|
|
|
type RegistryRequest struct {
|
|
// 用户名
|
|
Username string `json:"username" gorm:"column:username;unique;index" validate:"required"`
|
|
// 密码
|
|
Password string `json:"password" gorm:"column:username;type:varchar(255)" validate:"required"`
|
|
// Profile 信息
|
|
Profile
|
|
// 用户状态
|
|
Status
|
|
}
|
|
|
|
func (r *RegistryRequest) Validate() error {
|
|
return validator.Validate(r)
|
|
}
|
|
|
|
type Profile struct {
|
|
// 头像
|
|
Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(255)"`
|
|
// 用户昵称
|
|
NickName string `json:"nic_name" gorm:"column:nic_name;type:varchar(100)"`
|
|
// 用户邮箱, 验证用户有效性
|
|
Email string `json:"email" gorm:"column:email;type:varchar(100)"`
|
|
}
|
|
|
|
type Status struct {
|
|
// 冻结时间
|
|
BlockAt *time.Time `json:"block_at" gorm:"column:block_at"`
|
|
// 冻结原因
|
|
BlockReason string `json:"block_reason" gorm:"column:block_reason;type:text"`
|
|
}
|
|
|
|
func (s *Status) IsBlocked() bool {
|
|
return s.BlockAt != nil
|
|
}
|
|
|
|
func (u *User) TableName() string {
|
|
return "users"
|
|
}
|