2021-11-10 09:06:33 +00:00
|
|
|
package permission
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2022-10-10 03:46:33 +00:00
|
|
|
"git.selly.red/Selly-Modules/mongodb"
|
2023-04-05 10:54:05 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
2022-10-10 03:46:33 +00:00
|
|
|
"git.selly.red/Selly-Modules/usermngmt/internal"
|
|
|
|
"git.selly.red/Selly-Modules/usermngmt/model"
|
2021-11-10 09:06:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-04-05 10:54:05 +00:00
|
|
|
func FindByIDAndDelete(permissionID string) (*model.Permission, error) {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Find permissionID exists or not
|
|
|
|
id, isValid := mongodb.NewIDFromString(permissionID)
|
|
|
|
if !isValid {
|
|
|
|
return nil, errors.New(internal.ErrorInvalidPermission)
|
|
|
|
}
|
|
|
|
cond := bson.M{"_id": id}
|
|
|
|
doc, err := findOneAndDelete(ctx, cond)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := getResponse(doc)
|
|
|
|
return &res, 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 {
|
2023-04-05 10:54:05 +00:00
|
|
|
res = append(res, getResponse(permission))
|
2021-11-10 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2023-04-05 10:54:05 +00:00
|
|
|
|
|
|
|
func getResponse(permission model.DBPermission) model.Permission {
|
|
|
|
return 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,
|
|
|
|
}
|
|
|
|
}
|