add findRoleMethod

This commit is contained in:
Hoang 2021-11-19 13:50:24 +07:00
parent 49be1c3c96
commit 41c52b6fc6
3 changed files with 37 additions and 1 deletions

View File

@ -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
//

View File

@ -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
}

View File

@ -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
}