Feature/role create #5
26
action.go
26
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)
|
||||
}
|
||||
|
|
11
db.go
11
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))
|
||||
}
|
||||
|
|
|
@ -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 (
|
||||
"time"
|
||||
|
@ -30,4 +30,3 @@ type DBUser struct {
|
|||
CreatedAt time.Time `bson:"createdAt"`
|
||||
UpdatedAt time.Time `bson:"updatedAt"`
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package internal
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/Selly-Modules/mongodb"
|
|
@ -1,4 +1,4 @@
|
|||
package internal
|
||||
package model
|
||||
|
||||
type RoleShort struct {
|
||||
ID string `json:"_id"`
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
16
user/db.go
16
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 {
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
|
|
28
usermngmt.go
28
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
|
||||
|
|
Loading…
Reference in New Issue