Skip to content

Commit 9e031f7

Browse files
Added new dbconv function for transactions
1 parent d001aab commit 9e031f7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

internal/session/dbconv.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package session
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
47
"github.com/google/uuid"
58
"github.com/jackc/pgx/v5/pgtype"
69

710
"github.com/PythonHacker24/linux-acl-management-backend/internal/postgresql"
11+
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
812
)
913

1014
/* converts project session struct into PostgreSQL supported format */
@@ -35,3 +39,50 @@ func ConvertSessionToStoreParams(session *Session) (*postgresql.StoreSessionPQPa
3539
Expiry: expiry,
3640
}, nil
3741
}
42+
43+
/* converts project transaction struct into PostgreSQL supported format */
44+
func ConvertTransactiontoStoreParams(tx types.Transaction) (postgresql.CreateTransactionPQParams, error) {
45+
/* marshal ACL entries to JSON bytes */
46+
entriesJSON, err := json.Marshal(tx.Entries)
47+
if err != nil {
48+
return postgresql.CreateTransactionPQParams{}, fmt.Errorf("failed to marshal ACL entries: %w", err)
49+
}
50+
51+
/* convert timestamp to pgtype.Timestamptz */
52+
var timestamp pgtype.Timestamptz
53+
if err := timestamp.Scan(tx.Timestamp); err != nil {
54+
return postgresql.CreateTransactionPQParams{}, fmt.Errorf("failed to convert timestamp: %w", err)
55+
}
56+
57+
/* handle optional error message */
58+
var errorMsg pgtype.Text
59+
if tx.ErrorMsg != "" {
60+
errorMsg = pgtype.Text{String: tx.ErrorMsg, Valid: true}
61+
}
62+
63+
/* handle optional output */
64+
var output pgtype.Text
65+
if tx.Output != "" {
66+
output = pgtype.Text{String: tx.Output, Valid: true}
67+
}
68+
69+
/* Handle optional duration */
70+
var durationMs pgtype.Int8
71+
if tx.DurationMs > 0 {
72+
durationMs = pgtype.Int8{Int64: tx.DurationMs, Valid: true}
73+
}
74+
75+
return postgresql.CreateTransactionPQParams{
76+
ID: tx.ID,
77+
SessionID: tx.SessionID,
78+
Timestamp: timestamp,
79+
Operation: string(tx.Operation),
80+
TargetPath: tx.TargetPath,
81+
Entries: entriesJSON,
82+
Status: string(tx.Status),
83+
ErrorMsg: errorMsg,
84+
Output: output,
85+
ExecutedBy: tx.ExecutedBy,
86+
DurationMs: durationMs,
87+
}, nil
88+
}

0 commit comments

Comments
 (0)