2024-05-18 17:12:24 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"videoplayer/dao"
|
2024-06-28 10:10:23 +08:00
|
|
|
"videoplayer/proto"
|
2024-05-18 17:12:24 +08:00
|
|
|
)
|
|
|
|
|
|
2024-07-01 19:32:58 +08:00
|
|
|
func CreateUser(name, password, email, gender string, age int) uint {
|
|
|
|
|
return dao.CreateUser(name, password, email, gender, age)
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-07 20:16:08 +08:00
|
|
|
func GetUser(name, email, password string) dao.User {
|
2024-05-18 17:12:24 +08:00
|
|
|
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{}
|
|
|
|
|
}
|
2024-05-21 15:13:01 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-06-28 10:10:23 +08:00
|
|
|
|
2024-06-28 10:38:55 +08:00
|
|
|
func GetUserByID(id int) []proto.User {
|
2024-06-28 10:10:23 +08:00
|
|
|
return dao.FindUserByID(id)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 10:38:55 +08:00
|
|
|
func GetUserByNameLike(name string) []proto.User {
|
2024-06-28 10:10:23 +08:00
|
|
|
return dao.FindUserByNameLike(name)
|
|
|
|
|
}
|
2024-10-06 16:35:54 +08:00
|
|
|
|
|
|
|
|
func UpdateUser(user_id int, req proto.UpdateUserInfoReq) (int, error) {
|
|
|
|
|
cur_user := dao.FindUserByID2(user_id)
|
|
|
|
|
if user_id == req.ID {
|
|
|
|
|
dao.UpdateUserByID2(user_id, req)
|
|
|
|
|
return user_id, nil
|
|
|
|
|
} else if cur_user.Role == "admin" {
|
2024-10-06 16:41:29 +08:00
|
|
|
dao.UpdateUserByID2(req.ID, req)
|
2024-10-06 16:35:54 +08:00
|
|
|
return req.ID, nil
|
|
|
|
|
} else {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
}
|