refactored db code

This commit is contained in:
2026-02-09 22:14:38 +11:00
parent b89ee75ca7
commit a4b4f4f4af
7 changed files with 24 additions and 100 deletions

View File

@@ -77,12 +77,11 @@ func CreateRole(ctx context.Context, tx bun.Tx, role *Role) error {
}
role.CreatedAt = time.Now().Unix()
_, err := tx.NewInsert().
Model(role).
err := Insert(tx, role).
Returning("id").
Exec(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewInsert")
return errors.Wrap(err, "db.Insert")
}
return nil
@@ -97,12 +96,11 @@ func UpdateRole(ctx context.Context, tx bun.Tx, role *Role) error {
return errors.New("role id must be positive")
}
_, err := tx.NewUpdate().
Model(role).
err := Update(tx, role).
WherePK().
Exec(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewUpdate")
return errors.Wrap(err, "db.Update")
}
return nil
@@ -128,12 +126,11 @@ func AddPermissionToRole(ctx context.Context, tx bun.Tx, roleID, permissionID in
RoleID: roleID,
PermissionID: permissionID,
}
_, err := tx.NewInsert().
Model(rolePerm).
err := Insert(tx, rolePerm).
On("CONFLICT (role_id, permission_id) DO NOTHING").
Exec(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewInsert")
return errors.Wrap(err, "db.Insert")
}
return nil