Feature/all method #4
|
@ -48,6 +48,7 @@ func (payload CreateOptions) newUser() (result User, err error) {
|
||||||
return User{
|
return User{
|
||||||
ID: mongodb.NewObjectID(),
|
ID: mongodb.NewObjectID(),
|
||||||
Name: payload.Name,
|
Name: payload.Name,
|
||||||
|
SearchString: getSearchString(payload.Name, payload.Phone, payload.Email),
|
||||||
Phone: payload.Phone,
|
Phone: payload.Phone,
|
||||||
Email: payload.Email,
|
Email: payload.Email,
|
||||||
HashedPassword: hashPassword(payload.Password),
|
HashedPassword: hashPassword(payload.Password),
|
||||||
|
|
|
@ -44,12 +44,13 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error {
|
||||||
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
||||||
updateData := bson.M{
|
updateData := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"name": payload.Name,
|
"name": payload.Name,
|
||||||
"phone": payload.Phone,
|
"searchString": getSearchString(payload.Name, payload.Phone, payload.Email),
|
||||||
"email": payload.Email,
|
"phone": payload.Phone,
|
||||||
"roleId": roleID,
|
"email": payload.Email,
|
||||||
"other": payload.Other,
|
"roleId": roleID,
|
||||||
"updatedAt": now(),
|
"other": payload.Other,
|
||||||
|
"updatedAt": now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
helper.go
20
helper.go
|
@ -1,6 +1,9 @@
|
||||||
package usermngmt
|
package usermngmt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Selly-Modules/mongodb"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,3 +16,20 @@ func checkPasswordHash(password, hash string) bool {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSearchString(fieldList ...string) string {
|
||||||
|
var (
|
||||||
|
searchList = make([]interface{}, 0)
|
||||||
|
format = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, value := range fieldList {
|
||||||
|
searchList = append(searchList, mongodb.NonAccentVietnamese(value))
|
||||||
|
if i == 0 {
|
||||||
|
format += "%s"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
format += " %s"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(format, searchList...)
|
||||||
|
}
|
||||||
|
|
1
model.go
1
model.go
|
@ -10,6 +10,7 @@ import (
|
||||||
type User struct {
|
type User struct {
|
||||||
ID primitive.ObjectID `bson:"_id" json:"_id"`
|
ID primitive.ObjectID `bson:"_id" json:"_id"`
|
||||||
Name string `bson:"name" json:"name"`
|
Name string `bson:"name" json:"name"`
|
||||||
|
SearchString string `bson:"searchString" json:"-"`
|
||||||
Phone string `bson:"phone" json:"phone"` // unique
|
Phone string `bson:"phone" json:"phone"` // unique
|
||||||
Email string `bson:"email" json:"email"` // unique
|
Email string `bson:"email" json:"email"` // unique
|
||||||
HashedPassword string `bson:"hashedPassword" json:"-"`
|
HashedPassword string `bson:"hashedPassword" json:"-"`
|
||||||
|
|
Loading…
Reference in New Issue