|
1 | 1 | package session |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
4 | 7 | "github.com/google/uuid" |
5 | 8 | "github.com/jackc/pgx/v5/pgtype" |
6 | 9 |
|
7 | 10 | "github.com/PythonHacker24/linux-acl-management-backend/internal/postgresql" |
| 11 | + "github.com/PythonHacker24/linux-acl-management-backend/internal/types" |
8 | 12 | ) |
9 | 13 |
|
10 | 14 | /* converts project session struct into PostgreSQL supported format */ |
@@ -35,3 +39,50 @@ func ConvertSessionToStoreParams(session *Session) (*postgresql.StoreSessionPQPa |
35 | 39 | Expiry: expiry, |
36 | 40 | }, nil |
37 | 41 | } |
| 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