goweb/repository/user_repository.go

34 lines
707 B
Go

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