Feature/role create #5
17
action.go
17
action.go
|
@ -2,31 +2,36 @@ package usermngmt
|
|||
|
||||
import (
|
||||
"github.com/Selly-Modules/usermngmt/model"
|
||||
"github.com/Selly-Modules/usermngmt/role"
|
||||
"github.com/Selly-Modules/usermngmt/user"
|
||||
)
|
||||
|
||||
// Create ...
|
||||
func (s Service) Create(payload model.UserCreateOptions) error {
|
||||
return s.handler.User.Create(payload)
|
||||
return user.Create(payload)
|
||||
}
|
||||
|
||||
// Update ...
|
||||
func (s Service) Update(userID string, payload model.UserUpdateOptions) error {
|
||||
return s.handler.User.UpdateByUserID(userID, payload)
|
||||
return user.UpdateByUserID(userID, payload)
|
||||
}
|
||||
|
||||
// ChangeUserPassword ...
|
||||
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 {
|
||||
return s.handler.User.ChangeUserStatus(userID, newStatus)
|
||||
return user.ChangeUserStatus(userID, newStatus)
|
||||
}
|
||||
|
||||
// All ...
|
||||
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 {
|
||||
return s.handler.Role.Create(payload)
|
||||
return role.Create(payload)
|
||||
}
|
||||
|
|
|
@ -22,10 +22,12 @@ func Set(instance *mongo.Database, tablePrefix string) {
|
|||
prefix = tablePrefix
|
||||
}
|
||||
|
||||
// GetUserCol ...
|
||||
func GetUserCol() *mongo.Collection {
|
||||
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser))
|
||||
}
|
||||
|
||||
// GetRoleCol ...
|
||||
func GetRoleCol() *mongo.Collection {
|
||||
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole))
|
||||
}
|
||||
|
|
|
@ -6,4 +6,3 @@ const (
|
|||
|
||||
passwordHashingCost = 14
|
||||
)
|
||||
|
||||
|
|
|
@ -33,4 +33,3 @@ func GetSearchString(fieldList ...string) string {
|
|||
}
|
||||
return fmt.Sprintf(format, searchList...)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,4 +18,3 @@ func getHCMLocation() *time.Location {
|
|||
func Now() time.Time {
|
||||
return time.Now().In(getHCMLocation())
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// CommonQuery ...
|
||||
type CommonQuery struct {
|
||||
Page int64
|
||||
Limit int64
|
||||
|
@ -56,4 +57,3 @@ func (q *CommonQuery) SetDefaultLimit() {
|
|||
q.Limit = 20
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package model
|
||||
|
||||
// RoleShort ...
|
||||
type RoleShort struct {
|
||||
ID string `json:"_id"`
|
||||
Name string `json:"name"`
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"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 (
|
||||
doc model.DBRole
|
||||
col = database.GetRoleCol()
|
||||
|
|
|
@ -7,17 +7,14 @@ import (
|
|||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Handle struct {
|
||||
}
|
||||
|
||||
// FindByID ...
|
||||
func (h Handle) FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
|
||||
role, err := h.findByID(ctx, id)
|
||||
func FindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
|
||||
role, err := findByID(ctx, id)
|
||||
return role, err
|
||||
}
|
||||
|
||||
// Create ...
|
||||
func (h Handle) Create(payload model.RoleCreateOptions) error {
|
||||
func Create(payload model.RoleCreateOptions) error {
|
||||
// TODO later
|
||||
return nil
|
||||
}
|
||||
|
|
16
user/db.go
16
user/db.go
|
@ -12,7 +12,7 @@ import (
|
|||
"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 (
|
||||
col = database.GetUserCol()
|
||||
)
|
||||
|
@ -38,7 +38,7 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st
|
|||
return total != 0
|
||||
}
|
||||
|
||||
func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
|
||||
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
|
||||
var (
|
||||
col = database.GetRoleCol()
|
||||
)
|
||||
|
@ -57,7 +57,7 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
|
|||
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 (
|
||||
doc model.DBRole
|
||||
col = database.GetRoleCol()
|
||||
|
@ -66,7 +66,7 @@ func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.
|
|||
return doc, err
|
||||
}
|
||||
|
||||
func (h Handle) create(ctx context.Context, doc model.DBUser) error {
|
||||
func create(ctx context.Context, doc model.DBUser) error {
|
||||
var (
|
||||
col = database.GetUserCol()
|
||||
)
|
||||
|
@ -82,7 +82,7 @@ func (h Handle) create(ctx context.Context, doc model.DBUser) error {
|
|||
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 (
|
||||
col = database.GetUserCol()
|
||||
)
|
||||
|
@ -99,7 +99,7 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl
|
|||
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 (
|
||||
doc model.DBUser
|
||||
col = database.GetUserCol()
|
||||
|
@ -108,7 +108,7 @@ func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUs
|
|||
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 (
|
||||
col = database.GetUserCol()
|
||||
)
|
||||
|
@ -136,7 +136,7 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*
|
|||
}
|
||||
|
||||
// countByCondition ...
|
||||
func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 {
|
||||
func countByCondition(ctx context.Context, cond interface{}) int64 {
|
||||
var (
|
||||
col = database.GetUserCol()
|
||||
)
|
||||
|
|
|
@ -12,11 +12,8 @@ import (
|
|||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
type Handle struct {
|
||||
}
|
||||
|
||||
// Create ...
|
||||
func (h Handle) Create(payload model.UserCreateOptions) error {
|
||||
func Create(payload model.UserCreateOptions) error {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
@ -31,12 +28,12 @@ func (h Handle) Create(payload model.UserCreateOptions) error {
|
|||
if !isValid {
|
||||
return errors.New("invalid role id data")
|
||||
}
|
||||
if !h.isRoleIDExisted(ctx, roleID) {
|
||||
if !isRoleIDExisted(ctx, roleID) {
|
||||
return errors.New("role id does not exist")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
|
@ -47,7 +44,7 @@ func (h Handle) Create(payload model.UserCreateOptions) error {
|
|||
}
|
||||
|
||||
// Create user
|
||||
if err = h.create(ctx, doc); err != nil {
|
||||
if err = create(ctx, doc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -74,7 +71,7 @@ func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
|
|||
}
|
||||
|
||||
// All ...
|
||||
func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
|
||||
func All(queryParams model.UserAllQuery) (r model.UserAll) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
wg sync.WaitGroup
|
||||
|
@ -98,14 +95,14 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
|
|||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
docs := h.findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
|
||||
r.List = h.getResponseList(ctx, docs)
|
||||
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
|
||||
r.List = getResponseList(ctx, docs)
|
||||
}()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
r.Total = h.countByCondition(ctx, cond)
|
||||
r.Total = countByCondition(ctx, cond)
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
@ -113,11 +110,11 @@ func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
|
|||
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)
|
||||
|
||||
for _, user := range users {
|
||||
roleRaw, _ := h.roleFindByID(ctx, user.RoleID)
|
||||
roleRaw, _ := roleFindByID(ctx, user.RoleID)
|
||||
res = append(res, model.User{
|
||||
ID: user.ID.Hex(),
|
||||
Name: user.Name,
|
||||
|
@ -139,7 +136,7 @@ func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []mod
|
|||
}
|
||||
|
||||
// UpdateByUserID ...
|
||||
func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
|
||||
func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
@ -154,12 +151,12 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
|
|||
if !isValid {
|
||||
return errors.New("invalid role id data")
|
||||
}
|
||||
if !h.isRoleIDExisted(ctx, roleID) {
|
||||
if !isRoleIDExisted(ctx, roleID) {
|
||||
return errors.New("role id does not exist")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
|
@ -183,7 +180,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
|
|||
}
|
||||
|
||||
// Update
|
||||
if err := h.updateOneByCondition(ctx, cond, updateData); err != nil {
|
||||
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -191,7 +188,7 @@ func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) e
|
|||
}
|
||||
|
||||
// ChangeUserPassword ...
|
||||
func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error {
|
||||
func ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
@ -213,7 +210,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
|
|||
|
||||
// Find user
|
||||
id, _ := mongodb.NewIDFromString(userID)
|
||||
user, _ := h.findByID(ctx, id)
|
||||
user, _ := findByID(ctx, id)
|
||||
if user.ID.IsZero() {
|
||||
return errors.New("user not found")
|
||||
}
|
||||
|
@ -224,7 +221,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
|
|||
}
|
||||
|
||||
// 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{
|
||||
"hashedPassword": internal.HashPassword(opt.NewPassword),
|
||||
"updatedAt": internal.Now(),
|
||||
|
@ -237,7 +234,7 @@ func (h Handle) ChangeUserPassword(userID string, opt model.ChangePasswordOption
|
|||
}
|
||||
|
||||
// ChangeUserStatus ...
|
||||
func (h Handle) ChangeUserStatus(userID, newStatus string) error {
|
||||
func ChangeUserStatus(userID, newStatus string) error {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
@ -249,7 +246,7 @@ func (h Handle) ChangeUserStatus(userID, newStatus string) error {
|
|||
}
|
||||
|
||||
// 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{
|
||||
"status": newStatus,
|
||||
"updatedAt": internal.Now(),
|
||||
|
|
15
usermngmt.go
15
usermngmt.go
|
@ -6,8 +6,6 @@ import (
|
|||
|
||||
"github.com/Selly-Modules/mongodb"
|
||||
"github.com/Selly-Modules/usermngmt/database"
|
||||
"github.com/Selly-Modules/usermngmt/role"
|
||||
"github.com/Selly-Modules/usermngmt/user"
|
||||
)
|
||||
|
||||
// MongoDBConfig ...
|
||||
|
@ -23,16 +21,9 @@ type Config struct {
|
|||
TablePrefix string
|
||||
}
|
||||
|
||||
// Handler ...
|
||||
type Handler struct {
|
||||
User user.Handle
|
||||
Role role.Handle
|
||||
}
|
||||
|
||||
// Service ...
|
||||
type Service struct {
|
||||
config Config
|
||||
handler Handler
|
||||
config Config
|
||||
}
|
||||
|
||||
var s *Service
|
||||
|
@ -67,10 +58,6 @@ func Init(config Config) (*Service, error) {
|
|||
|
||||
s = &Service{
|
||||
config: config,
|
||||
handler: Handler{
|
||||
User: user.Handle{},
|
||||
Role: role.Handle{},
|
||||
},
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
|
Loading…
Reference in New Issue