From 4173187e21be943c7ef0bd3e8aaa6cd8f7f782be Mon Sep 17 00:00:00 2001 From: Hoang Date: Wed, 10 Nov 2021 09:54:49 +0700 Subject: [PATCH] fix comment --- action.go | 26 +++++++++--------- db.go | 11 +++----- init_handle.go | 21 --------------- internal/{ => model}/db_model.go | 3 +-- internal/{ => model}/query.go | 2 +- internal/{ => model}/role_model.go | 2 +- internal/{ => model}/user_model.go | 22 +--------------- role/db.go | 6 ++--- role/handle.go | 6 ++--- user/db.go | 16 ++++++------ user/handle.go | 42 ++++++++++++++++++++++-------- usermngmt.go | 28 +++++++++++++++++--- 12 files changed, 90 insertions(+), 95 deletions(-) delete mode 100644 init_handle.go rename internal/{ => model}/db_model.go (98%) rename internal/{ => model}/query.go (98%) rename internal/{ => model}/role_model.go (91%) rename internal/{ => model}/user_model.go (83%) diff --git a/action.go b/action.go index fb41213..5608282 100644 --- a/action.go +++ b/action.go @@ -1,30 +1,32 @@ package usermngmt -import "github.com/Selly-Modules/usermngmt/internal" +import ( + "github.com/Selly-Modules/usermngmt/internal/model" +) // Create ... -func (s Service) Create(payload internal.CreateOptions) error { - return s.userHandle().Create(payload) +func (s Service) Create(payload model.CreateOptions) error { + return s.handler.User.Create(payload) } // Update ... -func (s Service) Update(userID string, payload internal.UpdateOptions) error { - return s.userHandle().UpdateByUserID(userID, payload) +func (s Service) Update(userID string, payload model.UpdateOptions) error { + return s.handler.User.UpdateByUserID(userID, payload) } // ChangeUserPassword ... -func (s Service) ChangeUserPassword(userID string, payload internal.ChangePasswordOptions) error { - return s.userHandle().ChangeUserPassword(userID, payload) +func (s Service) ChangeUserPassword(userID string, payload model.ChangePasswordOptions) error { + return s.handler.User.ChangeUserPassword(userID, payload) } func (s Service) ChangeUserStatus(userID, newStatus string) error { - return s.userHandle().ChangeUserStatus(userID, newStatus) + return s.handler.User.ChangeUserStatus(userID, newStatus) } -func (s Service) All(query internal.AllQuery) internal.UserAll { - return s.userHandle().All(query) +func (s Service) All(query model.AllQuery) model.UserAll { + return s.handler.User.All(query) } -func (s Service) RoleCreate(payload internal.RoleCreateOptions) error { - return s.roleHandle().Create(payload) +func (s Service) RoleCreate(payload model.RoleCreateOptions) error { + return s.handler.Role.Create(payload) } diff --git a/db.go b/db.go index ef436a8..2b765fe 100644 --- a/db.go +++ b/db.go @@ -6,12 +6,7 @@ import ( "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)) +// getCollectionName ... +func (s Service) getCollectionName(tablePrefix, table string) *mongo.Collection { + return s.db.Collection(fmt.Sprintf("%s-%s", tablePrefix, table)) } diff --git a/init_handle.go b/init_handle.go deleted file mode 100644 index 30816b3..0000000 --- a/init_handle.go +++ /dev/null @@ -1,21 +0,0 @@ -package usermngmt - -import ( - "github.com/Selly-Modules/usermngmt/role" - "github.com/Selly-Modules/usermngmt/user" -) - -// userHandle ... -func (s Service) userHandle() user.Handle { - return user.Handle{ - Col: s.getUserCollection(), - RoleCol: s.getRoleCollection(), - } -} - -// roleHandle ... -func (s Service) roleHandle() role.Handle { - return role.Handle{ - Col: s.getRoleCollection(), - } -} diff --git a/internal/db_model.go b/internal/model/db_model.go similarity index 98% rename from internal/db_model.go rename to internal/model/db_model.go index 8b183bb..53aca13 100644 --- a/internal/db_model.go +++ b/internal/model/db_model.go @@ -1,4 +1,4 @@ -package internal +package model import ( "time" @@ -30,4 +30,3 @@ type DBUser struct { CreatedAt time.Time `bson:"createdAt"` UpdatedAt time.Time `bson:"updatedAt"` } - diff --git a/internal/query.go b/internal/model/query.go similarity index 98% rename from internal/query.go rename to internal/model/query.go index d4bf9a7..4ddf74c 100644 --- a/internal/query.go +++ b/internal/model/query.go @@ -1,4 +1,4 @@ -package internal +package model import ( "github.com/Selly-Modules/mongodb" diff --git a/internal/role_model.go b/internal/model/role_model.go similarity index 91% rename from internal/role_model.go rename to internal/model/role_model.go index 6db9c33..1f381b1 100644 --- a/internal/role_model.go +++ b/internal/model/role_model.go @@ -1,4 +1,4 @@ -package internal +package model type RoleShort struct { ID string `json:"_id"` diff --git a/internal/user_model.go b/internal/model/user_model.go similarity index 83% rename from internal/user_model.go rename to internal/model/user_model.go index 333ae7a..34562a2 100644 --- a/internal/user_model.go +++ b/internal/model/user_model.go @@ -1,11 +1,10 @@ -package internal +package model import ( "errors" "time" "github.com/Selly-Modules/logger" - "github.com/Selly-Modules/mongodb" ) // CreateOptions ... @@ -64,25 +63,6 @@ type ( } ) -// NewUser ... -func (payload CreateOptions) NewUser() (result DBUser, err error) { - timeNow := Now() - roleID, _ := mongodb.NewIDFromString(payload.RoleID) - return DBUser{ - ID: mongodb.NewObjectID(), - Name: payload.Name, - SearchString: GetSearchString(payload.Name, payload.Phone, payload.Email), - Phone: payload.Phone, - Email: payload.Email, - HashedPassword: HashPassword(payload.Password), - Status: payload.Status, - RoleID: roleID, - Other: payload.Other, - CreatedAt: timeNow, - UpdatedAt: timeNow, - }, nil -} - // Validate ... func (co CreateOptions) Validate() error { // Name diff --git a/role/db.go b/role/db.go index 2a7793c..bf98c99 100644 --- a/role/db.go +++ b/role/db.go @@ -3,14 +3,14 @@ package role import ( "context" - "github.com/Selly-Modules/usermngmt/internal" + "github.com/Selly-Modules/usermngmt/internal/model" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) -func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (internal.DBRole, error) { +func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( - doc internal.DBRole + doc model.DBRole ) err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err diff --git a/role/handle.go b/role/handle.go index 0178083..b00b9b1 100644 --- a/role/handle.go +++ b/role/handle.go @@ -3,7 +3,7 @@ package role import ( "context" - "github.com/Selly-Modules/usermngmt/internal" + "github.com/Selly-Modules/usermngmt/internal/model" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) @@ -13,13 +13,13 @@ type Handle struct { } // FindByID ... -func (h Handle) FindByID(ctx context.Context, id primitive.ObjectID) (internal.DBRole, error) { +func (h Handle) FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { role, err := h.findByID(ctx, id) return role, err } // Create ... -func (h Handle) Create(payload internal.RoleCreateOptions) error { +func (h Handle) Create(payload model.RoleCreateOptions) error { // TODO later return nil } diff --git a/user/db.go b/user/db.go index 6316ab2..0df6cf6 100644 --- a/user/db.go +++ b/user/db.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/Selly-Modules/logger" - "github.com/Selly-Modules/usermngmt/internal" + "github.com/Selly-Modules/usermngmt/internal/model" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" @@ -50,15 +50,15 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) return total != 0 } -func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (internal.DBRole, error) { +func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( - doc internal.DBRole + doc model.DBRole ) err := h.RoleCol.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } -func (h Handle) create(ctx context.Context, doc internal.DBUser) error { +func (h Handle) create(ctx context.Context, doc model.DBUser) error { _, err := h.Col.InsertOne(ctx, doc) if err != nil { logger.Error("usermngmt - Create", logger.LogData{ @@ -85,16 +85,16 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl return err } -func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (internal.DBUser, error) { +func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) { var ( - doc internal.DBUser + doc model.DBUser ) err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } -func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []internal.DBUser) { - docs = make([]internal.DBUser, 0) +func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) { + docs = make([]model.DBUser, 0) cursor, err := h.Col.Find(ctx, cond, opts...) if err != nil { diff --git a/user/handle.go b/user/handle.go index 8bf935c..f824490 100644 --- a/user/handle.go +++ b/user/handle.go @@ -8,6 +8,7 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/usermngmt/internal" + "github.com/Selly-Modules/usermngmt/internal/model" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) @@ -18,7 +19,7 @@ type Handle struct { } // Create ... -func (h Handle) Create(payload internal.CreateOptions) error { +func (h Handle) Create(payload model.CreateOptions) error { var ( ctx = context.Background() ) @@ -43,7 +44,7 @@ func (h Handle) Create(payload internal.CreateOptions) error { } // New user data from payload - doc, err := payload.NewUser() + doc, err := newUser(payload) if err != nil { return err } @@ -56,14 +57,33 @@ func (h Handle) Create(payload internal.CreateOptions) error { return nil } +// newUser ... +func newUser(payload model.CreateOptions) (result model.DBUser, err error) { + timeNow := internal.Now() + roleID, _ := mongodb.NewIDFromString(payload.RoleID) + return model.DBUser{ + ID: mongodb.NewObjectID(), + Name: payload.Name, + SearchString: internal.GetSearchString(payload.Name, payload.Phone, payload.Email), + Phone: payload.Phone, + Email: payload.Email, + HashedPassword: internal.HashPassword(payload.Password), + Status: payload.Status, + RoleID: roleID, + Other: payload.Other, + CreatedAt: timeNow, + UpdatedAt: timeNow, + }, nil +} + // All ... -func (h Handle) All(queryParams internal.AllQuery) (r internal.UserAll) { +func (h Handle) All(queryParams model.AllQuery) (r model.UserAll) { var ( ctx = context.Background() wg sync.WaitGroup cond = bson.M{} ) - query := internal.CommonQuery{ + query := model.CommonQuery{ Page: queryParams.Page, Limit: queryParams.Limit, Keyword: queryParams.Keyword, @@ -96,18 +116,18 @@ func (h Handle) All(queryParams internal.AllQuery) (r internal.UserAll) { return } -func (h Handle) getResponseList(ctx context.Context, users []internal.DBUser) []internal.User { - res := make([]internal.User, 0) +func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []model.User { + res := make([]model.User, 0) for _, user := range users { roleRaw, _ := h.roleFindByID(ctx, user.RoleID) - res = append(res, internal.User{ + res = append(res, model.User{ ID: user.ID.Hex(), Name: user.Name, Phone: user.Phone, Email: user.Email, Status: user.Status, - Role: internal.RoleShort{ + Role: model.RoleShort{ ID: roleRaw.ID.Hex(), Name: roleRaw.Name, IsAdmin: roleRaw.IsAdmin, @@ -122,7 +142,7 @@ func (h Handle) getResponseList(ctx context.Context, users []internal.DBUser) [] } // UpdateByUserID ... -func (h Handle) UpdateByUserID(userID string, payload internal.UpdateOptions) error { +func (h Handle) UpdateByUserID(userID string, payload model.UpdateOptions) error { var ( ctx = context.Background() ) @@ -174,7 +194,7 @@ func (h Handle) UpdateByUserID(userID string, payload internal.UpdateOptions) er } // ChangeUserPassword ... -func (h Handle) ChangeUserPassword(userID string, opt internal.ChangePasswordOptions) error { +func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { var ( ctx = context.Background() ) @@ -189,7 +209,7 @@ func (h Handle) ChangeUserPassword(userID string, opt internal.ChangePasswordOpt if _, isValid := mongodb.NewIDFromString(userID); !isValid { logger.Error("usermngmt - ChangePassword: invalid userID data", logger.LogData{ "payload": opt, - "userID": userID, + "userID": userID, }) return errors.New("invalid user id data") } diff --git a/usermngmt.go b/usermngmt.go index 5979cff..9e90bb7 100644 --- a/usermngmt.go +++ b/usermngmt.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/Selly-Modules/mongodb" + "github.com/Selly-Modules/usermngmt/role" + "github.com/Selly-Modules/usermngmt/user" "go.mongodb.org/mongo-driver/mongo" ) @@ -21,10 +23,17 @@ type Config struct { TablePrefix string } +// Handler ... +type Handler struct { + User user.Handle + Role role.Handle +} + // Service ... type Service struct { - Config - DB *mongo.Database + config Config + db *mongo.Database + handler Handler } var s *Service @@ -55,8 +64,19 @@ func Init(config Config) (*Service, error) { } s = &Service{ - Config: config, - DB: db, + config: config, + db: db, + } + + // Setup handle + s.handler = Handler{ + User: user.Handle{ + Col: s.getCollectionName(config.TablePrefix, tableUser), + RoleCol: s.getCollectionName(config.TablePrefix, tableRole), + }, + Role: role.Handle{ + Col: s.getCollectionName(config.TablePrefix, tableRole), + }, } return s, nil