From b4ddce55587ef5a6c673531955074e232d807830 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 11 Feb 2025 19:24:59 -0600 Subject: [PATCH] Add helper function --- audit_log.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/audit_log.go b/audit_log.go index 15129a5..b88ad73 100644 --- a/audit_log.go +++ b/audit_log.go @@ -28,8 +28,10 @@ func GetAuditLogs(ctx context.Context, opts *database.FilterOptions) ([]*AuditLo if err := database.WithTx(ctx, database.TxOptionsRO, func(tx *sql.Tx) error { q := opts.GetBuilder(nil) rows, err := q. - Columns("id", "user_id", "ip_address", "event_type", "details", "metadata", "created_on"). - From("audit_log"). + Columns("al.id", "al.user_id", "al.ip_address", "al.event_type", + "al.details", "al.metadata", "al.created_on"). + From("audit_log al"). + LeftJoin("users u ON al.user_id = u.id"). PlaceholderFormat(sq.Dollar). RunWith(tx). QueryContext(ctx) @@ -95,3 +97,13 @@ func (al *AuditLog) AddMetadata(name string, value any) { } al.Metadata[name] = value } + +// New is a helper to create an audit log instance for you +func New(userID int, ipAddress, eventType, details string) *AuditLog { + return &AuditLog{ + UserID: userID, + IPAddress: ipAddress, + EventType: eventType, + Details: details, + } +} -- 2.45.3