Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: sorting actions #15

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions internal/storage/postgres/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,45 +56,47 @@ func (a *Action) ByTxId(ctx context.Context, txId uint64, limit, offset int) (ac
}

func (a *Action) ByAddress(ctx context.Context, addressId uint64, filters storage.AddressActionsFilter) (actions []storage.AddressAction, err error) {
query := a.DB().NewSelect().
subQuery := a.DB().NewSelect().
Model((*storage.AddressAction)(nil)).
Where("address_id = ?", addressId)

if filters.ActionTypes.Bits > 0 {
query = query.Where("action_type IN (?)", bun.In(filters.ActionTypes.Strings()))
subQuery = subQuery.Where("action_type IN (?)", bun.In(filters.ActionTypes.Strings()))
}

query = sortScope(query, "action_id", filters.Sort)
query = limitScope(query, filters.Limit)
query = offsetScope(query, filters.Offset)
subQuery = sortScope(subQuery, "action_id", filters.Sort)
subQuery = limitScope(subQuery, filters.Limit)
subQuery = offsetScope(subQuery, filters.Offset)

err = a.DB().NewSelect().
TableExpr("(?) as address_action", query).
query := a.DB().NewSelect().
TableExpr("(?) as address_action", subQuery).
ColumnExpr("address_action.*").
ColumnExpr("action.id as action__id, action.height as action__height, action.time as action__time, action.position as action__position, action.type as action__type, action.tx_id as action__tx_id, action.data as action__data").
ColumnExpr("tx.hash as tx__hash").
Join("left join tx on tx.id = address_action.tx_id").
Join("left join action on action.id = address_action.action_id").
Scan(ctx, &actions)
Join("left join action on action.id = address_action.action_id")
query = sortScope(query, "action_id", filters.Sort)
err = query.Scan(ctx, &actions)
return
}

func (a *Action) ByRollup(ctx context.Context, rollupId uint64, limit, offset int, sort sdk.SortOrder) (actions []storage.RollupAction, err error) {
query := a.DB().NewSelect().
subQuery := a.DB().NewSelect().
Model((*storage.RollupAction)(nil)).
Where("rollup_id = ?", rollupId)

query = sortScope(query, "action_id", sort)
query = limitScope(query, limit)
query = offsetScope(query, offset)
subQuery = sortScope(subQuery, "action_id", sort)
subQuery = limitScope(subQuery, limit)
subQuery = offsetScope(subQuery, offset)

err = a.DB().NewSelect().
TableExpr("(?) as rollup_action", query).
query := a.DB().NewSelect().
TableExpr("(?) as rollup_action", subQuery).
ColumnExpr("rollup_action.*").
ColumnExpr("action.id as action__id, action.height as action__height, action.time as action__time, action.position as action__position, action.type as action__type, action.tx_id as action__tx_id, action.data as action__data").
ColumnExpr("tx.hash as tx__hash").
Join("left join tx on tx.id = rollup_action.tx_id").
Join("left join action on action.id = rollup_action.action_id").
Scan(ctx, &actions)
Join("left join action on action.id = rollup_action.action_id")
query = sortScope(query, "action_id", sort)
err = query.Scan(ctx, &actions)
return
}
Loading