2021-08-20 14:59:13 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-08-22 00:13:00 +08:00
|
|
|
"goweb/global"
|
|
|
|
"goweb/model"
|
2021-08-20 14:59:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type userRepository struct{}
|
|
|
|
|
|
|
|
type UserRepository interface {
|
|
|
|
Migrate() error
|
|
|
|
GetUserByPhone(string) (*model.User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserRepository 新建实例
|
|
|
|
func NewUserRepository() UserRepository {
|
|
|
|
return userRepository{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate 初始化数据库结构
|
|
|
|
func (r userRepository) Migrate() error {
|
|
|
|
return global.MySQLConn.AutoMigrate(&model.User{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByPhone 根据手机号查询用户信息
|
|
|
|
func (r userRepository) GetUserByPhone(phone string) (*model.User, error) {
|
|
|
|
u := model.User{Phone: phone}
|
|
|
|
err := global.MySQLConn.FirstOrCreate(&u, &u).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &u, nil
|
|
|
|
}
|