add findRoleMethod
This commit is contained in:
parent
49be1c3c96
commit
41c52b6fc6
|
@ -101,6 +101,11 @@ func (s Service) GetAllRoles(query model.RoleAllQuery) model.RoleAll {
|
|||
return role.All(query)
|
||||
}
|
||||
|
||||
// FindRole ...
|
||||
func (s Service) FindRole(roleID string) (model.Role, error) {
|
||||
return role.FindRole(roleID)
|
||||
}
|
||||
|
||||
//
|
||||
// Permission
|
||||
//
|
||||
|
|
|
@ -105,3 +105,12 @@ func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
|
|||
}
|
||||
return total != 0
|
||||
}
|
||||
|
||||
func findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
|
||||
var (
|
||||
doc model.DBRole
|
||||
col = database.GetRoleCol()
|
||||
)
|
||||
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
|
||||
return doc, err
|
||||
}
|
||||
|
|
|
@ -142,3 +142,25 @@ func getResponse(role model.DBRole) model.Role {
|
|||
UpdatedAt: role.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue