DeleteMonitor deletes a balance monitor from the database by its monitor ID. It removes the monitor from the `ledgerforge.balance_monitors` table. Parameters: - id: The ID of the monitor to be deleted. Returns: - error: If the deletion fails or the monitor is not found, an appropriate `APIError` i
(id string)
| 1357 | // Returns: |
| 1358 | // - error: If the deletion fails or the monitor is not found, an appropriate `APIError` is returned. |
| 1359 | func (d Datasource) DeleteMonitor(id string) error { |
| 1360 | // Execute the SQL DELETE statement, removing the monitor by its ID |
| 1361 | result, err := d.Conn.ExecContext(context.Background(), ` |
| 1362 | DELETE FROM ledgerforge.balance_monitors WHERE monitor_id = $1 |
| 1363 | `, id) |
| 1364 | // If an error occurred during execution, return an internal server error |
| 1365 | if err != nil { |
| 1366 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to delete monitor", err) |
| 1367 | } |
| 1368 | |
| 1369 | // Check how many rows were affected by the delete operation |
| 1370 | rowsAffected, err := result.RowsAffected() |
| 1371 | if err != nil { |
| 1372 | // If an error occurred while checking rows affected, return an internal server error |
| 1373 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to get rows affected", err) |
| 1374 | } |
| 1375 | |
| 1376 | // If no rows were affected, return a not found error indicating the monitor does not exist |
| 1377 | if rowsAffected == 0 { |
| 1378 | return apierror.NewAPIError(apierror.ErrNotFound, fmt.Sprintf("Monitor with ID '%s' not found", id), nil) |
| 1379 | } |
| 1380 | |
| 1381 | // Return nil if the deletion was successful |
| 1382 | return nil |
| 1383 | } |
| 1384 | |
| 1385 | // TakeBalanceSnapshots creates daily snapshots of balances in batches. |
| 1386 | // It uses the PostgreSQL function to process balances in chunks to avoid memory issues |