update AllCreateMethod

This commit is contained in:
Hoang 2021-11-16 11:11:04 +07:00
parent 3a20ec8cd7
commit ba3b0104ff
8 changed files with 164 additions and 56 deletions

View File

@ -15,7 +15,7 @@ import (
// user methods // user methods
// CreateUser ... // CreateUser ...
func (s Service) CreateUser(payload model.UserCreateOptions) error { func (s Service) CreateUser(payload model.UserCreateOptions) (id string, err error) {
return user.Create(payload) return user.Create(payload)
} }
@ -61,12 +61,14 @@ func (s Service) HasPermission(userID, permission string) bool {
// role methods // role methods
// CreateRole ... // CreateRole ...
func (s Service) CreateRole(payload model.RoleCreateOptions) error { func (s Service) CreateRole(payload model.RoleCreateOptions) (id string, err error) {
if err := role.Create(payload); err != nil { id, err = role.Create(payload)
return err if err != nil {
return
} }
cache.Roles() cache.Roles()
return nil return
} }
// UpdateRole ... // UpdateRole ...
@ -86,12 +88,14 @@ func (s Service) GetAllRoles(query model.RoleAllQuery) model.RoleAll {
// permission methods // permission methods
// CreatePermission ... // CreatePermission ...
func (s Service) CreatePermission(payload model.PermissionCreateOptions) error { func (s Service) CreatePermission(payload model.PermissionCreateOptions) (id string, err error) {
if err := permission.Create(payload); err != nil { id, err = permission.Create(payload)
return err if err != nil {
return
} }
cache.Roles() cache.Roles()
return nil return
} }
// UpdatePermission ... // UpdatePermission ...

View File

@ -95,3 +95,22 @@ func countByCondition(ctx context.Context, cond interface{}) int64 {
} }
return total return total
} }
func isPermissionIDExisted(ctx context.Context, permissionID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
// Find
cond := bson.M{
"_id": permissionID,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}

View File

@ -12,25 +12,26 @@ import (
) )
// Create ... // Create ...
func Create(payload model.PermissionCreateOptions) error { func Create(payload model.PermissionCreateOptions) (result string, err error) {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
// Validate payload // Validate payload
if err := payload.Validate(); err != nil { if err = payload.Validate(); err != nil {
return err return
} }
// New permission data from payload // New permission data from payload
doc := newPermission(payload) doc := newPermission(payload)
// Create permission // Create permission
if err := create(ctx, doc); err != nil { if err = create(ctx, doc); err != nil {
return err return
} }
return nil result = doc.ID.Hex()
return
} }
// newPermission ... // newPermission ...
@ -59,11 +60,14 @@ func Update(permissionID string, payload model.PermissionUpdateOptions) error {
return err return err
} }
// Validate permissionID // Find permissionID exists or not
id, isValid := mongodb.NewIDFromString(permissionID) id, isValid := mongodb.NewIDFromString(permissionID)
if !isValid { if !isValid {
return errors.New("invalid permission id data") return errors.New("invalid permission id data")
} }
if !isPermissionIDExisted(ctx, id) {
return errors.New("permission not found")
}
// Setup condition // Setup condition
cond := bson.M{ cond := bson.M{

View File

@ -7,6 +7,8 @@ import (
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/database" "github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/model" "github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
@ -84,3 +86,22 @@ func countByCondition(ctx context.Context, cond interface{}) int64 {
} }
return total return total
} }
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
// Find
cond := bson.M{
"_id": roleID,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Role - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}

View File

