fix comment

This commit is contained in:
Hoang 2021-11-08 15:34:20 +07:00
parent bfb263ca50
commit b06431b9b9
6 changed files with 122 additions and 86 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
// CreateOptions ... // CreateOptions ...
@ -14,9 +15,9 @@ type CreateOptions struct {
Name string Name string
Phone string Phone string
Email string Email string
HashPassword string HashedPassword string
Status string Status string
RoleID string RoleID primitive.ObjectID
Other string Other string
} }
@ -39,9 +40,14 @@ func (s Service) Create(payload CreateOptions) error {
return err return err
} }
// Find phone,email exists or not // Find roleID exists or not
if s.haveNameOrPhoneExisted(ctx, userData.Phone, userData.Email) { if !s.isRoleIDAlreadyExisted(ctx, userData.RoleID) {
return errors.New("have name or phone existed") return errors.New("roleID does not exist")
}
// Find phone number,email exists or not
if s.isPhoneNumberOrEmailAlreadyExisted(ctx, userData.Phone, userData.Email) {
return errors.New("phone number or email already existed")
} }
// Create device // Create device
@ -59,22 +65,14 @@ func (s Service) Create(payload CreateOptions) error {
func (payload CreateOptions) newUser() (result User, err error) { func (payload CreateOptions) newUser() (result User, err error) {
timeNow := now() timeNow := now()
// New RoleID from string
roleID, isValid := mongodb.NewIDFromString(payload.RoleID)
if !isValid {
err = errors.New("invalid roleID")
return
}
return User{ return User{
ID: mongodb.NewObjectID(), ID: mongodb.NewObjectID(),
Name: payload.Name, Name: payload.Name,
Phone: payload.Phone, Phone: payload.Phone,
Email: payload.Email, Email: payload.Email,
HashPassword: payload.HashPassword, HashedPassword: payload.HashedPassword,
Status: payload.Status, Status: payload.Status,
RoleID: roleID, RoleID: payload.RoleID,
Other: payload.Other, Other: payload.Other,
CreatedAt: timeNow, CreatedAt: timeNow,
UpdatedAt: timeNow, UpdatedAt: timeNow,

View File

@ -3,6 +3,7 @@ package usermngmt
// Constant ... // Constant ...
const ( const (
tableUser = "users" tableUser = "users"
tableRole = "roles"
timezoneHCM = "Asia/Ho_Chi_Minh" timezoneHCM = "Asia/Ho_Chi_Minh"
) )

68
db.go Normal file
View File

@ -0,0 +1,68 @@
package usermngmt
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// getUserCollection ...
func (s Service) getUserCollection() *mongo.Collection {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
}
// getRoleCollection ...
func (s Service) getRoleCollection() *mongo.Collection {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
}
func (s Service) isPhoneNumberOrEmailAlreadyExisted(ctx context.Context, phone, email string) bool {
var (
col = s.getUserCollection()
user = User{}
)
// Find
cond := bson.M{
"$or": []bson.M{
{
"phone": phone,
},
{
"email": email,
},
},
}
if err := col.FindOne(ctx, cond).Decode(&user); err != nil {
logger.Error("usermngmt - findByCondition", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return !user.ID.IsZero()
}
func (s Service) isRoleIDAlreadyExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = s.getRoleCollection()
role = Role{}
)
// Find
cond := bson.M{
"_id": roleID,
}
if err := col.FindOne(ctx, cond).Decode(&role); err != nil {
logger.Error("usermngmt - findRoleByCondition", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return !role.ID.IsZero()
}

View File

@ -1,42 +1 @@
package usermngmt package usermngmt
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// getUserCollection ...
func (s Service) getUserCollection() *mongo.Collection {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
}
func (s Service) haveNameOrPhoneExisted(ctx context.Context, phone, email string) bool {
var (
col = s.getUserCollection()
user = User{}
)
// Find
cond := bson.M{
"$or": []bson.M{
{
"phone": phone,
},
{
"email": email,
},
},
}
if err := col.FindOne(ctx, cond).Decode(&user); err != nil {
logger.Error("usermngmt - findByCondition", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return !user.ID.IsZero()
}

View File

@ -12,10 +12,20 @@ type User struct {
Name string `bson:"name" json:"name"` Name string `bson:"name" json:"name"`
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
HashPassword string `bson:"hashPassword" json:"hashPassword"` HashedPassword string `bson:"hashedPassword" json:"hashedPassword"`
Status string `bson:"status" json:"status"` Status string `bson:"status" json:"status"`
RoleID primitive.ObjectID `bson:"roleID" json:"roleId"` RoleID primitive.ObjectID `bson:"roleID" json:"roleId"`
Other string `bson:"other" json:"other"` Other string `bson:"other" json:"other"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
} }
// Role ...
type Role struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
Code string `bson:"code" json:"code"`
IsAdmin bool `bson:"isAdmin" json:"isAdmin"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}

View File

@ -31,12 +31,12 @@ func (co CreateOptions) validate() error {
return errors.New("no email data") return errors.New("no email data")
} }
// HashPassword // HashedPassword
if co.HashPassword == "" { if co.HashedPassword == "" {
logger.Error("usermngmt - Create: no hashPassword data", logger.LogData{ logger.Error("usermngmt - Create: no hashedPassword data", logger.LogData{
"payload": co, "payload": co,
}) })
return errors.New("no hashPassword data") return errors.New("no hashedPassword data")
} }
// Status // Status
@ -48,11 +48,11 @@ func (co CreateOptions) validate() error {
} }
// RoleID // RoleID
if co.RoleID == "" { if co.RoleID.IsZero() {
logger.Error("usermngmt - Create: no roleID data", logger.LogData{ logger.Error("usermngmt - Create: invalid roleID data", logger.LogData{
"payload": co, "payload": co,
}) })
return errors.New("no roleID data") return errors.New("invalid roleID data")
} }
return nil return nil