diff --git a/action.go b/action.go index 0eb7e1f..e10eefa 100644 --- a/action.go +++ b/action.go @@ -107,6 +107,15 @@ func (s Service) UpdatePermission(permissionID string, payload model.PermissionU return nil } +// DeletePermission ... +func (s Service) DeletePermission(permissionID string) error { + if err := permission.Delete(permissionID); err != nil { + return err + } + cache.Roles() + return nil +} + // GetAllPermissions ... func (s Service) GetAllPermissions(query model.PermissionAllQuery) model.PermissionAll { return permission.All(query) diff --git a/permission/db.go b/permission/db.go index e910fa5..320c1eb 100644 --- a/permission/db.go +++ b/permission/db.go @@ -54,6 +54,22 @@ func updateOneByCondition(ctx context.Context, cond interface{}, payload interfa return err } +func deleteOneByCondition(ctx context.Context, cond interface{}) error { + var ( + col = database.GetPermissionCol() + ) + _, err := col.DeleteOne(ctx, cond) + if err != nil { + logger.Error("usermngmt - Permission - DeleteOne", logger.LogData{ + "cond": cond, + "err": err.Error(), + }) + return fmt.Errorf("error when delete permission: %s", err.Error()) + } + + return err +} + func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) { var ( col = database.GetPermissionCol() diff --git a/permission/handle.go b/permission/handle.go index 87790d9..b17de7f 100644 --- a/permission/handle.go +++ b/permission/handle.go @@ -94,6 +94,29 @@ func Update(permissionID string, payload model.PermissionUpdateOptions) error { return nil } +// Delete ... +func Delete(permissionID string) error { + var ( + ctx = context.Background() + ) + + // Find permissionID exists or not + id, isValid := mongodb.NewIDFromString(permissionID) + if !isValid { + return errors.New("invalid permission id data") + } + if !isPermissionIDExisted(ctx, id) { + return errors.New("permission not found") + } + + // Delete + if err := deleteOneByCondition(ctx, bson.M{"_id": id}); err != nil { + return err + } + + return nil +} + // All ... func All(queryParams model.PermissionAllQuery) (r model.PermissionAll) { var (