From 4f4ea8550e8e28108b4da2fe2ba4f53b2f3872fa Mon Sep 17 00:00:00 2001 From: Hoang Date: Wed, 10 Nov 2021 11:42:23 +0700 Subject: [PATCH] fix comment again --- action.go | 17 +++++++++++------ database/db.go | 2 ++ internal/constant.go | 1 - internal/helper.go | 1 - internal/time.go | 1 - model/query.go | 2 +- model/role_response.go | 1 + role/db.go | 2 +- role/handle.go | 9 +++------ user/db.go | 16 ++++++++-------- user/handle.go | 41 +++++++++++++++++++---------------------- usermngmt.go | 15 +-------------- 12 files changed, 47 insertions(+), 61 deletions(-) diff --git a/action.go b/action.go index 64d3acb..6ea86f2 100644 --- a/action.go +++ b/action.go @@ -2,31 +2,36 @@ package usermngmt import ( "github.com/Selly-Modules/usermngmt/model" + "github.com/Selly-Modules/usermngmt/role" + "github.com/Selly-Modules/usermngmt/user" ) // Create ... func (s Service) Create(payload model.UserCreateOptions) error { - return s.handler.User.Create(payload) + return user.Create(payload) } // Update ... func (s Service) Update(userID string, payload model.UserUpdateOptions) error { - return s.handler.User.UpdateByUserID(userID, payload) + return user.UpdateByUserID(userID, payload) } // ChangeUserPassword ... func (s Service) ChangeUserPassword(userID string, payload model.ChangePasswordOptions) error { - return s.handler.User.ChangeUserPassword(userID, payload) + return user.ChangeUserPassword(userID, payload) } +// ChangeUserStatus ... func (s Service) ChangeUserStatus(userID, newStatus string) error { - return s.handler.User.ChangeUserStatus(userID, newStatus) + return user.ChangeUserStatus(userID, newStatus) } +// All ... func (s Service) All(query model.UserAllQuery) model.UserAll { - return s.handler.User.All(query) + return user.All(query) } +// RoleCreate ... func (s Service) RoleCreate(payload model.RoleCreateOptions) error { - return s.handler.Role.Create(payload) + return role.Create(payload) } diff --git a/database/db.go b/database/db.go index 6f30c60..f5ebbcf 100644 --- a/database/db.go +++ b/database/db.go @@ -22,10 +22,12 @@ func Set(instance *mongo.Database, tablePrefix string) { prefix = tablePrefix } +// GetUserCol ... func GetUserCol() *mongo.Collection { return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser)) } +// GetRoleCol ... func GetRoleCol() *mongo.Collection { return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole)) } diff --git a/internal/constant.go b/internal/constant.go index 7a5b32b..c41e88b 100644 --- a/internal/constant.go +++ b/internal/constant.go @@ -6,4 +6,3 @@ const ( passwordHashingCost = 14 ) - diff --git a/internal/helper.go b/internal/helper.go index 584a13d..e99d3b6 100644 --- a/internal/helper.go +++ b/internal/helper.go @@ -33,4 +33,3 @@ func GetSearchString(fieldList ...string) string { } return fmt.Sprintf(format, searchList...) } - diff --git a/internal/time.go b/internal/time.go index f1b88a1..f3acf96 100644 --- a/internal/time.go +++ b/internal/time.go @@ -18,4 +18,3 @@ func getHCMLocation() *time.Location { func Now() time.Time { return time.Now().In(getHCMLocation()) } - diff --git a/model/query.go b/model/query.go index 4ddf74c..b2af79d 100644 --- a/model/query.go +++ b/model/query.go @@ -6,6 +6,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) +// CommonQuery ... type CommonQuery struct { Page int64 Limit int64 @@ -56,4 +57,3 @@ func (q *CommonQuery) SetDefaultLimit() { q.Limit = 20 } } - diff --git a/model/role_response.go b/model/role_response.go index 0c84d23..117cf85 100644 --- a/model/role_response.go +++ b/model/role_response.go @@ -1,5 +1,6 @@ package model +// RoleShort ... type RoleShort struct { ID string `json:"_id"` Name string `json:"name"` diff --git a/role/db.go b/role/db.go index 3871ac4..6b41add 100644 --- a/role/db.go +++ b/role/db.go @@ -9,7 +9,7 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) -func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { +func findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( doc model.DBRole col = database.GetRoleCol() diff --git a/role/handle.go b/role/handle.go index 6cd48c4..9572478 100644 --- a/role/handle.go +++ b/role/handle.go @@ -7,17 +7,14 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) -type Handle struct { -} - // FindByID ... -func (h Handle) FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { - role, err := h.findByID(ctx, id) +func FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { + role, err := findByID(ctx, id) return role, err } // Create ... -func (h Handle) Create(payload model.RoleCreateOptions) error { +func Create(payload model.RoleCreateOptions) error { // TODO later return nil } diff --git a/user/db.go b/user/db.go index 493bafd..7134c53 100644 --- a/user/db.go +++ b/user/db.go @@ -12,7 +12,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { +func isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { var ( col = database.GetUserCol() ) @@ -38,7 +38,7 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st return total != 0 } -func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { +func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { var ( col = database.GetRoleCol() ) @@ -57,7 +57,7 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) return total != 0 } -func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { +func roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( doc model.DBRole col = database.GetRoleCol() @@ -66,7 +66,7 @@ func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model. return doc, err } -func (h Handle) create(ctx context.Context, doc model.DBUser) error { +func create(ctx context.Context, doc model.DBUser) error { var ( col = database.GetUserCol() ) @@ -82,7 +82,7 @@ func (h Handle) create(ctx context.Context, doc model.DBUser) error { return nil } -func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { +func updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { var ( col = database.GetUserCol() ) @@ -99,7 +99,7 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl return err } -func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) { +func findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) { var ( doc model.DBUser col = database.GetUserCol() @@ -108,7 +108,7 @@ func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUs return doc, err } -func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) { +func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) { var ( col = database.GetUserCol() ) @@ -136,7 +136,7 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...* } // countByCondition ... -func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 { +func countByCondition(ctx context.Context, cond interface{}) int64 { var ( col = database.GetUserCol() ) diff --git a/user/handle.go b/user/handle.go index 82ca23b..684013a 100644 --- a/user/handle.go +++ b/user/handle.go @@ -12,11 +12,8 @@ import ( "go.mongodb.org/mongo-driver/bson" ) -type Handle struct { -} - // Create ... -func (h Handle) Create(payload model.UserCreateOptions) error { +func Create(payload model.UserCreateOptions) error { var ( ctx = context.Background() ) @@ -31,12 +28,12 @@ func (h Handle) Create(payload model.UserCreateOptions) error { if !isValid { return errors.New("invalid role id data") } - if !h.isRoleIDExisted(ctx, roleID) { + if !isRoleIDExisted(ctx, roleID) { return errors.New("role id does not exist") } // Find phone number,email exists or not - if h.isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { + if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { return errors.New("phone number or email already existed") } @@ -47,7 +44,7 @@ func (h Handle) Create(payload model.UserCreateOptions) error { } // Create user - if err = h.create(ctx, doc); err != nil { + if err = create(ctx, doc); err != nil { return err } @@ -74,7 +71,7 @@ func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) { } // All ... -func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) { +func All(queryParams model.UserAllQuery) (r model.UserAll) { var ( ctx = context.Background() wg sync.WaitGroup @@ -98,14 +95,14 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) { wg.Add(1) go func() { defer wg.Done() - docs := h.findByCondition(ctx, cond, query.GetFindOptionsUsingPage()) - r.List = h.getResponseList(ctx, docs) + docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage()) + r.List = getResponseList(ctx, docs) }() wg.Add(1) go func() { defer wg.Done() - r.Total = h.countByCondition(ctx, cond) + r.Total = countByCondition(ctx, cond) }() wg.Wait() @@ -113,11 +110,11 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) { return } -func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []model.User { +func getResponseList(ctx context.Context, users []model.DBUser) []model.User { res := make([]model.User, 0) for _, user := range users { - roleRaw, _ := h.roleFindByID(ctx, user.RoleID) + roleRaw, _ := roleFindByID(ctx, user.RoleID) res = append(res, model.User{ ID: user.ID.Hex(), Name: user.Name, @@ -139,7 +136,7 @@ func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []mod } // UpdateByUserID ... -func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) error { +func UpdateByUserID(userID string, payload model.UserUpdateOptions) error { var ( ctx = context.Background() ) @@ -154,12 +151,12 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e if !isValid { return errors.New("invalid role id data") } - if !h.isRoleIDExisted(ctx, roleID) { + if !isRoleIDExisted(ctx, roleID) { return errors.New("role id does not exist") } // Find phone number,email exists or not - if h.isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { + if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { return errors.New("phone number or email already existed") } @@ -183,7 +180,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e } // Update - if err := h.updateOneByCondition(ctx, cond, updateData); err != nil { + if err := updateOneByCondition(ctx, cond, updateData); err != nil { return err } @@ -191,7 +188,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e } // ChangeUserPassword ... -func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { +func ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { var ( ctx = context.Background() ) @@ -213,7 +210,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption // Find user id, _ := mongodb.NewIDFromString(userID) - user, _ := h.findByID(ctx, id) + user, _ := findByID(ctx, id) if user.ID.IsZero() { return errors.New("user not found") } @@ -224,7 +221,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption } // Update password - if err = h.updateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + if err = updateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ "$set": bson.M{ "hashedPassword": internal.HashPassword(opt.NewPassword), "updatedAt": internal.Now(), @@ -237,7 +234,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption } // ChangeUserStatus ... -func (h Handle) ChangeUserStatus(userID, newStatus string) error { +func ChangeUserStatus(userID, newStatus string) error { var ( ctx = context.Background() ) @@ -249,7 +246,7 @@ func (h Handle) ChangeUserStatus(userID, newStatus string) error { } // Update status - if err := h.updateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ + if err := updateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ "$set": bson.M{ "status": newStatus, "updatedAt": internal.Now(), diff --git a/usermngmt.go b/usermngmt.go index a8b99ed..8df9586 100644 --- a/usermngmt.go +++ b/usermngmt.go @@ -6,8 +6,6 @@ import ( "github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/usermngmt/database" - "github.com/Selly-Modules/usermngmt/role" - "github.com/Selly-Modules/usermngmt/user" ) // MongoDBConfig ... @@ -23,16 +21,9 @@ type Config struct { TablePrefix string } -// Handler ... -type Handler struct { - User user.Handle - Role role.Handle -} - // Service ... type Service struct { - config Config - handler Handler + config Config } var s *Service @@ -67,10 +58,6 @@ func Init(config Config) (*Service, error) { s = &Service{ config: config, - handler: Handler{ - User: user.Handle{}, - Role: role.Handle{}, - }, } return s, nil