big ole refactor
This commit is contained in:
@@ -3,18 +3,17 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type deleter[T any] struct {
|
||||
tx bun.Tx
|
||||
q *bun.DeleteQuery
|
||||
resourceID any // Store ID before deletion for audit
|
||||
auditCallback AuditCallback
|
||||
auditRequest *http.Request
|
||||
tx bun.Tx
|
||||
q *bun.DeleteQuery
|
||||
resourceID any // Store ID before deletion for audit
|
||||
audit *AuditMeta
|
||||
auditInfo *AuditInfo
|
||||
}
|
||||
|
||||
type systemType interface {
|
||||
@@ -39,11 +38,10 @@ func (d *deleter[T]) Where(query string, args ...any) *deleter[T] {
|
||||
}
|
||||
|
||||
// WithAudit enables audit logging for this delete operation
|
||||
// The callback will be invoked after successful deletion with auto-generated audit info
|
||||
// If the callback returns an error, the transaction will be rolled back
|
||||
func (d *deleter[T]) WithAudit(r *http.Request, callback AuditCallback) *deleter[T] {
|
||||
d.auditRequest = r
|
||||
d.auditCallback = callback
|
||||
// If the provided *AuditInfo is nil, will use reflection to automatically work out the details
|
||||
func (d *deleter[T]) WithAudit(meta *AuditMeta, info *AuditInfo) *deleter[T] {
|
||||
d.audit = meta
|
||||
d.auditInfo = info
|
||||
return d
|
||||
}
|
||||
|
||||
@@ -57,21 +55,23 @@ func (d *deleter[T]) Delete(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Handle audit logging if enabled
|
||||
if d.auditCallback != nil && d.auditRequest != nil {
|
||||
tableName := extractTableName[T]()
|
||||
resourceType := extractResourceType(tableName)
|
||||
action := buildAction(resourceType, "delete")
|
||||
if d.audit != nil {
|
||||
if d.auditInfo == nil {
|
||||
tableName := extractTableName[T]()
|
||||
resourceType := extractResourceType(tableName)
|
||||
action := buildAction(resourceType, "delete")
|
||||
|
||||
info := &AuditInfo{
|
||||
Action: action,
|
||||
ResourceType: resourceType,
|
||||
ResourceID: d.resourceID,
|
||||
Details: nil, // Delete doesn't need details
|
||||
d.auditInfo = &AuditInfo{
|
||||
Action: action,
|
||||
ResourceType: resourceType,
|
||||
ResourceID: d.resourceID,
|
||||
Details: nil, // Delete doesn't need details
|
||||
}
|
||||
}
|
||||
|
||||
// Call audit callback - if it fails, return error to trigger rollback
|
||||
if err := d.auditCallback(ctx, d.tx, info, d.auditRequest); err != nil {
|
||||
return errors.Wrap(err, "audit.callback")
|
||||
err = LogSuccess(ctx, d.tx, d.audit, d.auditInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "LogSuccess")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func DeleteByID[T any](tx bun.Tx, id int) *deleter[T] {
|
||||
return DeleteItem[T](tx).Where("id = ?", id)
|
||||
}
|
||||
|
||||
func DeleteWithProtection[T systemType](ctx context.Context, tx bun.Tx, id int) error {
|
||||
func DeleteWithProtection[T systemType](ctx context.Context, tx bun.Tx, id int, audit *AuditMeta) error {
|
||||
deleter := DeleteByID[T](tx, id)
|
||||
item, err := GetByID[T](tx, id).Get(ctx)
|
||||
if err != nil {
|
||||
@@ -94,5 +94,8 @@ func DeleteWithProtection[T systemType](ctx context.Context, tx bun.Tx, id int)
|
||||
if (*item).isSystem() {
|
||||
return errors.New("record is system protected")
|
||||
}
|
||||
if audit != nil {
|
||||
deleter = deleter.WithAudit(audit, nil)
|
||||
}
|
||||
return deleter.Delete(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user