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

add orderBy param #324

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package http

import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -160,3 +161,58 @@ func readSkipCount(r *http.Request, skipMax, countMax int) (skip int, count int,

return skip, count, exists, nil
}

type Direction string

const (
Ascending Direction = "ASC"
Descending Direction = "DESC"
)

type OrderBy struct {
Name string
Direction Direction
}

// GetOrderBy returns the field names and direction to order the response by
func GetOrderBy(r *http.Request) ([]OrderBy, error) {
orderByParam := r.URL.Query().Get("orderBy")
if orderByParam == "" {
return []OrderBy{}, nil
}

paramSplit := strings.Split(orderByParam, ",")
var orderBys []OrderBy
for _, split := range paramSplit {
orderBy := strings.Split(split, ":")
if len(orderBy) != 2 {
return nil, fmt.Errorf("invalid orderBy: %s", orderBy)
}

name := strings.TrimSpace(orderBy[0])
if name == "" {
return nil, errors.New("missing orderBy name")
}

directionStr := strings.TrimSpace(orderBy[1])
if directionStr == "" {
return nil, errors.New("missing orderBy direction")
}
directionStr = strings.ToLower(directionStr)

var direction Direction
if strings.HasPrefix(directionStr, "asc") {
direction = Ascending
} else if strings.HasPrefix(directionStr, "desc") {
direction = Descending
} else {
return nil, fmt.Errorf("invalid orderBy direction: %s", direction)
}

orderBys = append(orderBys, OrderBy{
Name: name,
Direction: direction,
})
}
return orderBys, nil
}
135 changes: 135 additions & 0 deletions http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,138 @@ func TestLimitedSkipCount(t *testing.T) {
require.Equal(t, 250, skip)
require.Equal(t, 100, count)
}

func TestGetOrderBy(t *testing.T) {
tests := []struct {
name string
path string
expected []OrderBy
wantErr bool
}{
{
name: "valid - missing",
path: "/list",
expected: []OrderBy{},
wantErr: false,
},
{
name: "valid - empty",
path: "/list?orderBy=",
expected: []OrderBy{},
wantErr: false,
},
{
name: "valid - single",
path: "/list?orderBy=createdOn:ascending",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Ascending,
},
},
wantErr: false,
},
{
name: "valid - multiple",
path: "/list?orderBy=createdOn:ascending,updatedOn:descending",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Ascending,
},
{
Name: "updatedOn",
Direction: Descending,
},
},
wantErr: false,
},
{
name: "valid - short name asc",
path: "/list?orderBy=createdOn:asc",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Ascending,
},
},
wantErr: false,
},
{
name: "valid - short name desc",
path: "/list?orderBy=createdOn:desc",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Descending,
},
},
wantErr: false,
},
{
name: "valid - mixed cases ascending",
path: "/list?orderBy=createdOn:AsCeNdInG",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Ascending,
},
},
},
{
name: "valid - mixed cases descending",
path: "/list?orderBy=createdOn:DeScEnDiNg",
expected: []OrderBy{
{
Name: "createdOn",
Direction: Descending,
},
},
},
{
name: "invalid - missing colon",
path: "/list?orderBy=createdOndescending",
expected: nil,
wantErr: true,
},
{
name: "invalid - missing name",
path: "/list?orderBy=:ascending",
expected: nil,
wantErr: true,
},
{
name: "invalid - missing direction",
path: "/list?orderBy=createdOn:",
expected: nil,
wantErr: true,
},
{
name: "invalid - empty name",
path: "/list?orderBy=%20%20:ascending",
expected: nil,
wantErr: true,
},
{
name: "invalid - empty direction",
path: "/list?orderBy=createdOn:%20%20",
expected: nil,
wantErr: true,
},
{
name: "invalid - invalid direction",
path: "/list?orderBy=createdOn:invalid",
expected: nil,
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := httptest.NewRequest("GET", test.path, nil)
orderBy, err := GetOrderBy(r)
require.Equal(t, test.wantErr, err != nil)
require.Equal(t, test.expected, orderBy)
})
}
}
Loading