Skip to content

Commit 53fd510

Browse files
authored
Sort by transaction time (Asc/Desc) (#32)
* Sort by transaction time * Address review comments * Add sort by time constants
1 parent 1682023 commit 53fd510

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

models/search.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var (
1616
SearchNamespaceAccounts = "accounts"
1717
// SearchNamespaceTransactions holds search namespace of transactions
1818
SearchNamespaceTransactions = "transactions"
19+
// SortDescByTime option sorts search items in descending order of time
20+
SortDescByTime = "desc"
21+
// SortAscByTime option sorts search items in ascending order of time
22+
SortAscByTime = "asc"
1923
)
2024

2125
// SearchEngine is the interface for all search operations
@@ -118,9 +122,10 @@ type QueryContainer struct {
118122

119123
// SearchRawQuery represents the format of search query
120124
type SearchRawQuery struct {
121-
Offset int `json:"from,omitempty"`
122-
Limit int `json:"size,omitempty"`
123-
Query struct {
125+
Offset int `json:"from,omitempty"`
126+
Limit int `json:"size,omitempty"`
127+
SortTime string `json:"sort_time,omitempty"`
128+
Query struct {
124129
MustClause QueryContainer `json:"must"`
125130
ShouldClause QueryContainer `json:"should"`
126131
} `json:"query"`
@@ -188,9 +193,9 @@ func (rawQuery *SearchRawQuery) ToSQLQuery(namespace string) *SearchSQLQuery {
188193
var args []interface{}
189194

190195
switch namespace {
191-
case "accounts":
196+
case SearchNamespaceAccounts:
192197
q = "SELECT id, balance, data FROM current_balances"
193-
case "transactions":
198+
case SearchNamespaceTransactions:
194199
q = `SELECT id, timestamp, data,
195200
array_to_json(ARRAY(
196201
SELECT lines.account_id FROM lines
@@ -244,27 +249,31 @@ func (rawQuery *SearchRawQuery) ToSQLQuery(namespace string) *SearchSQLQuery {
244249
return &SearchSQLQuery{sql: q, args: args}
245250
}
246251

247-
q = q + " WHERE "
252+
q += " WHERE "
248253
if len(mustWhere) != 0 {
249-
q = q + "(" + strings.Join(mustWhere, " AND ") + ")"
254+
q += "(" + strings.Join(mustWhere, " AND ") + ")"
250255
if len(shouldWhere) != 0 {
251-
q = q + " AND "
256+
q += " AND "
252257
}
253258
}
254259

255260
if len(shouldWhere) != 0 {
256-
q = q + "(" + strings.Join(shouldWhere, " OR ") + ")"
261+
q += "(" + strings.Join(shouldWhere, " OR ") + ")"
257262
}
258263

259-
if namespace == "transactions" {
260-
q = q + " ORDER BY timestamp"
264+
if namespace == SearchNamespaceTransactions {
265+
if rawQuery.SortTime == SortDescByTime {
266+
q += " ORDER BY timestamp DESC"
267+
} else {
268+
q += " ORDER BY timestamp"
269+
}
261270
}
262271

263272
if offset > 0 {
264-
q = q + " OFFSET " + strconv.Itoa(offset) + " "
273+
q += " OFFSET " + strconv.Itoa(offset) + " "
265274
}
266275
if limit > 0 {
267-
q = q + " LIMIT " + strconv.Itoa(limit)
276+
q += " LIMIT " + strconv.Itoa(limit)
268277
}
269278

270279
q = enumerateSQLPlacholder(q)

0 commit comments

Comments
 (0)