videoplayer/dao/user.go

60 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dao
import (
"fmt"
"gorm.io/gorm"
"videoplayer/proto"
)
type User struct {
gorm.Model
Name string `gorm:"column:name"`
Age int `gorm:"column:age"`
Email string `gorm:"column:email"`
Password string `gorm:"column:password"`
Gender string `gorm:"column:gender"`
CreateTime string `gorm:"column:create_time"`
UpdateTime string `gorm:"column:update_time"`
}
func CreateUser(name string, password, email string) uint {
user := User{Name: name, Email: email, Password: password}
DB.Create(&user)
return user.ID
}
func DeleteUserByID(id int) int {
DB.Delete(&User{}, id)
return id
}
func FindUserByID(id int) []proto.User {
var users []proto.User
DB.Debug().Where("id = ?", id).First(&users)
return users
}
func FindUserByName(name string) User {
var user User
fmt.Println("name:", name)
DB.Debug().Where("name = ?", name).First(&user)
return user
}
// 根据name模糊查询邮箱也是,不查询密码
func FindUserByNameLike(name string) []proto.User {
var users []proto.User
DB.Debug().Where("name LIKE ? OR email LIKE ?", "%"+name+"%", "%"+name+"%").Find(&users)
return users
}
func FindUserByEmail(email string) User {
var user User
DB.Debug().Where("email = ?", email).First(&user)
return user
}
func UpdateUserByID(id int, name, password, email string) {
DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: name, Password: password, Email: email})
}