mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
fix(core): default to _id descending sort when sort is nil or parsing fails
Ensure MustGetSortOption returns a default bson.D{{"_id", -1}} on parse errors and
GetPaginationPipeline uses {_id: -1} when sort is nil to provide consistent default ordering.
This commit is contained in:
@@ -259,7 +259,8 @@ func GetSortsOption(c *gin.Context) (sort bson.D, err error) {
|
||||
func MustGetSortOption(c *gin.Context) (sort bson.D) {
|
||||
sort, err := GetSortsOption(c)
|
||||
if err != nil {
|
||||
return nil
|
||||
// Return default sort by _id descending when parsing fails
|
||||
return bson.D{{"_id", -1}}
|
||||
}
|
||||
return sort
|
||||
}
|
||||
|
||||
@@ -491,6 +491,9 @@ func GetPaginationPipeline(query bson.M, sort bson.D, skip, limit int) []bson.D
|
||||
if query == nil {
|
||||
query = bson.M{}
|
||||
}
|
||||
if sort == nil {
|
||||
sort = bson.D{{"_id", -1}}
|
||||
}
|
||||
return []bson.D{
|
||||
{{Key: "$match", Value: query}},
|
||||
{{Key: "$sort", Value: sort}},
|
||||
|
||||
Reference in New Issue
Block a user