diff --git a/action.go b/action.go index 5eb61ba..3774fdd 100644 --- a/action.go +++ b/action.go @@ -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 // diff --git a/role/db.go b/role/db.go index e02889b..020eac5 100644 --- a/role/db.go +++ b/role/db.go @@ -104,4 +104,13 @@ func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { return false } return total != 0 -} \ No newline at end of file +} + +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 +} diff --git a/role/handle.go b/role/handle.go index 87d8d78..19a60aa 100644 --- a/role/handle.go +++ b/role/handle.go @@ -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 +}