fix comment again

This commit is contained in:
Hoang 2021-11-10 11:01:39 +07:00
parent d6bc1a1c02
commit e95ab2ec0f
15 changed files with 124 additions and 89 deletions

View File

@ -1,16 +1,16 @@
package usermngmt package usermngmt
import ( import (
"github.com/Selly-Modules/usermngmt/internal/model" "github.com/Selly-Modules/usermngmt/model"
) )
// Create ... // Create ...
func (s Service) Create(payload model.CreateOptions) error { func (s Service) Create(payload model.UserCreateOptions) error {
return s.handler.User.Create(payload) return s.handler.User.Create(payload)
} }
// Update ... // Update ...
func (s Service) Update(userID string, payload model.UpdateOptions) error { func (s Service) Update(userID string, payload model.UserUpdateOptions) error {
return s.handler.User.UpdateByUserID(userID, payload) return s.handler.User.UpdateByUserID(userID, payload)
} }
@ -23,7 +23,7 @@ func (s Service) ChangeUserStatus(userID, newStatus string) error {
return s.handler.User.ChangeUserStatus(userID, newStatus) return s.handler.User.ChangeUserStatus(userID, newStatus)
} }
func (s Service) All(query model.AllQuery) model.UserAll { func (s Service) All(query model.UserAllQuery) model.UserAll {
return s.handler.User.All(query) return s.handler.User.All(query)
} }

View File

@ -2,7 +2,5 @@ package usermngmt
// Constant ... // Constant ...
const ( const (
tableUser = "users"
tableRole = "roles"
tablePrefixDefault = "usermngmt" tablePrefixDefault = "usermngmt"
) )

31
database/db.go Normal file
View File

@ -0,0 +1,31 @@
package database
import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
)
// Table
var (
tableUser = "users"
tableRole = "roles"
)
var (
db *mongo.Database
prefix string
)
func Set(instance *mongo.Database, tablePrefix string) {
db = instance
prefix = tablePrefix
}
func GetUserCol() *mongo.Collection {
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableUser))
}
func GetRoleCol() *mongo.Collection {
return db.Collection(fmt.Sprintf("%s-%s", prefix, tableRole))
}

12
db.go
View File

@ -1,12 +0,0 @@
package usermngmt
import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
)
// getCollectionName ...
func (s Service) getCollectionName(tablePrefix, table string) *mongo.Collection {
return s.db.Collection(fmt.Sprintf("%s-%s", tablePrefix, table))
}

7
model/role_request.go Normal file
View File

@ -0,0 +1,7 @@
package model
// RoleCreateOptions ...
type RoleCreateOptions struct {
Name string
}

View File

@ -5,8 +5,3 @@ type RoleShort struct {
Name string `json:"name"` Name string `json:"name"`
IsAdmin bool `json:"isAdmin"` IsAdmin bool `json:"isAdmin"`
} }
// RoleCreateOptions ...
type RoleCreateOptions struct {
Name string
}

View File

