From 41c52b6fc6412584e4f355763304cbb4bb095b02 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 19 Nov 2021 13:50:24 +0700 Subject: [PATCH] add findRoleMethod --- action.go | 5 +++++ role/db.go | 11 ++++++++++- role/handle.go | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) 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 +}