mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
"github.com/hanzoai/git/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
|
|
}
|