From 25d3f7bcfa78db22d1d3628258f1b79158204ebd Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Tue, 19 Nov 2024 16:11:35 +0700 Subject: [PATCH] refactor: change default sort order for /api/teams/all --- api/handler/team.go | 4 ++-- api/middleware/context.go | 17 +++++++++++------ api/middleware/context_test.go | 7 +++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/api/handler/team.go b/api/handler/team.go index 3dd5d41ce..60584a223 100644 --- a/api/handler/team.go +++ b/api/handler/team.go @@ -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) @@ -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" diff --git a/api/middleware/context.go b/api/middleware/context.go index 00b432ecc..aa5a0fbc3 100644 --- a/api/middleware/context.go +++ b/api/middleware/context.go @@ -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) diff --git a/api/middleware/context_test.go b/api/middleware/context_test.go index 79b23b6f5..325d9d359 100644 --- a/api/middleware/context_test.go +++ b/api/middleware/context_test.go @@ -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"}