@ -12,25 +12,26 @@ import (
) )
// Create ... // Create ...
func Create(payload model.RoleCreateOptions) error { func Create(payload model.RoleCreateOptions) (result string, err error) {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
// Validate payload // Validate payload
if err := payload.Validate(); err != nil { if err = payload.Validate(); err != nil {
return err return
} }
// New role data from payload // New role data from payload
doc := newRole(payload) doc := newRole(payload)
// Create role // Create role
if err := create(ctx, doc); err != nil { if err = create(ctx, doc); err != nil {
return err return
} }
return nil result = doc.ID.Hex()
return
} }
// newRole ... // newRole ...
@ -63,6 +64,11 @@ func Update(roleID string, payload model.RoleUpdateOptions) error {
return errors.New("invalid role id data") return errors.New("invalid role id data")
} }
// Find roleID exists or not
if !isRoleIDExisted(ctx, id) {
return errors.New("role not found")
}
// Setup condition // Setup condition
cond := bson.M{ cond := bson.M{
"_id": id, "_id": id,
@ -106,7 +112,11 @@ func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
go func() { go func() {
defer wg.Done() defer wg.Done()
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage()) docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
r.List = getResponseList(docs) res := make([]model.Role, 0)
for _, doc := range docs {
res = append(res, getResponse(doc))
}
r.List = res
}() }()
wg.Add(1) wg.Add(1)
@ -120,18 +130,13 @@ func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
return return
} }
func getResponseList(roles []model.DBRole) []model.Role { func getResponse(role model.DBRole) model.Role {
res := make([]model.Role, 0) return model.Role{
for _, role := range roles { ID: role.ID.Hex(),
res = append(res, model.Role{ Name: role.Name,
ID: role.ID.Hex(), Code: role.Code,
Name: role.Name, Level: role.Level,
Code: role.Code, CreatedAt: role.CreatedAt,
Level: role.Level, UpdatedAt: role.UpdatedAt,
CreatedAt: role.CreatedAt,
UpdatedAt: role.UpdatedAt,
})
} }
return res
} }

View File

@ -38,6 +38,44 @@ func isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool
return total != 0 return total != 0
} }
func isPhoneNumberExisted(ctx context.Context, phone string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
"phone": phone,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return total != 0
}
func isEmailExisted(ctx context.Context, email string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
"email": email,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return total != 0
}
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var ( var (
col = database.GetRoleCol() col = database.GetRoleCol()

View File

@ -16,46 +16,47 @@ import (
) )
// Create ... // Create ...
func Create(payload model.UserCreateOptions) error { func Create(payload model.UserCreateOptions) (result string, err error) {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
// Validate payload // Validate payload
if err := payload.Validate(); err != nil { if err = payload.Validate(); err != nil {
return err return
} }
// Find roleID exists or not // Find roleID exists or not
roleID, isValid := mongodb.NewIDFromString(payload.RoleID) roleID, isValid := mongodb.NewIDFromString(payload.RoleID)
if !isValid { if !isValid {
return errors.New("invalid role id data") err = errors.New("invalid role id data")
return
} }
if !isRoleIDExisted(ctx, roleID) { if !isRoleIDExisted(ctx, roleID) {
return errors.New("role id does not exist") err = errors.New("role id does not exist")
return
} }
// Find phone number,email exists or not // Find phone number,email exists or not
if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) {
return errors.New("phone number or email already existed") err = errors.New("phone number or email already existed")
return
} }
// New user data from payload // New user data from payload
doc, err := newUser(payload) doc := newUser(payload)
if err != nil {
return err
}
// Create user // Create user
if err = create(ctx, doc); err != nil { if err = create(ctx, doc); err != nil {
return err return
} }
return nil result = doc.ID.Hex()
return
} }
// newUser ... // newUser ...
func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) { func newUser(payload model.UserCreateOptions) model.DBUser {
timeNow := internal.Now() timeNow := internal.Now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID) roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return model.DBUser{ return model.DBUser{
@ -70,7 +71,7 @@ func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
Other: payload.Other, Other: payload.Other,
CreatedAt: timeNow, CreatedAt: timeNow,
UpdatedAt: timeNow, UpdatedAt: timeNow,
}, nil }
} }
// All ... // All ...
@ -156,13 +157,29 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
return errors.New("role id does not exist") return errors.New("role id does not exist")
} }
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid role id data")
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
return errors.New("user not found")
}
// Find phone number,email exists or not // Find phone number,email exists or not
if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { if user.Phone != payload.Phone {
return errors.New("phone number or email already existed") if isPhoneNumberExisted(ctx, payload.Phone) {
return errors.New("phone number already existed")
}
}
if user.Email != payload.Email {
if isEmailExisted(ctx, payload.Email) {
return errors.New("email already existed")
}
} }
// Setup condition // Setup condition
id, _ := mongodb.NewIDFromString(userID)
cond := bson.M{ cond := bson.M{
"_id": id, "_id": id,
} }

View File

@ -55,12 +55,12 @@ func Init(config Config) (*Service, error) {
return nil, err return nil, err
} }
// Init cache
cache.Init()
// Set database // Set database
database.Set(db, config.TablePrefix) database.Set(db, config.TablePrefix)
// Init cache
cache.Init()
s = &Service{ s = &Service{
config: config, config: config,
} }