fix comment again
This commit is contained in:
parent
d620c7c40e
commit
518de0245a
|
@ -2,11 +2,8 @@ package usermngmt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateOptions ...
|
// CreateOptions ...
|
||||||
|
@ -16,14 +13,13 @@ type CreateOptions struct {
|
||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
Status string
|
Status string
|
||||||
RoleID primitive.ObjectID
|
RoleID string
|
||||||
Other string
|
Other string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create ...
|
// Create ...
|
||||||
func (s Service) Create(payload CreateOptions) error {
|
func (s Service) Create(payload CreateOptions) error {
|
||||||
var (
|
var (
|
||||||
col = s.getUserCollection()
|
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,19 +30,15 @@ func (s Service) Create(payload CreateOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New user data from payload
|
// New user data from payload
|
||||||
userData, err := payload.newUser()
|
doc, err := payload.newUser()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create device
|
// Create user
|
||||||
_, err = col.InsertOne(ctx, userData)
|
err = s.userCreate(ctx, doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("usermngmt - Create", logger.LogData{
|
return err
|
||||||
"doc": userData,
|
|
||||||
"err": err.Error(),
|
|
||||||
})
|
|
||||||
return fmt.Errorf("error when create user: %s", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -54,6 +46,7 @@ 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()
|
||||||
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
||||||
return User{
|
return User{
|
||||||
ID: mongodb.NewObjectID(),
|
ID: mongodb.NewObjectID(),
|
||||||
Name: payload.Name,
|
Name: payload.Name,
|
||||||
|
@ -61,7 +54,7 @@ func (payload CreateOptions) newUser() (result User, err error) {
|
||||||
Email: payload.Email,
|
Email: payload.Email,
|
||||||
HashedPassword: hashPassword(payload.Password),
|
HashedPassword: hashPassword(payload.Password),
|
||||||
Status: payload.Status,
|
Status: payload.Status,
|
||||||
RoleID: payload.RoleID,
|
RoleID: roleID,
|
||||||
Other: payload.Other,
|
Other: payload.Other,
|
||||||
CreatedAt: timeNow,
|
CreatedAt: timeNow,
|
||||||
UpdatedAt: timeNow,
|
UpdatedAt: timeNow,
|
||||||
|
|
|
@ -2,8 +2,9 @@ package usermngmt
|
||||||
|
|
||||||
// Constant ...
|
// Constant ...
|
||||||
const (
|
const (
|
||||||
tableUser = "users"
|
tableUser = "users"
|
||||||
tableRole = "roles"
|
tableRole = "roles"
|
||||||
|
tablePrefixDefault = "usermngmt"
|
||||||
|
|
||||||
timezoneHCM = "Asia/Ho_Chi_Minh"
|
timezoneHCM = "Asia/Ho_Chi_Minh"
|
||||||
|
|
||||||
|
|
27
db.go
27
db.go
|
@ -12,18 +12,12 @@ import (
|
||||||
|
|
||||||
// getUserCollection ...
|
// getUserCollection ...
|
||||||
func (s Service) getUserCollection() *mongo.Collection {
|
func (s Service) getUserCollection() *mongo.Collection {
|
||||||
if s.TablePrefix != "" {
|
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
|
||||||
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
|
|
||||||
}
|
|
||||||
return s.DB.Collection(tableUser)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRoleCollection ...
|
// getRoleCollection ...
|
||||||
func (s Service) getRoleCollection() *mongo.Collection {
|
func (s Service) getRoleCollection() *mongo.Collection {
|
||||||
if s.TablePrefix != "" {
|
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
|
||||||
s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
|
|
||||||
}
|
|
||||||
return s.DB.Collection(tableRole)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
|
func (s Service) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
|
||||||
|
@ -72,3 +66,20 @@ func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
|
||||||
}
|
}
|
||||||
return total != 0
|
return total != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Service) userCreate(ctx context.Context, doc User) error {
|
||||||
|
var (
|
||||||
|
col = s.getUserCollection()
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err := col.InsertOne(ctx, doc)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("usermngmt - Create", logger.LogData{
|
||||||
|
"doc": doc,
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return fmt.Errorf("error when create user: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,11 @@ func Init(config Config) (*Service, error) {
|
||||||
return nil, errors.New("please provide all necessary information for init user")
|
return nil, errors.New("please provide all necessary information for init user")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If prefixTable is empty then it is usermngmt
|
||||||
|
if config.TablePrefix == "" {
|
||||||
|
config.TablePrefix = tablePrefixDefault
|
||||||
|
}
|
||||||
|
|
||||||
// Connect MongoDB
|
// Connect MongoDB
|
||||||
db, err := mongodb.Connect(
|
db, err := mongodb.Connect(
|
||||||
config.MongoDB.Host,
|
config.MongoDB.Host,
|
||||||
|
|
13
validate.go
13
validate.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
"github.com/Selly-Modules/logger"
|
||||||
|
"github.com/Selly-Modules/mongodb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (co CreateOptions) validate(ctx context.Context) error {
|
func (co CreateOptions) validate(ctx context.Context) error {
|
||||||
|
@ -49,15 +50,19 @@ func (co CreateOptions) validate(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoleID
|
// RoleID
|
||||||
if co.RoleID.IsZero() {
|
if co.RoleID == "" {
|
||||||
logger.Error("usermngmt - Create: invalid roleID data", logger.LogData{
|
logger.Error("usermngmt - Create: no roleID data", logger.LogData{
|
||||||
"payload": co,
|
"payload": co,
|
||||||
})
|
})
|
||||||
return errors.New("invalid roleID data")
|
return errors.New("no role id data")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find roleID exists or not
|
// Find roleID exists or not
|
||||||
if !s.isRoleIDExisted(ctx, co.RoleID) {
|
roleID, isValid := mongodb.NewIDFromString(co.RoleID)
|
||||||
|
if !isValid {
|
||||||
|
return errors.New("invalid role id data")
|
||||||
|
}
|
||||||
|
if !s.isRoleIDExisted(ctx, roleID) {
|
||||||
return errors.New("role id does not exist")
|
return errors.New("role id does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue