GetMonitorByID retrieves a BalanceMonitor by its unique MonitorID from the database. It queries the `ledgerforge.balance_monitors` table and maps the result into a model.BalanceMonitor object. Parameters: - id: The MonitorID of the monitor to retrieve. Returns: - *model.BalanceMonitor: A pointer t
(id string)
| 1174 | // - *model.BalanceMonitor: A pointer to the BalanceMonitor object if found. |
| 1175 | // - error: If the monitor is not found or if any errors occur during the query, an `APIError` is returned. |
| 1176 | func (d Datasource) GetMonitorByID(id string) (*model.BalanceMonitor, error) { |
| 1177 | var preciseValue int64 // Temporary variable to hold the precise value as int64 |
| 1178 | |
| 1179 | // Query the database to get the monitor details by MonitorID |
| 1180 | row := d.Conn.QueryRowContext(context.Background(), ` |
| 1181 | SELECT monitor_id, balance_id, field, operator, value, precision, precise_value, description, call_back_url, created_at |
| 1182 | FROM ledgerforge.balance_monitors WHERE monitor_id = $1 |
| 1183 | `, id) |
| 1184 | |
| 1185 | // Initialize an empty BalanceMonitor object |
| 1186 | monitor := &model.BalanceMonitor{} |
| 1187 | // Initialize an empty AlertCondition object (part of the monitor) |
| 1188 | condition := &model.AlertCondition{} |
| 1189 | |
| 1190 | // Scan the result into the monitor and condition fields |
| 1191 | err := row.Scan(&monitor.MonitorID, &monitor.BalanceID, &condition.Field, &condition.Operator, &condition.Value, &condition.Precision, &preciseValue, &monitor.Description, &monitor.CallBackURL, &monitor.CreatedAt) |
| 1192 | if err != nil { |
| 1193 | // Handle the case where the monitor with the specified ID is not found |
| 1194 | if err == sql.ErrNoRows { |
| 1195 | return nil, apierror.NewAPIError(apierror.ErrNotFound, fmt.Sprintf("Monitor with ID '%s' not found", id), err) |
| 1196 | } |
| 1197 | // Return an internal server error for other query issues |
| 1198 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to retrieve monitor", err) |
| 1199 | } |
| 1200 | |
| 1201 | // Populate the PreciseValue field in the condition (convert from int64 to big.Int) |
| 1202 | monitor.Condition = *condition |
| 1203 | monitor.Condition.PreciseValue = big.NewInt(preciseValue) |
| 1204 | |
| 1205 | // Return the populated BalanceMonitor object |
| 1206 | return monitor, nil |
| 1207 | } |
| 1208 | |
| 1209 | // GetAllMonitors retrieves all balance monitors from the database. |
| 1210 | // It queries the `ledgerforge.balance_monitors` table and returns a list of all monitors. |