@ -2,13 +2,12 @@ package model
import ( import (
"errors" "errors"
"time"
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
) )
// CreateOptions ... // UserCreateOptions ...
type CreateOptions struct { type UserCreateOptions struct {
Name string Name string
Phone string Phone string
Email string Email string
@ -18,8 +17,8 @@ type CreateOptions struct {
Other string Other string
} }
// UpdateOptions ... // UserUpdateOptions ...
type UpdateOptions struct { type UserUpdateOptions struct {
Name string Name string
Phone string Phone string
Email string Email string
@ -33,8 +32,8 @@ type ChangePasswordOptions struct {
NewPassword string NewPassword string
} }
// AllQuery ... // UserAllQuery ...
type AllQuery struct { type UserAllQuery struct {
Page int64 Page int64
Limit int64 Limit int64
Keyword string Keyword string
@ -42,29 +41,8 @@ type AllQuery struct {
Status string Status string
} }
// User ...
type User struct {
ID string `json:"_id"`
Name string `json:"name"`
Phone string `json:"phone"`
Email string `json:"email"`
Status string `json:"status"`
Role RoleShort `json:"role"`
Other string `json:"other"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type (
// UserAll ...
UserAll struct {
List []User `json:"list"`
Total int64 `json:"total"`
}
)
// Validate ... // Validate ...
func (co CreateOptions) Validate() error { func (co UserCreateOptions) Validate() error {
// Name // Name
if co.Name == "" { if co.Name == "" {
logger.Error("usermngmt - Create: no Name data", logger.LogData{ logger.Error("usermngmt - Create: no Name data", logger.LogData{
@ -117,7 +95,7 @@ func (co CreateOptions) Validate() error {
} }
// Validate ... // Validate ...
func (uo UpdateOptions) Validate() error { func (uo UserUpdateOptions) Validate() error {
// Name // Name
if uo.Name == "" { if uo.Name == "" {
logger.Error("usermngmt - Update: no name data", logger.LogData{ logger.Error("usermngmt - Update: no name data", logger.LogData{

26
model/user_response.go Normal file
View File

@ -0,0 +1,26 @@
package model
import (
"time"
)
// User ...
type User struct {
ID string `json:"_id"`
Name string `json:"name"`
Phone string `json:"phone"`
Email string `json:"email"`
Status string `json:"status"`
Role RoleShort `json:"role"`
Other string `json:"other"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type (
// UserAll ...
UserAll struct {
List []User `json:"list"`
Total int64 `json:"total"`
}
)

View File

@ -3,7 +3,8 @@ package role
import ( import (
"context" "context"
"github.com/Selly-Modules/usermngmt/internal/model" "github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/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"
) )
@ -11,7 +12,8 @@ import (
func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
var ( var (
doc model.DBRole doc model.DBRole
col = database.GetRoleCol()
) )
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err return doc, err
} }

View File

@ -3,13 +3,11 @@ package role
import ( import (
"context" "context"
"github.com/Selly-Modules/usermngmt/internal/model" "github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
) )
type Handle struct { type Handle struct {
Col *mongo.Collection
} }
// FindByID ... // FindByID ...

View File

@ -5,13 +5,17 @@ import (
"fmt" "fmt"
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/internal/model" "github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/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"
) )
func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
var (
col = database.GetUserCol()
)
// Find // Find
cond := bson.M{ cond := bson.M{
"$or": []bson.M{ "$or": []bson.M{
@ -23,7 +27,7 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st
}, },
}, },
} }
total, err := h.Col.CountDocuments(ctx, cond) total, err := col.CountDocuments(ctx, cond)
if err != nil { if err != nil {
logger.Error("usermngmt - countUserByCondition", logger.LogData{ logger.Error("usermngmt - countUserByCondition", logger.LogData{
"condition": cond, "condition": cond,
@ -35,11 +39,14 @@ func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email st
} }
func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
// Find // Find
cond := bson.M{ cond := bson.M{
"_id": roleID, "_id": roleID,
} }
total, err := h.RoleCol.CountDocuments(ctx, cond) total, err := col.CountDocuments(ctx, cond)
if err != nil { if err != nil {
logger.Error("usermngmt - countRoleByCondition", logger.LogData{ logger.Error("usermngmt - countRoleByCondition", logger.LogData{
"condition": cond, "condition": cond,
@ -53,13 +60,17 @@ func (h Handle) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID)
func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) { func (h Handle) roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
var ( var (
doc model.DBRole doc model.DBRole
col = database.GetRoleCol()
) )
err := h.RoleCol.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err return doc, err
} }
func (h Handle) create(ctx context.Context, doc model.DBUser) error { func (h Handle) create(ctx context.Context, doc model.DBUser) error {
_, err := h.Col.InsertOne(ctx, doc) var (
col = database.GetUserCol()
)
_, err := col.InsertOne(ctx, doc)
if err != nil { if err != nil {
logger.Error("usermngmt - Create", logger.LogData{ logger.Error("usermngmt - Create", logger.LogData{
"doc": doc, "doc": doc,
@ -72,7 +83,10 @@ func (h Handle) create(ctx context.Context, doc model.DBUser) error {
} }
func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error {
_, err := h.Col.UpdateOne(ctx, cond, payload) var (
col = database.GetUserCol()
)
_, err := col.UpdateOne(ctx, cond, payload)
if err != nil { if err != nil {
logger.Error("usermngmt - Update", logger.LogData{ logger.Error("usermngmt - Update", logger.LogData{
"cond": cond, "cond": cond,
@ -88,15 +102,19 @@ func (h Handle) updateOneByCondition(ctx context.Context, cond interface{}, payl
func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) { func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error) {
var ( var (
doc model.DBUser doc model.DBUser
col = database.GetUserCol()
) )
err := h.Col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) err := 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 []model.DBUser) { func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) {
var (
col = database.GetUserCol()
)
docs = make([]model.DBUser, 0) docs = make([]model.DBUser, 0)
cursor, err := h.Col.Find(ctx, cond, opts...) cursor, err := col.Find(ctx, cond, opts...)
if err != nil { if err != nil {
logger.Error("usermngmt - All", logger.LogData{ logger.Error("usermngmt - All", logger.LogData{
"cond": cond, "cond": cond,
@ -119,7 +137,10 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*
// countByCondition ... // countByCondition ...
func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 { func (h Handle) countByCondition(ctx context.Context, cond interface{}) int64 {
total, err := h.Col.CountDocuments(ctx, cond) var (
col = database.GetUserCol()
)
total, err := col.CountDocuments(ctx, cond)
if err != nil { if err != nil {
logger.Error("usermngmt - Count", logger.LogData{ logger.Error("usermngmt - Count", logger.LogData{
"err": err.Error(), "err": err.Error(),

View File

@ -8,18 +8,15 @@ 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" "github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
) )
type Handle struct { type Handle struct {
Col *mongo.Collection
RoleCol *mongo.Collection
} }
// Create ... // Create ...
func (h Handle) Create(payload model.CreateOptions) error { func (h Handle) Create(payload model.UserCreateOptions) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )
@ -58,7 +55,7 @@ func (h Handle) Create(payload model.CreateOptions) error {
} }
// newUser ... // newUser ...
func newUser(payload model.CreateOptions) (result model.DBUser, err error) { func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
timeNow := internal.Now() timeNow := internal.Now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID) roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return model.DBUser{ return model.DBUser{
@ -77,7 +74,7 @@ func newUser(payload model.CreateOptions) (result model.DBUser, err error) {
} }
// All ... // All ...
func (h Handle) All(queryParams model.AllQuery) (r model.UserAll) { func (h Handle) All(queryParams model.UserAllQuery) (r model.UserAll) {
var ( var (
ctx = context.Background() ctx = context.Background()
wg sync.WaitGroup wg sync.WaitGroup
@ -142,7 +139,7 @@ func (h Handle) getResponseList(ctx context.Context, users []model.DBUser) []mod
} }
// UpdateByUserID ... // UpdateByUserID ...
func (h Handle) UpdateByUserID(userID string, payload model.UpdateOptions) error { func (h Handle) UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
var ( var (
ctx = context.Background() ctx = context.Background()
) )

View File

@ -5,9 +5,9 @@ import (
"fmt" "fmt"
"github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/mongodb"
"github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/role" "github.com/Selly-Modules/usermngmt/role"
"github.com/Selly-Modules/usermngmt/user" "github.com/Selly-Modules/usermngmt/user"
"go.mongodb.org/mongo-driver/mongo"
) )
// MongoDBConfig ... // MongoDBConfig ...
@ -32,7 +32,6 @@ type Handler struct {
// Service ... // Service ...
type Service struct { type Service struct {
config Config config Config
db *mongo.Database
handler Handler handler Handler
} }
@ -63,19 +62,14 @@ func Init(config Config) (*Service, error) {
return nil, err return nil, err
} }
// Set database
database.Set(db, config.TablePrefix)
s = &Service{ s = &Service{
config: config, config: config,
db: db, handler: Handler{
} User: user.Handle{},
Role: role.Handle{},
// 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),
}, },
} }