CreateMonitor creates a new BalanceMonitor record in the database. This function generates a unique MonitorID for the monitor, sets the creation timestamp, and inserts the monitor's data into the `ledgerforge.balance_monitors` table. Parameters: - monitor: A model.BalanceMonitor object containing d
(monitor model.BalanceMonitor)
| 1126 | // - model.BalanceMonitor: The newly created BalanceMonitor object with updated MonitorID and CreatedAt timestamp. |
| 1127 | // - error: If any errors occur during the creation process, an `APIError` is returned. |
| 1128 | func (d Datasource) CreateMonitor(monitor model.BalanceMonitor) (model.BalanceMonitor, error) { |
| 1129 | // Generate a unique MonitorID and set the current timestamp for CreatedAt |
| 1130 | monitor.MonitorID = model.GenerateUUIDWithSuffix("mon") |
| 1131 | monitor.CreatedAt = time.Now() |
| 1132 | |
| 1133 | // If PreciseValue is nil, initialize it to 0 |
| 1134 | if monitor.Condition.PreciseValue == nil { |
| 1135 | monitor.Condition.PreciseValue = big.NewInt(0) |
| 1136 | } |
| 1137 | |
| 1138 | // Insert the monitor data into the balance_monitors table |
| 1139 | _, err := d.Conn.ExecContext(context.Background(), ` |
| 1140 | INSERT INTO ledgerforge.balance_monitors (monitor_id, balance_id, field, operator, value, precision, precise_value, description, call_back_url, created_at) |
| 1141 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) |
| 1142 | `, monitor.MonitorID, monitor.BalanceID, monitor.Condition.Field, monitor.Condition.Operator, monitor.Condition.Value, monitor.Condition.Precision, monitor.Condition.PreciseValue.String(), monitor.Description, monitor.CallBackURL, monitor.CreatedAt) |
| 1143 | // Handle database errors |
| 1144 | if err != nil { |
| 1145 | pqErr, ok := err.(*pq.Error) |
| 1146 | if ok { |
| 1147 | // Handle unique violation error |
| 1148 | switch pqErr.Code.Name() { |
| 1149 | case "unique_violation": |
| 1150 | return model.BalanceMonitor{}, apierror.NewAPIError(apierror.ErrConflict, "Monitor with this ID already exists", err) |
| 1151 | // Handle foreign key violation error |
| 1152 | case "foreign_key_violation": |
| 1153 | return model.BalanceMonitor{}, apierror.NewAPIError(apierror.ErrBadRequest, "Invalid balance ID", err) |
| 1154 | // Handle other database errors |
| 1155 | default: |
| 1156 | return model.BalanceMonitor{}, apierror.NewAPIError(apierror.ErrInternalServer, "Database error occurred", err) |
| 1157 | } |
| 1158 | } |
| 1159 | // Return a generic internal server error if no specific error is matched |
| 1160 | return model.BalanceMonitor{}, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to create monitor", err) |
| 1161 | } |
| 1162 | |
| 1163 | // Return the successfully created BalanceMonitor object |
| 1164 | return monitor, nil |
| 1165 | } |
| 1166 | |
| 1167 | // GetMonitorByID retrieves a BalanceMonitor by its unique MonitorID from the database. |
| 1168 | // It queries the `ledgerforge.balance_monitors` table and maps the result into a model.BalanceMonitor object. |