MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / GetAllLedgersWithFilterAndOptions

Method GetAllLedgersWithFilterAndOptions

database/ledger.go:242–330  ·  view source on GitHub ↗

GetAllLedgersWithFilterAndOptions retrieves ledgers with filtering, sorting, and optional count. It uses the filter package to build SQL WHERE and ORDER BY conditions. Parameters: - ctx: Context for the database operation. - filters: A QueryFilterSet containing the filter conditions. - opts: Query

(ctx context.Context, filters *filter.QueryFilterSet, opts *filter.QueryOptions, limit, offset int)

Source from the content-addressed store, hash-verified

240// - *int64: Optional total count of matching records (if opts.IncludeCount is true).
241// - error: An error if the query fails or if there's an issue processing the results.
242func (d Datasource) GetAllLedgersWithFilterAndOptions(ctx context.Context, filters *filter.QueryFilterSet, opts *filter.QueryOptions, limit, offset int) ([]model.Ledger, *int64, error) {
243 if limit <= 0 || limit > 100 {
244 limit = 20
245 }
246
247 if opts == nil {
248 opts = &filter.QueryOptions{}
249 }
250 if err := filter.ValidateSortByForTable(opts, "ledgers"); err != nil {
251 return nil, nil, apierror.NewAPIError(apierror.ErrBadRequest, "Invalid sort_by field", nil)
252 }
253
254 result, err := filter.BuildWithOptions(filters, "ledgers", "", 1, opts)
255 if err != nil {
256 return nil, nil, apierror.NewAPIError(apierror.ErrBadRequest, fmt.Sprintf("Invalid filter: %s", err.Error()), err)
257 }
258
259 // Determine select fields based on whether count is requested
260 selectFields := "ledger_id, name, created_at, meta_data"
261 if opts != nil && opts.IncludeCount {
262 selectFields = "ledger_id, name, created_at, meta_data, COUNT(*) OVER() AS total_count"
263 }
264
265 // Build base query
266 baseQuery := fmt.Sprintf(`
267 SELECT %s
268 FROM ledgerforge.ledgers
269 `, selectFields)
270
271 var args []interface{}
272 args = append(args, result.Args...)
273 argPos := result.NextArgPos
274
275 // Add WHERE clause if filters are provided
276 if len(result.Conditions) > 0 {
277 logicalOperator := filter.LogicalAnd
278 if filters != nil {
279 logicalOperator = filters.LogicalOperator
280 }
281 baseQuery += " WHERE " + filter.BuildConditionExpression(result.Conditions, logicalOperator)
282 }
283
284 // Add ORDER BY clause
285 baseQuery += " ORDER BY " + result.OrderBy
286
287 // Add pagination
288 baseQuery += fmt.Sprintf(" LIMIT $%d OFFSET $%d", argPos, argPos+1)
289 args = append(args, limit, offset)
290
291 rows, err := d.Conn.QueryContext(ctx, baseQuery, args...)
292 if err != nil {
293 return nil, nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to retrieve ledgers", err)
294 }
295 defer func() { _ = rows.Close() }()
296
297 ledgers := []model.Ledger{}
298 var totalCount *int64
299

Callers 1

Calls 6

ValidateSortByForTableFunction · 0.92
NewAPIErrorFunction · 0.92
BuildWithOptionsFunction · 0.92
BuildConditionExpressionFunction · 0.92
ErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected