Feature/role create #5

Merged
lqhoang99 merged 8 commits from feature/RoleCreate into master 2021-11-10 06:43:29 +00:00
15 changed files with 124 additions and 89 deletions
Showing only changes of commit e95ab2ec0f - Show all commits

View File

@ -1,16 +1,16 @@
package usermngmt
import (
"github.com/Selly-Modules/usermngmt/internal/model"
"github.com/Selly-Modules/usermngmt/model"
)
// Create ...
func (s Service) Create(payload model.CreateOptions) error {
func (s Service) Create(payload model.UserCreateOptions) error {
return s.handler.User.Create(payload)
}
// 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)
}
@ -23,7 +23,7 @@ func (s Service) ChangeUserStatus(userID, newStatus string) error {
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)
}

View File

@ -2,7 +2,5 @@ package usermngmt
// Constant ...
const (
tableUser = "users"
tableRole = "roles"
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

@ -4,9 +4,4 @@ type RoleShort struct {
ID string `json:"_id"`
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
}
// RoleCreateOptions ...
type RoleCreateOptions struct {
Name string
}
}

View File

@ -2,13 +2,12 @@ package model
import (
"errors"
"time"
"github.com/Selly-Modules/logger"
)
// CreateOptions ...
type CreateOptions struct {
// UserCreateOptions ...
type UserCreateOptions struct {
Name string
Phone string
Email string
@ -18,8 +17,8 @@ type CreateOptions struct {
Other string
}
// UpdateOptions ...
type UpdateOptions struct {
// UserUpdateOptions ...
type UserUpdateOptions struct {
Name string
Phone string
Email string
@ -33,8 +32,8 @@ type ChangePasswordOptions struct {
NewPassword string
}
// AllQuery ...
type AllQuery struct {
// UserAllQuery ...
type UserAllQuery struct {
Page int64
Limit int64
Keyword string
@ -42,29 +41,8 @@ type AllQuery struct {
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 ...
func (co CreateOptions) Validate() error {
func (co UserCreateOptions) Validate() error {
// Name
if co.Name == "" {
logger.Error("usermngmt - Create: no Name data", logger.LogData{
@ -117,7 +95,7 @@ func (co CreateOptions) Validate() error {
}
// Validate ...
func (uo UpdateOptions) Validate() error {
func (uo UserUpdateOptions) Validate() error {
// Name
if uo.Name == "" {
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 (
"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/primitive"
)
@ -11,7 +12,8 @@ import (
func (h Handle) findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
var (
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
}

View File

@ -3,13 +3,11 @@ package role
import (
"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/mongo"
)
type Handle struct {
Col *mongo.Collection
}
// FindByID ...

View File

@ -5,13 +5,17 @@ import (
"fmt"
"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/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (h Handle) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := 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 {
logger.Error("usermngmt - countUserByCondition", logger.LogData{
"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 {
var (
col = database.GetRoleCol()
)
// Find
cond := bson.M{
"_id": roleID,
}
total, err := h.RoleCol.CountDocuments(ctx, cond)
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - countRoleByCondition", logger.LogData{
"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) {
var (
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
}
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 {
logger.Error("usermngmt - Create", logger.LogData{
"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 {
_, err := h.Col.UpdateOne(ctx, cond, payload)
var (
col = database.GetUserCol()
)
_, err := col.UpdateOne(ctx, cond, payload)
if err != nil {
logger.Error("usermngmt - Update", logger.LogData{
"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) {
var (
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
}
func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBUser) {
var (
col = database.GetUserCol()
)
docs = make([]model.DBUser, 0)
cursor, err := h.Col.Find(ctx, cond, opts...)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - All", logger.LogData{
"cond": cond,
@ -119,7 +137,10 @@ func (h Handle) findByCondition(ctx context.Context, cond interface{}, opts ...*
// countByCondition ...
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 {
logger.Error("usermngmt - Count", logger.LogData{
"err": err.Error(),

View File

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

View File

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