Skip to content

Commit

Permalink
refactor: change default sort order for /api/teams/all
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Nov 19, 2024
1 parent 8075259 commit 25d3f7b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api/handler/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func teams(router chi.Router) {
middleware.AdminOnlyMiddleware(),
middleware.Paginate(getAllTeamsDefaultPage, getAllTeamsDefaultSize),
middleware.SearchTextContext(regexp.MustCompile(getAllTeamsDefaultRegexTemplate)),
middleware.SortOrderContext(api.NoSortOrder),
middleware.SortOrderContext(api.AscSortOrder),
).Get("/all", getAllTeams)
router.Get("/", getAllTeamsForUser)
router.Post("/", createTeam)
Expand Down Expand Up @@ -147,7 +147,7 @@ func getTeam(writer http.ResponseWriter, request *http.Request) {
// @param size query int false "Number of items to be displayed on one page. if size = -1 then all teams returned" default(-1)
// @param p query int false "Defines the number of the displayed page. E.g, p=2 would display the 2nd page" default(0)
// @param searchText query string false "Regular expression which will be applied to team id or team name than filtering teams" default(.*)
// @param sort query string false "String to set sort order (by name). On empty - no order, asc - ascending, desc - descending" default()
// @param sort query string false "String to set sort order (by name). On empty - no order, asc - ascending, desc - descending" default(asc)
// @success 200 {object} dto.TeamsList "Teams fetched successfully"
// @failure 400 {object} api.ErrorInvalidRequestExample "Bad request from client"
// @failure 403 {object} api.ErrorForbiddenExample "Forbidden"
Expand Down
17 changes: 11 additions & 6 deletions api/middleware/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,19 @@ func SortOrderContext(defaultSortOrder api.SortOrder) func(next http.Handler) ht
return
}

var sortOrder api.SortOrder
queryParamName := "sort"

sortVal := api.SortOrder(urlValues.Get("sort"))
switch sortVal {
case api.NoSortOrder, api.AscSortOrder, api.DescSortOrder:
sortOrder = sortVal
default:
var sortOrder api.SortOrder
if !urlValues.Has(queryParamName) {
sortOrder = defaultSortOrder
} else {
sortVal := api.SortOrder(urlValues.Get(queryParamName))
switch sortVal {
case api.NoSortOrder, api.AscSortOrder, api.DescSortOrder:
sortOrder = sortVal
default:
sortOrder = defaultSortOrder
}
}

ctx := context.WithValue(request.Context(), sortOrderContextKey, sortOrder)
Expand Down
7 changes: 7 additions & 0 deletions api/middleware/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ func TestSortOrderContext(t *testing.T) {
Convey("Checking sort order context", t, func() {
defaultSortOrder := api.NoSortOrder

Convey("with no query parameter", func() {
testRequestOk(
"/test",
SortOrderContext(defaultSortOrder),
nil)
})

Convey("with correct query parameter", func() {
sortOrders := []api.SortOrder{api.NoSortOrder, api.AscSortOrder, api.DescSortOrder, "some"}

Expand Down

0 comments on commit 25d3f7b

Please sign in to comment.