everybody loves a refactor
This commit is contained in:
@@ -2,7 +2,6 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -46,13 +45,18 @@ func (d *deleter[T]) WithAudit(meta *AuditMeta, info *AuditInfo) *deleter[T] {
|
||||
}
|
||||
|
||||
func (d *deleter[T]) Delete(ctx context.Context) error {
|
||||
_, err := d.q.Exec(ctx)
|
||||
result, err := d.q.Exec(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "bun.DeleteQuery.Exec")
|
||||
}
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "result.RowsAffected")
|
||||
}
|
||||
if rows == 0 {
|
||||
resource := extractResourceType(extractTableName[T]())
|
||||
return BadRequestNotFound(resource, "id", d.resourceID)
|
||||
}
|
||||
|
||||
// Handle audit logging if enabled
|
||||
if d.audit != nil {
|
||||
@@ -88,9 +92,6 @@ func DeleteWithProtection[T systemType](ctx context.Context, tx bun.Tx, id int,
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetByID")
|
||||
}
|
||||
if item == nil {
|
||||
return errors.New("record not found")
|
||||
}
|
||||
if (*item).isSystem() {
|
||||
return errors.New("record is system protected")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user