GetAccountByNumber retrieves an account based on its number. It queries the database for an account with the given number and returns the account details if found. Parameters: - number: The account number to search for. Returns: - A pointer to the Account object if found, or an error if the account
(number string)
| 296 | // Returns: |
| 297 | // - A pointer to the Account object if found, or an error if the account is not found or a query error occurs. |
| 298 | func (d Datasource) GetAccountByNumber(number string) (*model.Account, error) { |
| 299 | // Query the database for the account with the given number |
| 300 | row := d.Conn.QueryRowContext(context.Background(), ` |
| 301 | SELECT account_id, name, number, bank_name, created_at, meta_data |
| 302 | FROM ledgerforge.accounts WHERE number = $1 |
| 303 | `, number) |
| 304 | |
| 305 | account := &model.Account{} |
| 306 | var metaDataJSON []byte |
| 307 | |
| 308 | // Scan the result into the Account object |
| 309 | err := row.Scan(&account.AccountID, &account.Name, &account.Number, &account.BankName, &account.CreatedAt, &metaDataJSON) |
| 310 | if err != nil { |
| 311 | if err == sql.ErrNoRows { |
| 312 | return nil, fmt.Errorf("account with number '%s' not found", number) |
| 313 | } |
| 314 | return nil, err |
| 315 | } |
| 316 | |
| 317 | // Unmarshal the metadata JSON into the MetaData field |
| 318 | err = json.Unmarshal(metaDataJSON, &account.MetaData) |
| 319 | if err != nil { |
| 320 | return nil, err |
| 321 | } |
| 322 | |
| 323 | // Return the account object |
| 324 | return account, nil |
| 325 | } |
| 326 | |
| 327 | // UpdateAccount updates a specific account in the database. |
| 328 | // It updates the account's name, number, bank name, and metadata based on the account ID. |
no outgoing calls