GetAccountByID retrieves an account by its ID from the database. It uses a transaction to ensure consistency and can include additional related entities like balance, identity, or ledger if specified in the `include` parameter. Parameters: - id: The ID of the account to retrieve. - include: A list o
(id string, include []string)
| 67 | // Returns: |
| 68 | // - A pointer to the retrieved Account or an error if something goes wrong. |
| 69 | func (d Datasource) GetAccountByID(id string, include []string) (*model.Account, error) { |
| 70 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) |
| 71 | defer cancel() |
| 72 | |
| 73 | // Start a transaction |
| 74 | tx, err := d.Conn.BeginTx(ctx, nil) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | // Prepare the query with additional includes if needed |
| 80 | var queryBuilder strings.Builder |
| 81 | query := prepareAccountQueries(queryBuilder, include) |
| 82 | |
| 83 | // Execute the query |
| 84 | row := tx.QueryRow(query, id) |
| 85 | |
| 86 | // Scan the result into the account object |
| 87 | account, err := scanAccountRow(row, tx, include) |
| 88 | if err != nil { |
| 89 | if err == sql.ErrNoRows { |
| 90 | // No account found for the given ID |
| 91 | return nil, fmt.Errorf("account with ID '%s' not found", id) |
| 92 | } |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | // Commit the transaction |
| 97 | err = tx.Commit() |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | // Return the account object |
| 103 | return account, nil |
| 104 | } |
| 105 | |
| 106 | // prepareAccountQueries constructs an SQL query for retrieving accounts, including |
| 107 | // optional related entities such as balance, identity, and ledger if specified in the `include` parameter. |