Enhance company and company category search forms by adding new sortable fields (email, phone, country, state, language, currency) and updating related API documentation.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
@@ -189,6 +190,10 @@ func documentSortValue(docMap bson.M, sortField string) (interface{}, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
if strings.Contains(sortField, ".") {
|
||||
return nestedDocumentValue(docMap, sortField)
|
||||
}
|
||||
|
||||
sortValue, exists := docMap[sortField]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("sort field %s not found in document", sortField)
|
||||
@@ -196,6 +201,30 @@ func documentSortValue(docMap bson.M, sortField string) (interface{}, error) {
|
||||
return sortValue, nil
|
||||
}
|
||||
|
||||
func nestedDocumentValue(docMap bson.M, path string) (interface{}, error) {
|
||||
parts := strings.Split(path, ".")
|
||||
var current interface{} = docMap
|
||||
|
||||
for _, part := range parts {
|
||||
nested, ok := current.(bson.M)
|
||||
if !ok {
|
||||
if nestedMap, ok := current.(map[string]interface{}); ok {
|
||||
nested = bson.M(nestedMap)
|
||||
} else {
|
||||
return nil, fmt.Errorf("sort field %s not found in document", path)
|
||||
}
|
||||
}
|
||||
|
||||
value, exists := nested[part]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("sort field %s not found in document", path)
|
||||
}
|
||||
current = value
|
||||
}
|
||||
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func documentIDHex(docMap bson.M) (string, error) {
|
||||
id, exists := docMap["_id"]
|
||||
if !exists {
|
||||
|
||||
Reference in New Issue
Block a user