GetMatchingRules retrieves all matching rules from the database. Parameters: - ctx: Context for managing the request and tracing. Returns: - A slice of MatchingRule pointers or an error wrapped in an APIError if the operation fails.
(ctx context.Context)
| 441 | // Returns: |
| 442 | // - A slice of MatchingRule pointers or an error wrapped in an APIError if the operation fails. |
| 443 | func (d Datasource) GetMatchingRules(ctx context.Context) ([]*model.MatchingRule, error) { |
| 444 | ctx, span := otel.Tracer("reconciliation.database").Start(ctx, "Fetching matching rules") |
| 445 | defer span.End() |
| 446 | |
| 447 | // Execute the query to fetch all matching rules |
| 448 | rows, err := d.Conn.QueryContext(ctx, ` |
| 449 | SELECT id, rule_id, created_at, updated_at, name, description, criteria |
| 450 | FROM ledgerforge.matching_rules |
| 451 | `) |
| 452 | if err != nil { |
| 453 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to retrieve matching rules", err) |
| 454 | } |
| 455 | defer func() { _ = rows.Close() }() |
| 456 | |
| 457 | var rules []*model.MatchingRule |
| 458 | |
| 459 | // Iterate over the rows to scan each rule into a MatchingRule object |
| 460 | for rows.Next() { |
| 461 | rule := &model.MatchingRule{} |
| 462 | var criteriaJSON []byte |
| 463 | err = rows.Scan( |
| 464 | &rule.ID, &rule.RuleID, &rule.CreatedAt, &rule.UpdatedAt, |
| 465 | &rule.Name, &rule.Description, &criteriaJSON, |
| 466 | ) |
| 467 | if err != nil { |
| 468 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to scan matching rule data", err) |
| 469 | } |
| 470 | |
| 471 | // Unmarshal the criteria JSON into the MatchingRule's Criteria field |
| 472 | err = json.Unmarshal(criteriaJSON, &rule.Criteria) |
| 473 | if err != nil { |
| 474 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to unmarshal matching rule criteria", err) |
| 475 | } |
| 476 | |
| 477 | // Append the rule to the rules slice |
| 478 | rules = append(rules, rule) |
| 479 | } |
| 480 | |
| 481 | // Check for any errors that occurred during the iteration |
| 482 | if err = rows.Err(); err != nil { |
| 483 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Error occurred while iterating over matching rules", err) |
| 484 | } |
| 485 | |
| 486 | return rules, nil |
| 487 | } |
| 488 | |
| 489 | // UpdateMatchingRule updates a specific matching rule in the database. |
| 490 | // Parameters: |