2021-04-25 01:53:49 +00:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
|
|
|
"chat/rdm"
|
|
|
|
"chat/user"
|
|
|
|
"chat/utils"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Ctx context.Context
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:29:38 +00:00
|
|
|
func UserInfo(id int) *user.User {
|
|
|
|
rd := rdm.GetRdm()
|
|
|
|
ctx := context.Background()
|
|
|
|
key := user.GetUserKey(id)
|
|
|
|
u := rd.HGetAll(ctx, key).Val()
|
|
|
|
return map2user(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func map2user(hash map[string]string) *user.User {
|
|
|
|
id, _ := strconv.Atoi(hash["id"])
|
|
|
|
sex, _ := strconv.Atoi(hash["sex"])
|
|
|
|
return &user.User{
|
|
|
|
Id: id,
|
|
|
|
Name: hash["name"],
|
|
|
|
Sex: int8(sex),
|
|
|
|
Password: hash["password"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:16:18 +00:00
|
|
|
func AddUser(u user.User) error {
|
2021-04-25 01:53:49 +00:00
|
|
|
getRdm := rdm.GetRdm()
|
2021-04-28 08:16:18 +00:00
|
|
|
if u.Name == "" {
|
2021-04-25 01:53:49 +00:00
|
|
|
return errors.New("user name can't be empty")
|
|
|
|
}
|
2021-04-28 08:16:18 +00:00
|
|
|
if u.Password == "" {
|
2021-04-25 01:53:49 +00:00
|
|
|
return errors.New("user password can't be empty")
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:16:18 +00:00
|
|
|
if utils.IsContain(u.Sex, []int8{1, 2}) < 0 {
|
2021-04-25 01:53:49 +00:00
|
|
|
return errors.New("user name can't be empty")
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:16:18 +00:00
|
|
|
r := getRdm.HMSet(Ctx, user.GetUserKey(u.Id), map[string]interface{}{
|
|
|
|
"id": strconv.Itoa(u.Id),
|
|
|
|
"name": u.Name,
|
|
|
|
"password": u.Password,
|
|
|
|
"sex": strconv.Itoa(int(u.Sex)),
|
2021-04-25 01:53:49 +00:00
|
|
|
})
|
|
|
|
if e := r.Err(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|