usermngmt/role/handle.go

169 lines
3.1 KiB
Go
Raw Permalink Normal View History

2021-11-10 01:44:22 +00:00
package role
import (
"context"
2021-11-10 07:50:43 +00:00
"errors"
"sync"
2021-11-10 01:44:22 +00:00
2022-10-10 03:46:33 +00:00
"git.selly.red/Selly-Modules/mongodb"
"git.selly.red/Selly-Modules/usermngmt/internal"
"git.selly.red/Selly-Modules/usermngmt/model"
2021-11-10 07:50:43 +00:00
"go.mongodb.org/mongo-driver/bson"
2021-11-10 01:44:22 +00:00
)
// Create ...
2021-11-16 04:11:04 +00:00
func Create(payload model.RoleCreateOptions) (result string, err error) {
2021-11-10 07:50:43 +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 07:50:43 +00:00
}
// New role data from payload
doc := newRole(payload)
// Create role
2021-11-16 04:11:04 +00:00
if err = create(ctx, doc); err != nil {
return
2021-11-10 07:50:43 +00:00
}
2021-11-16 04:11:04 +00:00
result = doc.ID.Hex()
return
2021-11-10 01:44:22 +00:00
}
2021-11-10 07:50:43 +00:00
// newRole ...
func newRole(payload model.RoleCreateOptions) model.DBRole {
timeNow := internal.Now()
return model.DBRole{
ID: mongodb.NewObjectID(),
Name: payload.Name,
2021-11-10 08:00:36 +00:00
Code: internal.GenerateCode(payload.Name),
2021-11-12 05:16:38 +00:00
Level: payload.Level,
2021-11-19 02:59:33 +00:00
IsAdmin: payload.IsAdmin,
2021-11-10 07:50:43 +00:00
CreatedAt: timeNow,
UpdatedAt: timeNow,
}
}
// Update ...
func Update(roleID string, payload model.RoleUpdateOptions) error {
var (
ctx = context.Background()
)
// Validate payload
if err := payload.Validate(); err != nil {
return err
}
// Validate roleID
id, isValid := mongodb.NewIDFromString(roleID)
if !isValid {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidRole)
2021-11-10 07:50:43 +00:00
}
2021-11-16 04:11:04 +00:00
// Find roleID exists or not
if !isRoleIDExisted(ctx, id) {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorNotFoundRole)
2021-11-16 04:11:04 +00:00
}
2021-11-10 07:50:43 +00:00
// Setup condition
cond := bson.M{
"_id": id,
}
// Setup update data
updateData := bson.M{
"$set": bson.M{
"name": payload.Name,
2021-11-10 08:00:36 +00:00
"code": internal.GenerateCode(payload.Name),
2021-11-12 05:16:38 +00:00
"level": payload.Level,
2021-11-19 02:59:33 +00:00
"isAdmin": payload.IsAdmin,
2021-11-10 07:50:43 +00:00
"updatedAt": internal.Now(),
},
}
// Update
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
return err
}
return nil
}
// All ...
func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
var (
ctx = context.Background()
wg sync.WaitGroup
cond = bson.M{}
)
query := model.CommonQuery{
Page: queryParams.Page,
Limit: queryParams.Limit,
2021-11-16 07:26:50 +00:00
Sort: queryParams.Sort,
2021-11-10 07:50:43 +00:00
}
// Assign condition
query.SetDefaultLimit()
wg.Add(1)
go func() {
defer wg.Done()
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
2021-11-16 04:11:04 +00:00
res := make([]model.Role, 0)
for _, doc := range docs {
res = append(res, getResponse(doc))
}
r.List = res
2021-11-10 07:50:43 +00:00
}()
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 07:50:43 +00:00
return
}
2021-11-16 04:11:04 +00:00
func getResponse(role model.DBRole) model.Role {
return model.Role{
ID: role.ID.Hex(),
Name: role.Name,
Code: role.Code,
Level: role.Level,
2021-11-24 10:26:00 +00:00
IsAdmin: role.IsAdmin,
2021-11-16 04:11:04 +00:00
CreatedAt: role.CreatedAt,
UpdatedAt: role.UpdatedAt,
2021-11-10 07:50:43 +00:00
}
}
2021-11-19 06:50:24 +00:00
// FindRole ...
func FindRole(roleID string) (r model.Role, err error) {
var (
ctx = context.Background()
)
// Find role exists or not
id, isValid := mongodb.NewIDFromString(roleID)
if !isValid {
2021-12-07 07:28:52 +00:00
err = errors.New(internal.ErrorInvalidRole)
2021-11-19 06:50:24 +00:00
return
}
role, _ := findByID(ctx, id)
if role.ID.IsZero() {
2021-12-07 07:28:52 +00:00
err = errors.New(internal.ErrorNotFoundRole)
2021-11-19 06:50:24 +00:00
return
}
r = getResponse(role)
return
}