usermngmt/permission/handle.go

173 lines
3.4 KiB
Go
Raw Normal View History

2021-11-10 09:06:33 +00:00
package permission
import (
"context"
"errors"
"sync"
"github.com/Selly-Modules/mongodb"
"github.com/Selly-Modules/usermngmt/internal"
"github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson"
)
// Create ...
2021-11-16 04:11:04 +00:00
func Create(payload model.PermissionCreateOptions) (result string, err error) {
2021-11-10 09:06:33 +00:00
var (
ctx = context.Background()
)
// Validate payload
2021-11-16 04:11:04 +00:00
if err = payload.Validate(); err != nil {
return
2021-11-10 09:06:33 +00:00
}
// New permission data from payload
doc := newPermission(payload)
// Create permission
2021-11-16 04:11:04 +00:00
if err = create(ctx, doc); err != nil {
return
2021-11-10 09:06:33 +00:00
}
2021-11-16 04:11:04 +00:00
result = doc.ID.Hex()
return
2021-11-10 09:06:33 +00:00
}
// newPermission ...
func newPermission(payload model.PermissionCreateOptions) model.DBPermission {
timeNow := internal.Now()
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
return model.DBPermission{
ID: mongodb.NewObjectID(),
Name: payload.Name,
2021-11-12 05:16:38 +00:00
Code: payload.Code,
2021-11-10 09:06:33 +00:00
RoleID: roleID,
Desc: payload.Desc,
CreatedAt: timeNow,
UpdatedAt: timeNow,
}
}
// Update ...
func Update(permissionID string, payload model.PermissionUpdateOptions) error {
var (
ctx = context.Background()
)
// Validate payload
if err := payload.Validate(); err != nil {
return err
}
2021-11-16 04:11:04 +00:00
// Find permissionID exists or not
2021-11-10 09:06:33 +00:00
id, isValid := mongodb.NewIDFromString(permissionID)
if !isValid {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPermission)
2021-11-10 09:06:33 +00:00
}
2021-11-16 04:11:04 +00:00
if !isPermissionIDExisted(ctx, id) {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorNotFoundPermission)
2021-11-16 04:11:04 +00:00
}
2021-11-10 09:06:33 +00:00
// Setup condition
cond := bson.M{
"_id": id,
}
// Setup update data
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
updateData := bson.M{
"$set": bson.M{
"name": payload.Name,
2021-11-12 05:16:38 +00:00
"code": payload.Code,
2021-11-10 09:06:33 +00:00
"roleId": roleID,
"desc": payload.Desc,
"updatedAt": internal.Now(),
},
}
// Update
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
return err
}
return nil
}
2021-11-16 08:34:56 +00:00
// Delete ...
func Delete(permissionID string) error {
var (
ctx = context.Background()
)
// Find permissionID exists or not
id, isValid := mongodb.NewIDFromString(permissionID)
if !isValid {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPermission)
2021-11-16 08:34:56 +00:00
}
if !isPermissionIDExisted(ctx, id) {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorNotFoundPermission)
2021-11-16 08:34:56 +00:00
}
// Delete
if err := deleteOneByCondition(ctx, bson.M{"_id": id}); err != nil {
return err
}
return nil
}
2021-11-10 09:06:33 +00:00
// All ...
func All(queryParams model.PermissionAllQuery) (r model.PermissionAll) {
var (
ctx = context.Background()
wg sync.WaitGroup
cond = bson.M{}
)
query := model.CommonQuery{
2021-11-17 03:13:08 +00:00
Page: queryParams.Page,
Limit: queryParams.Limit,
Sort: queryParams.Sort,
RoleID: queryParams.RoleID,
2021-11-10 09:06:33 +00:00
}
// Assign condition
query.SetDefaultLimit()
2021-11-17 03:13:08 +00:00
query.AssignRoleID(cond)
2021-11-10 09:06:33 +00:00
wg.Add(1)
go func() {
defer wg.Done()
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
r.List = getResponseList(docs)
}()
wg.Add(1)
go func() {
defer wg.Done()
r.Total = countByCondition(ctx, cond)
}()
wg.Wait()
2021-11-23 04:48:40 +00:00
r.Limit = query.Limit
2021-11-10 09:06:33 +00:00
return
}
func getResponseList(permissions []model.DBPermission) []model.Permission {
res := make([]model.Permission, 0)
for _, permission := range permissions {
res = append(res, model.Permission{
ID: permission.ID.Hex(),
Name: permission.Name,
Code: permission.Code,
RoleID: permission.RoleID.Hex(),
Desc: permission.Desc,
CreatedAt: permission.CreatedAt,
UpdatedAt: permission.UpdatedAt,
})
}
return res
}