Skip to content

Commit

Permalink
trade: support custom order by column
Browse files Browse the repository at this point in the history
  • Loading branch information
ycdesu committed Jul 9, 2024
1 parent be27d32 commit bce68ff
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
32 changes: 27 additions & 5 deletions pkg/service/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -32,7 +33,11 @@ type QueryTradesOptions struct {

// ASC or DESC
Ordering string
Limit uint64

// OrderByColumn is the column name to order by
// Currently we only support traded_at and gid column.
OrderByColumn string
Limit uint64
}

type TradingVolume struct {
Expand Down Expand Up @@ -304,12 +309,29 @@ func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)
sel = sel.Where(sq.Eq{"exchange": options.Sessions})
}

if options.Ordering != "" {
sel = sel.OrderBy("traded_at " + options.Ordering)
} else {
sel = sel.OrderBy("traded_at ASC")
var orderByColumn string
switch options.OrderByColumn {
case "":
orderByColumn = "traded_at"
case "traded_at", "gid":
orderByColumn = options.OrderByColumn
default:
return nil, fmt.Errorf("invalid order by column: %s", options.OrderByColumn)
}

var ordering string

switch strings.ToUpper(options.Ordering) {
case "":
ordering = "ASC"
case "ASC", "DESC":
ordering = options.Ordering
default:
return nil, fmt.Errorf("invalid ordering: %s", options.Ordering)
}

sel = sel.OrderBy(orderByColumn + " " + ordering)

if options.Limit > 0 {
sel = sel.Limit(options.Limit)
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/service/trade_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package service

import (
"database/sql"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -87,3 +89,32 @@ func Test_queryTradesSQL(t *testing.T) {
}))
})
}

func TestTradeService_Query(t *testing.T) {
db, mock, err := sqlmock.New()
if !assert.NoError(t, err) {
return
}
defer db.Close()

sqlxDB := sqlx.NewDb(db, "mysql")
defer sqlxDB.Close()

s := NewTradeService(sqlxDB)

_, err = s.Query(QueryTradesOptions{Ordering: "test_ordering"})
assert.Error(t, err)
assert.Equal(t, "invalid ordering: test_ordering", err.Error())

_, err = s.Query(QueryTradesOptions{OrderByColumn: "invalid_column"})
assert.Error(t, err)
assert.Equal(t, "invalid order by column: invalid_column", err.Error())

mock.ExpectQuery("SELECT \\* FROM trades ORDER BY gid DESC").WillReturnError(sql.ErrNoRows)
_, err = s.Query(QueryTradesOptions{Ordering: "DESC", OrderByColumn: "gid"})
assert.Equal(t, sql.ErrNoRows, err)

mock.ExpectQuery("SELECT \\* FROM trades ORDER BY traded_at ASC").WillReturnError(sql.ErrNoRows)
_, err = s.Query(QueryTradesOptions{Ordering: "ASC", OrderByColumn: "traded_at"})
assert.Equal(t, sql.ErrNoRows, err)
}

0 comments on commit bce68ff

Please sign in to comment.