Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"gitea.dev/models/db"
|
|
"gitea.dev/services/context"
|
|
)
|
|
|
|
// ResolveSortOrder reads "sort" and "order" query params and returns the matching
|
|
// SearchOrderBy from orderByMap. When "sort" is absent, returns defaultOrder.
|
|
// On invalid input it writes a 422 response and returns ok=false; callers should
|
|
// then return immediately.
|
|
func ResolveSortOrder(ctx *context.APIContext, orderByMap map[string]map[string]db.SearchOrderBy, defaultOrder db.SearchOrderBy) (db.SearchOrderBy, bool) {
|
|
sortMode := ctx.FormString("sort")
|
|
if sortMode == "" {
|
|
return defaultOrder, true
|
|
}
|
|
sortOrder := ctx.FormString("order")
|
|
if sortOrder == "" {
|
|
sortOrder = "asc"
|
|
}
|
|
orderMap, ok := orderByMap[sortOrder]
|
|
if !ok {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort order: %q", sortOrder))
|
|
return "", false
|
|
}
|
|
orderBy, ok := orderMap[sortMode]
|
|
if !ok {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort mode: %q", sortMode))
|
|
return "", false
|
|
}
|
|
return orderBy, true
|
|
}
|