59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"videoplayer/dao"
|
|
"videoplayer/proto"
|
|
)
|
|
|
|
func CreateUser(name, password, email, gender string, age int) uint {
|
|
return dao.CreateUser(name, password, email, gender, age)
|
|
}
|
|
|
|
func GetUser(name, email, password string) dao.User {
|
|
emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
|
re := regexp.MustCompile(emailRegex)
|
|
var user dao.User
|
|
if re.MatchString(name) {
|
|
user = dao.FindUserByEmail(name)
|
|
} else {
|
|
user = dao.FindUserByName(name)
|
|
}
|
|
if user.ID != 0 && user.Password == password {
|
|
return user
|
|
}
|
|
return dao.User{}
|
|
}
|
|
|
|
func ContainsUser(name, email string) bool {
|
|
user := dao.FindUserByName(name)
|
|
user2 := dao.FindUserByEmail(email)
|
|
if user.ID != 0 || user2.ID != 0 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func GetUserByID(id int) []proto.User {
|
|
return dao.FindUserByID(id)
|
|
}
|
|
|
|
func GetUserByNameLike(name string) []proto.User {
|
|
return dao.FindUserByNameLike(name)
|
|
}
|
|
|
|
func UpdateUser(user_id int, req proto.UpdateUserInfoReq) (int, error) {
|
|
cur_user := dao.FindUserByID2(user_id)
|
|
fmt.Println("cur_user:", cur_user, "req:", req)
|
|
if user_id == req.ID {
|
|
dao.UpdateUserByID2(user_id, req)
|
|
return user_id, nil
|
|
} else if cur_user.Role == "admin" {
|
|
dao.UpdateUserByID2(req.ID, req)
|
|
return req.ID, nil
|
|
} else {
|
|
return 0, nil
|
|
}
|
|
}
|