fix comment again

This commit is contained in:
Hoang 2021-11-10 11:42:23 +07:00
parent e95ab2ec0f
commit 4f4ea8550e
12 changed files with 47 additions and 61 deletions

View File

@ -2,31 +2,36 @@ package usermngmt
import ( import (
"github.com/Selly-Modules/usermngmt/model" "github.com/Selly-Modules/usermngmt/model"
"github.com/Selly-Modules/usermngmt/role"
"github.com/Selly-Modules/usermngmt/user"
) )
// Create ... // Create ...
func (s Service) Create(payload model.UserCreateOptions) error { func (s Service) Create(payload model.UserCreateOptions) error {
return s.handler.User.Create(payload) return user.Create(payload)
} }
// Update ... // Update ...
func (s Service) Update(userID string, payload model.UserUpdateOptions) error { func (s Service) Update(userID string, payload model.UserUpdateOptions) error {
return s.handler.User.UpdateByUserID(userID, payload) return user.UpdateByUserID(userID, payload)
} }
// ChangeUserPassword ... // ChangeUserPassword ...
func (s Service) ChangeUserPassword(userID string, payload model.ChangePasswordOptions) error { 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 { 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 { 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 { func (s Service) RoleCreate(payload model.RoleCreateOptions) error {
return s.handler.Role.Create(payload) return role.Create(payload)
} }

View File

@ -22,10 +22,12 @@ func Set(instance *mongo.Database, tablePrefix string) {
prefix = tablePrefix prefix = tablePrefix
} }
// GetUserCol ...
func GetUserCol() *mongo.Collection { func GetUserCol() *mongo.Collection {
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser)) return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser))
} }
// GetRoleCol ...
func GetRoleCol() *mongo.Collection { func GetRoleCol() *mongo.Collection {
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole)) return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole))
} }

View File

@ -6,4 +6,3 @@ const (
passwordHashingCost = 14 passwordHashingCost = 14
) )

View File

@ -33,4 +33,3 @@ func GetSearchString(fieldList ...string) string {
} }
return fmt.Sprintf(format, searchList...) return fmt.Sprintf(format, searchList...)
} }

View File

@ -18,4 +18,3 @@ func getHCMLocation() *time.Location {
func Now() time.Time { func Now() time.Time {
return time.Now().In(getHCMLocation()) return time.Now().In(getHCMLocation())
} }

View File

@ -6,6 +6,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
// CommonQuery ...
type CommonQuery struct { type CommonQuery struct {
Page int64 Page int64
Limit int64 Limit int64
@ -56,4 +57,3 @@ func (q *CommonQuery) SetDefaultLimit() {
q.Limit = 20 q.Limit = 20
} }
} }

View File

