fixed some migration issues and added generics for update and insert
This commit is contained in:
@@ -156,3 +156,32 @@ func (l *Logger) CleanupOldLogs(ctx context.Context, daysToKeep int) (int, error
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// Callback returns a db.AuditCallback that logs to this Logger
|
||||
// This is used with the generic database helpers (Insert, Update, Delete)
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// audit := auditlog.NewLogger(conn)
|
||||
// err := db.Insert(tx, season).
|
||||
// WithAudit(r, audit.Callback()).
|
||||
// Exec(ctx)
|
||||
func (l *Logger) Callback() db.AuditCallback {
|
||||
return func(ctx context.Context, tx bun.Tx, info *db.AuditInfo, r *http.Request) error {
|
||||
user := db.CurrentUser(ctx)
|
||||
if user == nil {
|
||||
return errors.New("no user in context for audit logging")
|
||||
}
|
||||
|
||||
return l.LogSuccess(
|
||||
ctx,
|
||||
tx,
|
||||
user,
|
||||
info.Action,
|
||||
info.ResourceType,
|
||||
info.ResourceID,
|
||||
info.Details,
|
||||
r,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user