MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / GetSourceDestination

Method GetSourceDestination

database/balance.go:767–822  ·  view source on GitHub ↗

GetSourceDestination retrieves balances for both the source and destination by their IDs. It queries the database using a stored procedure `ledgerforge.get_balances_by_id`, which takes the sourceId and destinationId as inputs. The function processes each balance, converting balance fields to big.Int

(sourceId, destinationId string)

Source from the content-addressed store, hash-verified

765// - []*model.Balance: A slice of pointers to Balance objects containing the source and destination balances with their details such as balance amount, credit balance, debit balance, and metadata.
766// - error: An error if any occurs during the query execution, data retrieval, or JSON parsing.
767func (d Datasource) GetSourceDestination(sourceId, destinationId string) ([]*model.Balance, error) {
768 // Execute SQL query to select balances for source and destination using a stored procedure
769 rows, err := d.Conn.QueryContext(context.Background(), `
770 SELECT ledgerforge.get_balances_by_id($1,$2)
771 `, sourceId, destinationId)
772 if err != nil {
773 // Return an error if the query execution fails
774 return nil, err
775 }
776 defer func(rows *sql.Rows) {
777 // Ensure the rows are closed after the query is completed
778 err := rows.Close()
779 if err != nil {
780 logrus.WithError(err).Error("failed to close rows") // Log any error that occurs while closing the rows
781 }
782 }(rows)
783
784 // Slice to store the retrieved balances
785 var balances []*model.Balance
786
787 // Iterate through the result set and scan each row into a Balance object
788 for rows.Next() {
789 balance := model.Balance{}
790 var metaDataJSON []byte
791
792 // Scan the values from the current row into the balance object and temporary metadata variable
793 err = rows.Scan(
794 &balance.BalanceID,
795 &balance.Balance,
796 &balance.CreditBalance,
797 &balance.DebitBalance,
798 &balance.Currency,
799 &balance.CurrencyMultiplier,
800 &balance.LedgerID,
801 &balance.CreatedAt,
802 &metaDataJSON,
803 )
804 if err != nil {
805 // Return an error if scanning the row fails
806 return nil, err
807 }
808
809 // Parse the metadata JSON into the MetaData map field
810 err = json.Unmarshal(metaDataJSON, &balance.MetaData)
811 if err != nil {
812 // Return an error if JSON parsing fails
813 return nil, err
814 }
815
816 // Append the balance to the slice of balances
817 balances = append(balances, &balance)
818 }
819
820 // Return the slice of balances containing the source and destination balances
821 return balances, nil
822}
823
824// UpdateBalances updates both the source and destination balances in a single transaction.

Calls 2

CloseMethod · 0.45
ErrorMethod · 0.45