@ -1,5 +1,6 @@
package model package model
// RoleShort ...
type RoleShort struct { type RoleShort struct {
ID string `json:"_id"` ID string `json:"_id"`
Name string `json:"name"` Name string `json:"name"`

View File

@ -9,7 +9,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive" "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 ( var (
doc model.DBRole doc model.DBRole
col = database.GetRoleCol() col = database.GetRoleCol()

View File

@ -7,17 +7,14 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
type Handle struct {
}
// FindByID ... // FindByID ...
func (h Handle) FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { func FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
role, err := h.findByID(ctx, id) role, err := findByID(ctx, id)
return role, err return role, err
} }
// Create ... // Create ...
func (h Handle) Create(payload model.RoleCreateOptions) error { func Create(payload model.RoleCreateOptions) error {
// TODO later // TODO later
return nil return nil
} }

View File

@ -12,7 +12,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "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 ( var (
col = database.GetUserCol() col = database.GetUserCol()
) )
@ -38,7 +38,7 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st
return total != 0 return total != 0
} }
func (h Handle) 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()
) )
@ -57,7 +57,7 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
return total != 0 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 ( var (
doc model.DBRole doc model.DBRole
col = database.GetRoleCol() col = database.GetRoleCol()
@ -66,7 +66,7 @@ func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.
return doc, err return doc, err
} }
func (h Handle) create(ctx context.Context, doc model.DBUser) error { func create(ctx context.Context, doc model.DBUser) error {
var ( var (
col = database.GetUserCol() col = database.GetUserCol()
) )
@ -82,7 +82,7 @@ func (h Handle) create(ctx context.Context, doc model.DBUser) error {
return nil 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 ( var (
col = database.GetUserCol() col = database.GetUserCol()
) )
@ -99,7 +99,7 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl
return err 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 ( var (
doc model.DBUser doc model.DBUser
col = database.GetUserCol() col = database.GetUserCol()
@ -108,7 +108,7 @@ func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUs
return doc, err 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 ( var (
col = database.GetUserCol() col = database.GetUserCol()
) )
@ -136,7 +136,7 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*
} }
// countByCondition ... // countByCondition ...
func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 { func countByCondition(ctx context.Context, cond interface{}) int64 {
var ( var (
col = database.GetUserCol() col = database.GetUserCol()
) )

View File

@ -12,11 +12,8 @@ import (
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
type Handle struct {
}
// Create ... // Create ...
func (h Handle) Create(payload model.UserCreateOptions) error { func Create(payload model.UserCreateOptions) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
@ -31,12 +28,12 @@ func (h Handle) Create(payload model.UserCreateOptions) error {
if !isValid { if !isValid {
return errors.New("invalid role id data") return errors.New("invalid role id data")
} }
if !h.isRoleIDExisted(ctx, roleID) { if !isRoleIDExisted(ctx, roleID) {
return errors.New("role id does not exist") return errors.New("role id does not exist")
} }
// Find phone number,email exists or not // 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") return errors.New("phone number or email already existed")
} }
@ -47,7 +44,7 @@ func (h Handle) Create(payload model.UserCreateOptions) error {
} }
// Create user // Create user
if err = h.create(ctx, doc); err != nil { if err = create(ctx, doc); err != nil {
return err return err
} }
@ -74,7 +71,7 @@ func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
} }
// All ... // All ...
func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) { func All(queryParams model.UserAllQuery) (r model.UserAll) {
var ( var (
ctx = context.Background() ctx = context.Background()
wg sync.WaitGroup wg sync.WaitGroup
@ -98,14 +95,14 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
docs := h.findByCondition(ctx, cond, query.GetFindOptionsUsingPage()) docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
r.List = h.getResponseList(ctx, docs) r.List = getResponseList(ctx, docs)
}() }()
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
r.Total = h.countByCondition(ctx, cond) r.Total = countByCondition(ctx, cond)
}() }()
wg.Wait() wg.Wait()
@ -113,11 +110,11 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
return 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) res := make([]model.User, 0)
for _, user := range users { for _, user := range users {
roleRaw, _ := h.roleFindByID(ctx, user.RoleID) roleRaw, _ := roleFindByID(ctx, user.RoleID)
res = append(res, model.User{ res = append(res, model.User{
ID: user.ID.Hex(), ID: user.ID.Hex(),
Name: user.Name, Name: user.Name,
@ -139,7 +136,7 @@ func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []mod
} }
// UpdateByUserID ... // UpdateByUserID ...
func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) error { func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
@ -154,12 +151,12 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
if !isValid { if !isValid {
return errors.New("invalid role id data") return errors.New("invalid role id data")
} }
if !h.isRoleIDExisted(ctx, roleID) { if !isRoleIDExisted(ctx, roleID) {
return errors.New("role id does not exist") return errors.New("role id does not exist")
} }
// Find phone number,email exists or not // 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") return errors.New("phone number or email already existed")
} }
@ -183,7 +180,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
} }
// Update // Update
if err := h.updateOneByCondition(ctx, cond, updateData); err != nil { if err := updateOneByCondition(ctx, cond, updateData); err != nil {
return err return err
} }
@ -191,7 +188,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
} }
// ChangeUserPassword ... // ChangeUserPassword ...
func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { func ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
@ -213,7 +210,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
// Find user // Find user
id, _ := mongodb.NewIDFromString(userID) id, _ := mongodb.NewIDFromString(userID)
user, _ := h.findByID(ctx, id) user, _ := findByID(ctx, id)
if user.ID.IsZero() { if user.ID.IsZero() {
return errors.New("user not found") return errors.New("user not found")
} }
@ -224,7 +221,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
} }
// Update password // 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{ "$set": bson.M{
"hashedPassword": internal.HashPassword(opt.NewPassword), "hashedPassword": internal.HashPassword(opt.NewPassword),
"updatedAt": internal.Now(), "updatedAt": internal.Now(),
@ -237,7 +234,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
} }
// ChangeUserStatus ... // ChangeUserStatus ...
func (h Handle) ChangeUserStatus(userID, newStatus string) error { func ChangeUserStatus(userID, newStatus string) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
@ -249,7 +246,7 @@ func (h Handle) ChangeUserStatus(userID, newStatus string) error {
} }
// Update status // 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{ "$set": bson.M{
"status": newStatus, "status": newStatus,
"updatedAt": internal.Now(), "updatedAt": internal.Now(),

View File

@ -6,8 +6,6 @@ import (
"github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/mongodb"
"github.com/Selly-Modules/usermngmt/database" "github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/role"
"github.com/Selly-Modules/usermngmt/user"
) )
// MongoDBConfig ... // MongoDBConfig ...
@ -23,16 +21,9 @@ type Config struct {
TablePrefix string TablePrefix string
} }
// Handler ...
type Handler struct {
User user.Handle
Role role.Handle
}
// Service ... // Service ...
type Service struct { type Service struct {
config Config config Config
handler Handler
} }
var s *Service var s *Service
@ -67,10 +58,6 @@ func Init(config Config) (*Service, error) {
s = &Service{ s = &Service{
config: config, config: config,
handler: Handler{
User: user.Handle{},
Role: role.Handle{},
},
} }
return s, nil return s, nil