goweb/repository/user_repository.go
2021-08-22 00:13:00 +08:00

34 lines
695 B
Go

package repository
import (
"goweb/global"
"goweb/model"
)
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
}