diff --git a/internal/monero/monero.go b/internal/monero/monero.go index 1686af0..c385160 100644 --- a/internal/monero/monero.go +++ b/internal/monero/monero.go @@ -87,7 +87,7 @@ type QueryNodes struct { } // toSQL generates SQL query from query parameters -func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection string) { +func (q *QueryNodes) toSQL() (args []interface{}, where string) { wq := []string{} if q.Host != "" { @@ -128,17 +128,14 @@ func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection st where = "WHERE " + strings.Join(wq, " AND ") } - as := []string{"last_checked", "uptime"} - sortBy = "last_checked" - if slices.Contains(as, q.SortBy) { - sortBy = q.SortBy + if !slices.Contains([]string{"last_checked", "uptime"}, q.SortBy) { + q.SortBy = "last_checked" } - sortDirection = "DESC" - if q.SortDirection == "asc" { - sortDirection = "ASC" + if q.SortDirection != "asc" { + q.SortDirection = "DESC" } - return args, where, sortBy, sortDirection + return args, where } // Nodes represents a list of nodes @@ -150,7 +147,7 @@ type Nodes struct { // Get nodes from database func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) { - args, where, sortBy, sortDirection := q.toSQL() + args, where := q.toSQL() var nodes Nodes @@ -174,12 +171,12 @@ func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) { * FROM tbl_node - %s -- where query if any + %s ORDER BY %s %s LIMIT ? - OFFSET ?`, where, sortBy, sortDirection) + OFFSET ?`, where, q.SortBy, q.SortDirection) err = r.db.Select(&nodes.Items, query, args...) return nodes, err diff --git a/internal/monero/monero_test.go b/internal/monero/monero_test.go index d47ebb0..b9cddf5 100644 --- a/internal/monero/monero_test.go +++ b/internal/monero/monero_test.go @@ -87,18 +87,18 @@ func TestQueryNodes_toSQL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotArgs, gotWhere, gotSortBy, gotSortDirection := tt.query.toSQL() + gotArgs, gotWhere := tt.query.toSQL() if !equalArgs(gotArgs, tt.wantArgs) { t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs) } if gotWhere != tt.wantWhere { t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere) } - if gotSortBy != tt.wantSortBy { - t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy) + if tt.query.SortBy != tt.wantSortBy { + t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", tt.query.SortBy, tt.wantSortBy) } - if gotSortDirection != tt.wantSortDirection { - t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", gotSortDirection, tt.wantSortDirection) + if tt.query.SortDirection != tt.wantSortDirection { + t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", tt.query.SortDirection, tt.wantSortDirection) } }) } @@ -107,19 +107,20 @@ func TestQueryNodes_toSQL(t *testing.T) { // Single bench test: // go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v func Benchmark_QueryNodes_toSQL(b *testing.B) { + q := QueryNodes{ + Host: "test", + Nettype: "any", + Protocol: "any", + CC: "any", + Status: -1, + CORS: -1, + RowsPerPage: 10, + Page: 1, + SortBy: "last_checked", + SortDirection: "desc", + } for i := 0; i < b.N; i++ { - _, _, _, _ = QueryNodes{ - Host: "test", - Nettype: "any", - Protocol: "any", - CC: "any", - Status: -1, - CORS: -1, - RowsPerPage: 10, - Page: 1, - SortBy: "last_checked", - SortDirection: "desc", - }.toSQL() + _, _ = q.toSQL() } }