From e95ab2ec0f786136298d0fe13ff3623f83be62fa Mon Sep 17 00:00:00 2001 From: Hoang Date: Wed, 10 Nov 2021 11:01:39 +0700 Subject: [PATCH] fix comment again --- action.go | 8 ++-- constant.go | 2 - database/db.go | 31 +++++++++++++++ db.go | 12 ------ {internal/model => model}/db.go | 0 {internal/model => model}/query.go | 0 model/role_request.go | 7 ++++ .../model/role.go => model/role_response.go | 7 +--- .../model/user.go => model/user_request.go | 38 ++++-------------- model/user_response.go | 26 +++++++++++++ role/db.go | 6 ++- role/handle.go | 4 +- user/db.go | 39 ++++++++++++++----- user/handle.go | 13 +++---- usermngmt.go | 20 ++++------ 15 files changed, 124 insertions(+), 89 deletions(-) create mode 100644 database/db.go delete mode 100644 db.go rename {internal/model => model}/db.go (100%) rename {internal/model => model}/query.go (100%) create mode 100644 model/role_request.go rename internal/model/role.go => model/role_response.go (63%) rename internal/model/user.go => model/user_request.go (78%) create mode 100644 model/user_response.go diff --git a/action.go b/action.go index 5608282..64d3acb 100644 --- a/action.go +++ b/action.go @@ -1,16 +1,16 @@ package usermngmt import ( - "github.com/Selly-Modules/usermngmt/internal/model" + "github.com/Selly-Modules/usermngmt/model" ) // Create ... -func (s Service) Create(payload model.CreateOptions) error { +func (s Service) Create(payload model.UserCreateOptions) error { return s.handler.User.Create(payload) } // Update ... -func (s Service) Update(userID string, payload model.UpdateOptions) error { +func (s Service) Update(userID string, payload model.UserUpdateOptions) error { return s.handler.User.UpdateByUserID(userID, payload) } @@ -23,7 +23,7 @@ func (s Service) ChangeUserStatus(userID, newStatus string) error { return s.handler.User.ChangeUserStatus(userID, newStatus) } -func (s Service) All(query model.AllQuery) model.UserAll { +func (s Service) All(query model.UserAllQuery) model.UserAll { return s.handler.User.All(query) } diff --git a/constant.go b/constant.go index 4252788..fe98da9 100644 --- a/constant.go +++ b/constant.go @@ -2,7 +2,5 @@ package usermngmt // Constant ... const ( - tableUser = "users" - tableRole = "roles" tablePrefixDefault = "usermngmt" ) diff --git a/database/db.go b/database/db.go new file mode 100644 index 0000000..6f30c60 --- /dev/null +++ b/database/db.go @@ -0,0 +1,31 @@ +package database + +import ( + "fmt" + + "go.mongodb.org/mongo-driver/mongo" +) + +// Table +var ( + tableUser = "users" + tableRole = "roles" +) + +var ( + db *mongo.Database + prefix string +) + +func Set(instance *mongo.Database, tablePrefix string) { + db = instance + prefix = tablePrefix +} + +func GetUserCol() *mongo.Collection { + return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser)) +} + +func GetRoleCol() *mongo.Collection { + return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole)) +} diff --git a/db.go b/db.go deleted file mode 100644 index 2b765fe..0000000 --- a/db.go +++ /dev/null @@ -1,12 +0,0 @@ -package usermngmt - -import ( - "fmt" - - "go.mongodb.org/mongo-driver/mongo" -) - -// getCollectionName ... -func (s Service) getCollectionName(tablePrefix, table string) *mongo.Collection { - return s.db.Collection(fmt.Sprintf("%s-%s", tablePrefix, table)) -} diff --git a/internal/model/db.go b/model/db.go similarity index 100% rename from internal/model/db.go rename to model/db.go diff --git a/internal/model/query.go b/model/query.go similarity index 100% rename from internal/model/query.go rename to model/query.go diff --git a/model/role_request.go b/model/role_request.go new file mode 100644 index 0000000..db992a0 --- /dev/null +++ b/model/role_request.go @@ -0,0 +1,7 @@ +package model + +// RoleCreateOptions ... +type RoleCreateOptions struct { + Name string +} + diff --git a/internal/model/role.go b/model/role_response.go similarity index 63% rename from internal/model/role.go rename to model/role_response.go index 1f381b1..0c84d23 100644 --- a/internal/model/role.go +++ b/model/role_response.go @@ -4,9 +4,4 @@ type RoleShort struct { ID string `json:"_id"` Name string `json:"name"` IsAdmin bool `json:"isAdmin"` -} - -// RoleCreateOptions ... -type RoleCreateOptions struct { - Name string -} +} \ No newline at end of file diff --git a/internal/model/user.go b/model/user_request.go similarity index 78% rename from internal/model/user.go rename to model/user_request.go index 34562a2..d27b3f1 100644 --- a/internal/model/user.go +++ b/model/user_request.go @@ -2,13 +2,12 @@ package model import ( "errors" - "time" "github.com/Selly-Modules/logger" ) -// CreateOptions ... -type CreateOptions struct { +// UserCreateOptions ... +type UserCreateOptions struct { Name string Phone string Email string @@ -18,8 +17,8 @@ type CreateOptions struct { Other string } -// UpdateOptions ... -type UpdateOptions struct { +// UserUpdateOptions ... +type UserUpdateOptions struct { Name string Phone string Email string @@ -33,8 +32,8 @@ type ChangePasswordOptions struct { NewPassword string } -// AllQuery ... -type AllQuery struct { +// UserAllQuery ... +type UserAllQuery struct { Page int64 Limit int64 Keyword string @@ -42,29 +41,8 @@ type AllQuery struct { Status string } -// User ... -type User struct { - ID string `json:"_id"` - Name string `json:"name"` - Phone string `json:"phone"` - Email string `json:"email"` - Status string `json:"status"` - Role RoleShort `json:"role"` - Other string `json:"other"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` -} - -type ( - // UserAll ... - UserAll struct { - List []User `json:"list"` - Total int64 `json:"total"` - } -) - // Validate ... -func (co CreateOptions) Validate() error { +func (co UserCreateOptions) Validate() error { // Name if co.Name == "" { logger.Error("usermngmt - Create: no Name data", logger.LogData{ @@ -117,7 +95,7 @@ func (co CreateOptions) Validate() error { } // Validate ... -func (uo UpdateOptions) Validate() error { +func (uo UserUpdateOptions) Validate() error { // Name if uo.Name == "" { logger.Error("usermngmt - Update: no name data", logger.LogData{ diff --git a/model/user_response.go b/model/user_response.go new file mode 100644 index 0000000..5344b7b --- /dev/null +++ b/model/user_response.go @@ -0,0 +1,26 @@ +package model + +import ( + "time" +) + +// User ... +type User struct { + ID string `json:"_id"` + Name string `json:"name"` + Phone string `json:"phone"` + Email string `json:"email"` + Status string `json:"status"` + Role RoleShort `json:"role"` + Other string `json:"other"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` +} + +type ( + // UserAll ... + UserAll struct { + List []User `json:"list"` + Total int64 `json:"total"` + } +) diff --git a/role/db.go b/role/db.go index bf98c99..3871ac4 100644 --- a/role/db.go +++ b/role/db.go @@ -3,7 +3,8 @@ package role import ( "context" - "github.com/Selly-Modules/usermngmt/internal/model" + "github.com/Selly-Modules/usermngmt/database" + "github.com/Selly-Modules/usermngmt/model" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -11,7 +12,8 @@ import ( func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( doc model.DBRole + col = database.GetRoleCol() ) - err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) + err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } diff --git a/role/handle.go b/role/handle.go index b00b9b1..6cd48c4 100644 --- a/role/handle.go +++ b/role/handle.go @@ -3,13 +3,11 @@ package role import ( "context" - "github.com/Selly-Modules/usermngmt/internal/model" + "github.com/Selly-Modules/usermngmt/model" "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" ) type Handle struct { - Col *mongo.Collection } // FindByID ... diff --git a/user/db.go b/user/db.go index 0df6cf6..493bafd 100644 --- a/user/db.go +++ b/user/db.go @@ -5,13 +5,17 @@ import ( "fmt" "github.com/Selly-Modules/logger" - "github.com/Selly-Modules/usermngmt/internal/model" + "github.com/Selly-Modules/usermngmt/database" + "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" ) func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { + var ( + col = database.GetUserCol() + ) // Find cond := bson.M{ "$or": []bson.M{ @@ -23,7 +27,7 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st }, }, } - total, err := h.Col.CountDocuments(ctx, cond) + total, err := col.CountDocuments(ctx, cond) if err != nil { logger.Error("usermngmt - countUserByCondition", logger.LogData{ "condition": cond, @@ -35,11 +39,14 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st } func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { + var ( + col = database.GetRoleCol() + ) // Find cond := bson.M{ "_id": roleID, } - total, err := h.RoleCol.CountDocuments(ctx, cond) + total, err := col.CountDocuments(ctx, cond) if err != nil { logger.Error("usermngmt - countRoleByCondition", logger.LogData{ "condition": cond, @@ -53,13 +60,17 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { var ( doc model.DBRole + col = database.GetRoleCol() ) - err := h.RoleCol.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) + err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } func (h Handle) create(ctx context.Context, doc model.DBUser) error { - _, err := h.Col.InsertOne(ctx, doc) + var ( + col = database.GetUserCol() + ) + _, err := col.InsertOne(ctx, doc) if err != nil { logger.Error("usermngmt - Create", logger.LogData{ "doc": doc, @@ -72,7 +83,10 @@ func (h Handle) create(ctx context.Context, doc model.DBUser) error { } func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { - _, err := h.Col.UpdateOne(ctx, cond, payload) + var ( + col = database.GetUserCol() + ) + _, err := col.UpdateOne(ctx, cond, payload) if err != nil { logger.Error("usermngmt - Update", logger.LogData{ "cond": cond, @@ -88,15 +102,19 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) { var ( doc model.DBUser + col = database.GetUserCol() ) - err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) + err := 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 []model.DBUser) { + var ( + col = database.GetUserCol() + ) docs = make([]model.DBUser, 0) - cursor, err := h.Col.Find(ctx, cond, opts...) + cursor, err := col.Find(ctx, cond, opts...) if err != nil { logger.Error("usermngmt - All", logger.LogData{ "cond": cond, @@ -119,7 +137,10 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...* // countByCondition ... func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 { - total, err := h.Col.CountDocuments(ctx, cond) + var ( + col = database.GetUserCol() + ) + total, err := col.CountDocuments(ctx, cond) if err != nil { logger.Error("usermngmt - Count", logger.LogData{ "err": err.Error(), diff --git a/user/handle.go b/user/handle.go index f824490..82ca23b 100644 --- a/user/handle.go +++ b/user/handle.go @@ -8,18 +8,15 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/usermngmt/internal" - "github.com/Selly-Modules/usermngmt/internal/model" + "github.com/Selly-Modules/usermngmt/model" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" ) type Handle struct { - Col *mongo.Collection - RoleCol *mongo.Collection } // Create ... -func (h Handle) Create(payload model.CreateOptions) error { +func (h Handle) Create(payload model.UserCreateOptions) error { var ( ctx = context.Background() ) @@ -58,7 +55,7 @@ func (h Handle) Create(payload model.CreateOptions) error { } // newUser ... -func newUser(payload model.CreateOptions) (result model.DBUser, err error) { +func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) { timeNow := internal.Now() roleID, _ := mongodb.NewIDFromString(payload.RoleID) return model.DBUser{ @@ -77,7 +74,7 @@ func newUser(payload model.CreateOptions) (result model.DBUser, err error) { } // All ... -func (h Handle) All(queryParams model.AllQuery) (r model.UserAll) { +func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) { var ( ctx = context.Background() wg sync.WaitGroup @@ -142,7 +139,7 @@ func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []mod } // UpdateByUserID ... -func (h Handle) UpdateByUserID(userID string, payload model.UpdateOptions) error { +func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) error { var ( ctx = context.Background() ) diff --git a/usermngmt.go b/usermngmt.go index 9e90bb7..a8b99ed 100644 --- a/usermngmt.go +++ b/usermngmt.go @@ -5,9 +5,9 @@ import ( "fmt" "github.com/Selly-Modules/mongodb" + "github.com/Selly-Modules/usermngmt/database" "github.com/Selly-Modules/usermngmt/role" "github.com/Selly-Modules/usermngmt/user" - "go.mongodb.org/mongo-driver/mongo" ) // MongoDBConfig ... @@ -32,7 +32,6 @@ type Handler struct { // Service ... type Service struct { config Config - db *mongo.Database handler Handler } @@ -63,19 +62,14 @@ func Init(config Config) (*Service, error) { return nil, err } + // Set database + database.Set(db, config.TablePrefix) + s = &Service{ 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), + handler: Handler{ + User: user.Handle{}, + Role: role.Handle{}, }, }