fix comment
This commit is contained in:
parent
c082388d64
commit
4173187e21
26
action.go
26
action.go
|
@ -1,30 +1,32 @@
|
||||||
package usermngmt
|
package usermngmt
|
||||||
|
|
||||||
import "github.com/Selly-Modules/usermngmt/internal"
|
import (
|
||||||
|
"github.com/Selly-Modules/usermngmt/internal/model"
|
||||||
|
)
|
||||||
|
|
||||||
// Create ...
|
// Create ...
|
||||||
func (s Service) Create(payload internal.CreateOptions) error {
|
func (s Service) Create(payload model.CreateOptions) error {
|
||||||
return s.userHandle().Create(payload)
|
return s.handler.User.Create(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update ...
|
// Update ...
|
||||||
func (s Service) Update(userID string, payload internal.UpdateOptions) error {
|
func (s Service) Update(userID string, payload model.UpdateOptions) error {
|
||||||
return s.userHandle().UpdateByUserID(userID, payload)
|
return s.handler.User.UpdateByUserID(userID, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangeUserPassword ...
|
// ChangeUserPassword ...
|
||||||
func (s Service) ChangeUserPassword(userID string, payload internal.ChangePasswordOptions) error {
|
func (s Service) ChangeUserPassword(userID string, payload model.ChangePasswordOptions) error {
|
||||||
return s.userHandle().ChangeUserPassword(userID, payload)
|
return s.handler.User.ChangeUserPassword(userID, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) ChangeUserStatus(userID, newStatus string) error {
|
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 {
|
func (s Service) All(query model.AllQuery) model.UserAll {
|
||||||
return s.userHandle().All(query)
|
return s.handler.User.All(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) RoleCreate(payload internal.RoleCreateOptions) error {
|
func (s Service) RoleCreate(payload model.RoleCreateOptions) error {
|
||||||
return s.roleHandle().Create(payload)
|
return s.handler.Role.Create(payload)
|
||||||
}
|
}
|
||||||
|
|
11
db.go
11
db.go
|
@ -6,12 +6,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getUserCollection ...
|
// getCollectionName ...
|
||||||
func (s Service) getUserCollection() *mongo.Collection {
|
func (s Service) getCollectionName(tablePrefix, table string) *mongo.Collection {
|
||||||
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
|
return s.db.Collection(fmt.Sprintf("%s-%s", tablePrefix, table))
|
||||||
}
|
|
||||||
|
|
||||||
// getRoleCollection ...
|
|
||||||
func (s Service) getRoleCollection() *mongo.Collection {
|
|
||||||
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
@ -30,4 +30,3 @@ type DBUser struct {
|
||||||
CreatedAt time.Time `bson:"createdAt"`
|
CreatedAt time.Time `bson:"createdAt"`
|
||||||
UpdatedAt time.Time `bson:"updatedAt"`
|
UpdatedAt time.Time `bson:"updatedAt"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package model
|
||||||
|
|
||||||
type RoleShort struct {
|
type RoleShort struct {
|
||||||
ID string `json:"_id"`
|
ID string `json:"_id"`
|
|
@ -1,11 +1,10 @@
|
||||||
package internal
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
"github.com/Selly-Modules/logger"
|
||||||
"github.com/Selly-Modules/mongodb"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateOptions ...
|
// 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 ...
|
// Validate ...
|
||||||
func (co CreateOptions) Validate() error {
|
func (co CreateOptions) Validate() error {
|
||||||
// Name
|
// Name
|
|
@ -3,14 +3,14 @@ package role
|
||||||
import (
|
import (
|
||||||
"context"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"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 (
|
var (
|
||||||
doc internal.DBRole
|
doc model.DBRole
|
||||||
)
|
)
|
||||||
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||||
return doc, err
|
return doc, err
|
||||||
|
|
|
@ -3,7 +3,7 @@ package role
|
||||||
import (
|
import (
|
||||||
"context"
|
"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/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
@ -13,13 +13,13 @@ type Handle struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByID ...
|
// 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)
|
role, err := h.findByID(ctx, id)
|
||||||
return role, err
|
return role, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create ...
|
// Create ...
|
||||||
func (h Handle) Create(payload internal.RoleCreateOptions) error {
|
func (h Handle) Create(payload model.RoleCreateOptions) error {
|
||||||
// TODO later
|
// TODO later
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
16
user/db.go
16
user/db.go
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
@ -50,15 +50,15 @@ 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) (internal.DBRole, error) {
|
func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
|
||||||
var (
|
var (
|
||||||
doc internal.DBRole
|
doc model.DBRole
|
||||||
)
|
)
|
||||||
err := h.RoleCol.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
err := h.RoleCol.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||||
return doc, err
|
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)
|
_, err := h.Col.InsertOne(ctx, doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("usermngmt - Create", logger.LogData{
|
logger.Error("usermngmt - Create", logger.LogData{
|
||||||
|
@ -85,16 +85,16 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl
|
||||||
return err
|
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 (
|
var (
|
||||||
doc internal.DBUser
|
doc model.DBUser
|
||||||
)
|
)
|
||||||
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||||
return doc, err
|
return doc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []internal.DBUser) {
|
func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) {
|
||||||
docs = make([]internal.DBUser, 0)
|
docs = make([]model.DBUser, 0)
|
||||||
|
|
||||||
cursor, err := h.Col.Find(ctx, cond, opts...)
|
cursor, err := h.Col.Find(ctx, cond, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/Selly-Modules/logger"
|
"github.com/Selly-Modules/logger"
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
"github.com/Selly-Modules/usermngmt/internal"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +19,7 @@ type Handle struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create ...
|
// Create ...
|
||||||
func (h Handle) Create(payload internal.CreateOptions) error {
|
func (h Handle) Create(payload model.CreateOptions) error {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
|
@ -43,7 +44,7 @@ func (h Handle) Create(payload internal.CreateOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New user data from payload
|
// New user data from payload
|
||||||
doc, err := payload.NewUser()
|
doc, err := newUser(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -56,14 +57,33 @@ func (h Handle) Create(payload internal.CreateOptions) error {
|
||||||
return nil
|
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 ...
|
// All ...
|
||||||
func (h Handle) All(queryParams internal.AllQuery) (r internal.UserAll) {
|
func (h Handle) All(queryParams model.AllQuery) (r model.UserAll) {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
cond = bson.M{}
|
cond = bson.M{}
|
||||||
)
|
)
|
||||||
query := internal.CommonQuery{
|
query := model.CommonQuery{
|
||||||
Page: queryParams.Page,
|
Page: queryParams.Page,
|
||||||
Limit: queryParams.Limit,
|
Limit: queryParams.Limit,
|
||||||
Keyword: queryParams.Keyword,
|
Keyword: queryParams.Keyword,
|
||||||
|
@ -96,18 +116,18 @@ func (h Handle) All(queryParams internal.AllQuery) (r internal.UserAll) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handle) getResponseList(ctx context.Context, users []internal.DBUser) []internal.User {
|
func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []model.User {
|
||||||
res := make([]internal.User, 0)
|
res := make([]model.User, 0)
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
roleRaw, _ := h.roleFindByID(ctx, user.RoleID)
|
roleRaw, _ := h.roleFindByID(ctx, user.RoleID)
|
||||||
res = append(res, internal.User{
|
res = append(res, model.User{
|
||||||
ID: user.ID.Hex(),
|
ID: user.ID.Hex(),
|
||||||
Name: user.Name,
|
Name: user.Name,
|
||||||
Phone: user.Phone,
|
Phone: user.Phone,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Status: user.Status,
|
Status: user.Status,
|
||||||
Role: internal.RoleShort{
|
Role: model.RoleShort{
|
||||||
ID: roleRaw.ID.Hex(),
|
ID: roleRaw.ID.Hex(),
|
||||||
Name: roleRaw.Name,
|
Name: roleRaw.Name,
|
||||||
IsAdmin: roleRaw.IsAdmin,
|
IsAdmin: roleRaw.IsAdmin,
|
||||||
|
@ -122,7 +142,7 @@ func (h Handle) getResponseList(ctx context.Context, users []internal.DBUser) []
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateByUserID ...
|
// UpdateByUserID ...
|
||||||
func (h Handle) UpdateByUserID(userID string, payload internal.UpdateOptions) error {
|
func (h Handle) UpdateByUserID(userID string, payload model.UpdateOptions) error {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
|
@ -174,7 +194,7 @@ func (h Handle) UpdateByUserID(userID string, payload internal.UpdateOptions) er
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangeUserPassword ...
|
// ChangeUserPassword ...
|
||||||
func (h Handle) ChangeUserPassword(userID string, opt internal.ChangePasswordOptions) error {
|
func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
)
|
)
|
||||||
|
@ -189,7 +209,7 @@ func (h Handle) ChangeUserPassword(userID string, opt internal.ChangePasswordOpt
|
||||||
if _, isValid := mongodb.NewIDFromString(userID); !isValid {
|
if _, isValid := mongodb.NewIDFromString(userID); !isValid {
|
||||||
logger.Error("usermngmt - ChangePassword: invalid userID data", logger.LogData{
|
logger.Error("usermngmt - ChangePassword: invalid userID data", logger.LogData{
|
||||||
"payload": opt,
|
"payload": opt,
|
||||||
"userID": userID,
|
"userID": userID,
|
||||||
})
|
})
|
||||||
return errors.New("invalid user id data")
|
return errors.New("invalid user id data")
|
||||||
}
|
}
|
||||||
|
|
28
usermngmt.go
28
usermngmt.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
|
"github.com/Selly-Modules/usermngmt/role"
|
||||||
|
"github.com/Selly-Modules/usermngmt/user"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,10 +23,17 @@ 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
|
||||||
DB *mongo.Database
|
db *mongo.Database
|
||||||
|
handler Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
var s *Service
|
var s *Service
|
||||||
|
@ -55,8 +64,19 @@ func Init(config Config) (*Service, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
s = &Service{
|
s = &Service{
|
||||||
Config: config,
|
config: config,
|
||||||
DB: db,
|
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
|
return s, nil
|
||||||
|
|
Loading…
Reference in New Issue