fix comment again

This commit is contained in:
Hoang 2021-11-08 18:21:52 +07:00
parent d620c7c40e
commit 518de0245a
5 changed files with 43 additions and 28 deletions

View File

@ -2,11 +2,8 @@ package usermngmt
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// CreateOptions ...
@ -16,14 +13,13 @@ type CreateOptions struct {
Email string
Password string
Status string
RoleID primitive.ObjectID
RoleID string
Other string
}
// Create ...
func (s Service) Create(payload CreateOptions) error {
var (
col = s.getUserCollection()
ctx = context.Background()
)
@ -34,19 +30,15 @@ func (s Service) Create(payload CreateOptions) error {
}
// New user data from payload
userData, err := payload.newUser()
doc, err := payload.newUser()
if err != nil {
return err
}
// Create device
_, err = col.InsertOne(ctx, userData)
// Create user
err = s.userCreate(ctx, doc)
if err != nil {
logger.Error("usermngmt - Create", logger.LogData{
"doc": userData,
"err": err.Error(),
})
return fmt.Errorf("error when create user: %s", err.Error())
return err
}
return nil
@ -54,6 +46,7 @@ func (s Service) Create(payload CreateOptions) error {
func (payload CreateOptions) newUser() (result User, err error) {
timeNow := now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return User{
ID: mongodb.NewObjectID(),
Name: payload.Name,
@ -61,7 +54,7 @@ func (payload CreateOptions) newUser() (result User, err error) {
Email: payload.Email,
HashedPassword: hashPassword(payload.Password),
Status: payload.Status,
RoleID: payload.RoleID,
RoleID: roleID,
Other: payload.Other,
CreatedAt: timeNow,
UpdatedAt: timeNow,

View File

@ -4,6 +4,7 @@ package usermngmt
const (
tableUser = "users"
tableRole = "roles"
tablePrefixDefault = "usermngmt"
timezoneHCM = "Asia/Ho_Chi_Minh"

25
db.go
View File

@ -12,18 +12,12 @@ import (
// getUserCollection ...
func (s Service) getUserCollection() *mongo.Collection {
if s.TablePrefix != "" {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
}
return s.DB.Collection(tableUser)
}
// getRoleCollection ...
func (s Service) getRoleCollection() *mongo.Collection {
if s.TablePrefix != "" {
s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
}
return s.DB.Collection(tableRole)
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
}
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
}
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
}

View File

@ -35,6 +35,11 @@ func Init(config Config) (*Service, error) {
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
db, err := mongodb.Connect(
config.MongoDB.Host,

View File

@ -5,6 +5,7 @@ import (
"errors"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb"
)
func (co CreateOptions) validate(ctx context.Context) error {
@ -49,15 +50,19 @@ func (co CreateOptions) validate(ctx context.Context) error {
}
// RoleID
if co.RoleID.IsZero() {
logger.Error("usermngmt - Create: invalid roleID data", logger.LogData{
if co.RoleID == "" {
logger.Error("usermngmt - Create: no roleID data", logger.LogData{
"payload": co,
})
return errors.New("invalid roleID data")
return errors.New("no role id data")
}
// 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")
}