update AllCreateMethod

This commit is contained in:
Hoang 2021-11-16 11:11:04 +07:00
parent 3a20ec8cd7
commit ba3b0104ff
8 changed files with 164 additions and 56 deletions

View File

@ -15,7 +15,7 @@ import (
// user methods
// CreateUser ...
func (s Service) CreateUser(payload model.UserCreateOptions) error {
func (s Service) CreateUser(payload model.UserCreateOptions) (id string, err error) {
return user.Create(payload)
}
@ -61,12 +61,14 @@ func (s Service) HasPermission(userID, permission string) bool {
// role methods
// CreateRole ...
func (s Service) CreateRole(payload model.RoleCreateOptions) error {
if err := role.Create(payload); err != nil {
return err
func (s Service) CreateRole(payload model.RoleCreateOptions) (id string, err error) {
id, err = role.Create(payload)
if err != nil {
return
}
cache.Roles()
return nil
return
}
// UpdateRole ...
@ -86,12 +88,14 @@ func (s Service) GetAllRoles(query model.RoleAllQuery) model.RoleAll {
// permission methods
// CreatePermission ...
func (s Service) CreatePermission(payload model.PermissionCreateOptions) error {
if err := permission.Create(payload); err != nil {
return err
func (s Service) CreatePermission(payload model.PermissionCreateOptions) (id string, err error) {
id, err = permission.Create(payload)
if err != nil {
return
}
cache.Roles()
return nil
return
}
// UpdatePermission ...

View File

@ -95,3 +95,22 @@ func countByCondition(ctx context.Context, cond interface{}) int64 {
}
return total
}
func isPermissionIDExisted(ctx context.Context, permissionID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
// Find
cond := bson.M{
"_id": permissionID,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}

View File

@ -12,25 +12,26 @@ import (
)
// Create ...
func Create(payload model.PermissionCreateOptions) error {
func Create(payload model.PermissionCreateOptions) (result string, err error) {
var (
ctx = context.Background()
)
// Validate payload
if err := payload.Validate(); err != nil {
return err
if err = payload.Validate(); err != nil {
return
}
// New permission data from payload
doc := newPermission(payload)
// Create permission
if err := create(ctx, doc); err != nil {
return err
if err = create(ctx, doc); err != nil {
return
}
return nil
result = doc.ID.Hex()
return
}
// newPermission ...
@ -59,11 +60,14 @@ func Update(permissionID string, payload model.PermissionUpdateOptions) error {
return err
}
// Validate permissionID
// Find permissionID exists or not
id, isValid := mongodb.NewIDFromString(permissionID)
if !isValid {
return errors.New("invalid permission id data")
}
if !isPermissionIDExisted(ctx, id) {
return errors.New("permission not found")
}
// Setup condition
cond := bson.M{

View File

@ -7,6 +7,8 @@ import (
"github.com/Selly-Modules/logger"
"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"
)
@ -84,3 +86,22 @@ func countByCondition(ctx context.Context, cond interface{}) int64 {
}
return total
}
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
// Find
cond := bson.M{
"_id": roleID,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Role - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}

View File

@ -12,25 +12,26 @@ import (
)
// Create ...
func Create(payload model.RoleCreateOptions) error {
func Create(payload model.RoleCreateOptions) (result string, err error) {
var (
ctx = context.Background()
)
// Validate payload
if err := payload.Validate(); err != nil {
return err
if err = payload.Validate(); err != nil {
return
}
// New role data from payload
doc := newRole(payload)
// Create role
if err := create(ctx, doc); err != nil {
return err
if err = create(ctx, doc); err != nil {
return
}
return nil
result = doc.ID.Hex()
return
}
// newRole ...
@ -63,6 +64,11 @@ func Update(roleID string, payload model.RoleUpdateOptions) error {
return errors.New("invalid role id data")
}
// Find roleID exists or not
if !isRoleIDExisted(ctx, id) {
return errors.New("role not found")
}
// Setup condition
cond := bson.M{
"_id": id,
@ -106,7 +112,11 @@ func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
go func() {
defer wg.Done()
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
r.List = getResponseList(docs)
res := make([]model.Role, 0)
for _, doc := range docs {
res = append(res, getResponse(doc))
}
r.List = res
}()
wg.Add(1)
@ -120,18 +130,13 @@ func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
return
}
func getResponseList(roles []model.DBRole) []model.Role {
res := make([]model.Role, 0)
for _, role := range roles {
res = append(res, model.Role{
ID: role.ID.Hex(),
Name: role.Name,
Code: role.Code,
Level: role.Level,
CreatedAt: role.CreatedAt,
UpdatedAt: role.UpdatedAt,
})
func getResponse(role model.DBRole) model.Role {
return model.Role{
ID: role.ID.Hex(),
Name: role.Name,
Code: role.Code,
Level: role.Level,
CreatedAt: role.CreatedAt,
UpdatedAt: role.UpdatedAt,
}
return res
}

View File

@ -38,6 +38,44 @@ func isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool
return total != 0
}
func isPhoneNumberExisted(ctx context.Context, phone string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
"phone": phone,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return total != 0
}
func isEmailExisted(ctx context.Context, email string) bool {
var (
col = database.GetUserCol()
)
// Find
cond := bson.M{
"email": email,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - User - CountDocuments", logger.LogData{
"condition": cond,
"err": err.Error(),
})
return true
}
return total != 0
}
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()

View File

@ -16,46 +16,47 @@ import (
)
// Create ...
func Create(payload model.UserCreateOptions) error {
func Create(payload model.UserCreateOptions) (result string, err error) {
var (
ctx = context.Background()
)
// Validate payload
if err := payload.Validate(); err != nil {
return err
if err = payload.Validate(); err != nil {
return
}
// Find roleID exists or not
roleID, isValid := mongodb.NewIDFromString(payload.RoleID)
if !isValid {
return errors.New("invalid role id data")
err = errors.New("invalid role id data")
return
}
if !isRoleIDExisted(ctx, roleID) {
return errors.New("role id does not exist")
err = errors.New("role id does not exist")
return
}
// Find phone number,email exists or not
if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) {
return errors.New("phone number or email already existed")
err = errors.New("phone number or email already existed")
return
}
// New user data from payload
doc, err := newUser(payload)
if err != nil {
return err
}
doc := newUser(payload)
// Create user
if err = create(ctx, doc); err != nil {
return err
return
}
return nil
result = doc.ID.Hex()
return
}
// newUser ...
func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
func newUser(payload model.UserCreateOptions) model.DBUser {
timeNow := internal.Now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return model.DBUser{
@ -70,7 +71,7 @@ func newUser(payload model.UserCreateOptions) (result model.DBUser, err error) {
Other: payload.Other,
CreatedAt: timeNow,
UpdatedAt: timeNow,
}, nil
}
}
// All ...
@ -156,13 +157,29 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
return errors.New("role id does not exist")
}
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid role id data")
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
return errors.New("user not found")
}
// Find phone number,email exists or not
if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) {
return errors.New("phone number or email already existed")
if user.Phone != payload.Phone {
if isPhoneNumberExisted(ctx, payload.Phone) {
return errors.New("phone number already existed")
}
}
if user.Email != payload.Email {
if isEmailExisted(ctx, payload.Email) {
return errors.New("email already existed")
}
}
// Setup condition
id, _ := mongodb.NewIDFromString(userID)
cond := bson.M{
"_id": id,
}

View File

@ -55,12 +55,12 @@ func Init(config Config) (*Service, error) {
return nil, err
}
// Init cache
cache.Init()
// Set database
database.Set(db, config.TablePrefix)
// Init cache
cache.Init()
s = &Service{
config: config,
}