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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddUser(user user.User) error {
|
|
|
|
getRdm := rdm.GetRdm()
|
|
|
|
if user.Name == "" {
|
|
|
|
return errors.New("user name can't be empty")
|
|
|
|
}
|
|
|
|
if user.Password == "" {
|
|
|
|
return errors.New("user password can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsContain(user.Sex, []int8{1, 2}) < 0 {
|
|
|
|
return errors.New("user name can't be empty")
|
|
|
|
}
|
|
|
|
|
2021-04-25 08:29:30 +00:00
|
|
|
r := getRdm.HMSet(Ctx, "go_chat_Users:"+strconv.Itoa(user.Id), map[string]interface{}{
|
|
|
|
"id": strconv.Itoa(user.Id),
|
|
|
|
"name": user.Name,
|
|
|
|
"password": user.Password,
|
|
|
|
"sex": strconv.Itoa(int(user.Sex)),
|
2021-04-25 01:53:49 +00:00
|
|
|
})
|
|
|
|
if e := r.Err(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|