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
|
|
|
|
2021-11-10 07:50:43 +00:00
|
|
|
"github.com/Selly-Modules/mongodb"
|
|
|
|
"github.com/Selly-Modules/usermngmt/internal"
|
2021-11-10 04:01:39 +00:00
|
|
|
"github.com/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 {
|
|
|
|
return errors.New("invalid role id data")
|
|
|
|
}
|
|
|
|
|
2021-11-16 04:11:04 +00:00
|
|
|
// Find roleID exists or not
|
|
|
|
if !isRoleIDExisted(ctx, id) {
|
|
|
|
return errors.New("role not found")
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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,
|
|
|
|
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 {
|
|
|
|
err = errors.New("invalid role id data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
role, _ := findByID(ctx, id)
|
|
|
|
if role.ID.IsZero() {
|
|
|
|
err = errors.New("role not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r = getResponse(role)
|
|
|
|
return
|
|
|
|
}
|