CreateAccount inserts a new Account into the database. This function handles metadata serialization and database insertion. Parameters: - account: The account model containing fields such as name, number, bank name, currency, ledger ID, identity ID, and balance ID. Returns: - model.Account: The crea
(account model.Account)
| 38 | // - model.Account: The created account with the assigned account ID and creation timestamp. |
| 39 | // - error: Returns an error if any issue occurs while marshalling metadata or executing the database query. |
| 40 | func (d Datasource) CreateAccount(account model.Account) (model.Account, error) { |
| 41 | // Serialize metadata into JSON |
| 42 | metaDataJSON, err := json.Marshal(account.MetaData) |
| 43 | if err != nil { |
| 44 | return account, err // Return error if metadata marshalling fails |
| 45 | } |
| 46 | |
| 47 | // Generate a unique account ID and assign the current time for the account creation |
| 48 | account.AccountID = model.GenerateUUIDWithSuffix("acc") |
| 49 | account.CreatedAt = time.Now() |
| 50 | |
| 51 | // Insert the new account into the database |
| 52 | _, err = d.Conn.ExecContext(context.Background(), ` |
| 53 | INSERT INTO ledgerforge.accounts (account_id, name, number, bank_name, currency, ledger_id, identity_id, balance_id, created_at, meta_data) |
| 54 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) |
| 55 | `, account.AccountID, account.Name, account.Number, account.BankName, account.Currency, account.LedgerID, account.IdentityID, account.BalanceID, account.CreatedAt, metaDataJSON) |
| 56 | |
| 57 | // Return the account object and any error that occurred during the database operation |
| 58 | return account, err |
| 59 | } |
| 60 | |
| 61 | // GetAccountByID retrieves an account by its ID from the database. |
| 62 | // It uses a transaction to ensure consistency and can include additional |