From efa28749de9f7d8f6e330e93de6a9e31134f6a0b Mon Sep 17 00:00:00 2001 From: Hoang Date: Tue, 9 Nov 2021 14:16:34 +0700 Subject: [PATCH] fix userModel --- action_create.go | 4 ++-- action_get_all.go | 8 ++++---- db.go | 16 ++++++++-------- db_model.go | 32 ++++++++++++++++++++++++++++++++ model.go | 35 ++++------------------------------- 5 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 db_model.go diff --git a/action_create.go b/action_create.go index 616eae2..d09d1af 100644 --- a/action_create.go +++ b/action_create.go @@ -42,10 +42,10 @@ func (s Service) Create(payload CreateOptions) error { return nil } -func (payload CreateOptions) newUser() (result User, err error) { +func (payload CreateOptions) newUser() (result dbUser, err error) { timeNow := now() roleID, _ := mongodb.NewIDFromString(payload.RoleID) - return User{ + return dbUser{ ID: mongodb.NewObjectID(), Name: payload.Name, SearchString: getSearchString(payload.Name, payload.Phone, payload.Email), diff --git a/action_get_all.go b/action_get_all.go index 47b65c7..a169559 100644 --- a/action_get_all.go +++ b/action_get_all.go @@ -17,7 +17,7 @@ type AllQuery struct { } // All ... -func (s Service) All(queryParams AllQuery) (r ResponseUserAll) { +func (s Service) All(queryParams AllQuery) (r UserAll) { var ( ctx = context.Background() wg sync.WaitGroup @@ -56,12 +56,12 @@ func (s Service) All(queryParams AllQuery) (r ResponseUserAll) { return } -func getResponseList(ctx context.Context, users []User) []ResponseUser { - res := make([]ResponseUser, 0) +func getResponseList(ctx context.Context, users []dbUser) []User { + res := make([]User, 0) for _, user := range users { role, _ := s.roleFindByID(ctx, user.RoleID) - res = append(res, ResponseUser{ + res = append(res, User{ ID: user.ID.Hex(), Name: user.Name, Phone: user.Phone, diff --git a/db.go b/db.go index dbfade4..0e88f3e 100644 --- a/db.go +++ b/db.go @@ -68,7 +68,7 @@ func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) return total != 0 } -func (s Service) userCreate(ctx context.Context, doc User) error { +func (s Service) userCreate(ctx context.Context, doc dbUser) error { var ( col = s.getUserCollection() ) @@ -103,20 +103,20 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{}, return err } -func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (User, error) { +func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (dbUser, error) { var ( col = s.getUserCollection() - doc User + doc dbUser ) err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } -func (s Service) userFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []User) { +func (s Service) userFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []dbUser) { var ( col = s.getUserCollection() ) - docs = make([]User, 0) + docs = make([]dbUser, 0) cursor, err := col.Find(ctx, cond, opts...) if err != nil { @@ -155,11 +155,11 @@ func (s Service) userCountByCondition(ctx context.Context, cond interface{}) int return total } -func (s Service) roleFindByID(ctx context.Context, id primitive.ObjectID) (Role, error) { +func (s Service) roleFindByID(ctx context.Context, id primitive.ObjectID) (dbRole, error) { var ( col = s.getRoleCollection() - doc Role + doc dbRole ) err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err -} \ No newline at end of file +} diff --git a/db_model.go b/db_model.go new file mode 100644 index 0000000..2eb0465 --- /dev/null +++ b/db_model.go @@ -0,0 +1,32 @@ +package usermngmt + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +// dbUser ... +type dbUser struct { + ID primitive.ObjectID `bson:"_id"` + Name string `bson:"name"` + SearchString string `bson:"searchString"` + Phone string `bson:"phone"` // unique + Email string `bson:"email"` // unique + HashedPassword string `bson:"hashedPassword"` + Status string `bson:"status"` + RoleID primitive.ObjectID `bson:"roleId"` + Other string `bson:"other"` + CreatedAt time.Time `bson:"createdAt"` + UpdatedAt time.Time `bson:"updatedAt"` +} + +// dbRole ... +type dbRole struct { + ID primitive.ObjectID `bson:"_id" json:"_id"` + Name string `bson:"name" json:"name"` + Code string `bson:"code" json:"code"` + IsAdmin bool `bson:"isAdmin" json:"isAdmin"` + CreatedAt time.Time `bson:"createdAt" json:"createdAt"` + UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` +} diff --git a/model.go b/model.go index a44ee3d..7fe6391 100644 --- a/model.go +++ b/model.go @@ -2,27 +2,10 @@ package usermngmt import ( "time" - - "go.mongodb.org/mongo-driver/bson/primitive" ) // User ... type User struct { - ID primitive.ObjectID `bson:"_id" json:"_id"` - Name string `bson:"name" json:"name"` - SearchString string `bson:"searchString" json:"-"` - Phone string `bson:"phone" json:"phone"` // unique - Email string `bson:"email" json:"email"` // unique - HashedPassword string `bson:"hashedPassword" json:"-"` - Status string `bson:"status" json:"status"` - RoleID primitive.ObjectID `bson:"roleId" json:"roleId"` - Other string `bson:"other" json:"other"` - CreatedAt time.Time `bson:"createdAt" json:"createdAt"` - UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` -} - -// ResponseUser ... -type ResponseUser struct { ID string `json:"_id"` Name string `json:"name"` Phone string `json:"phone"` @@ -34,16 +17,6 @@ type ResponseUser struct { UpdatedAt time.Time `json:"updatedAt"` } -// Role ... -type Role struct { - ID primitive.ObjectID `bson:"_id" json:"_id"` - Name string `bson:"name" json:"name"` - Code string `bson:"code" json:"code"` - IsAdmin bool `bson:"isAdmin" json:"isAdmin"` - CreatedAt time.Time `bson:"createdAt" json:"createdAt"` - UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` -} - type RoleShort struct { ID string `json:"_id"` Name string `json:"name"` @@ -51,9 +24,9 @@ type RoleShort struct { } type ( - // ResponseUserAll ... - ResponseUserAll struct { - List []ResponseUser `json:"list"` - Total int64 `json:"total"` + // UserAll ... + UserAll struct { + List []User `json:"list"` + Total int64 `json:"total"` } )