UpdateMonitor updates an existing balance monitor in the database. It updates fields such as `balance_id`, `field`, `operator`, `value`, `description`, and `call_back_url` for the monitor identified by `monitor_id`. Parameters: - monitor: A pointer to the `BalanceMonitor` object containing the upda
(monitor *model.BalanceMonitor)
| 1321 | // Returns: |
| 1322 | // - error: If the update fails, an appropriate `APIError` is returned. |
| 1323 | func (d Datasource) UpdateMonitor(monitor *model.BalanceMonitor) error { |
| 1324 | // Execute the SQL update statement, replacing the placeholder values with the monitor's data |
| 1325 | result, err := d.Conn.ExecContext(context.Background(), ` |
| 1326 | UPDATE ledgerforge.balance_monitors |
| 1327 | SET balance_id = $2, field = $3, operator = $4, value = $5, description = $6, call_back_url = $7 |
| 1328 | WHERE monitor_id = $1 |
| 1329 | `, monitor.MonitorID, monitor.BalanceID, monitor.Condition.Field, monitor.Condition.Operator, monitor.Condition.Value, monitor.Description, monitor.CallBackURL) |
| 1330 | // If an error occurred during execution, return an internal server error |
| 1331 | if err != nil { |
| 1332 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to update monitor", err) |
| 1333 | } |
| 1334 | |
| 1335 | // Check how many rows were affected by the update |
| 1336 | rowsAffected, err := result.RowsAffected() |
| 1337 | if err != nil { |
| 1338 | // If an error occurred while checking rows affected, return an internal server error |
| 1339 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to get rows affected", err) |
| 1340 | } |
| 1341 | |
| 1342 | // If no rows were affected, return a not found error indicating the monitor does not exist |
| 1343 | if rowsAffected == 0 { |
| 1344 | return apierror.NewAPIError(apierror.ErrNotFound, fmt.Sprintf("Monitor with ID '%s' not found", monitor.MonitorID), nil) |
| 1345 | } |
| 1346 | |
| 1347 | // Return nil if the update was successful |
| 1348 | return nil |
| 1349 | } |
| 1350 | |
| 1351 | // DeleteMonitor deletes a balance monitor from the database by its monitor ID. |
| 1352 | // It removes the monitor from the `ledgerforge.balance_monitors` table. |