This repository has been archived on 2025-03-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
boilerplate/internal/utils/sorting.go
gogacoder 10c7a9bac8 feat: sorting for primitive fields
# Conflicts:
#	frontend/src/App.vue
2025-03-12 15:50:10 +07:00

41 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"app/internal/database"
"errors"
"fmt"
"reflect"
)
// SortByOrder Order items by specified field and a sort type
// Example: SortByOrder(map[string]string{"name": "ASC"}, &models.Post{})
// ASC - по возрастанию (от А до Я)
// DESC - по убыванию (от Я до А)
func SortByOrder[T any](fieldsSortOrder map[string]string, entity T) ([]*T, error) {
var (
orderQuery string
items []*T
)
for name, order := range fieldsSortOrder {
structInfo := reflect.ValueOf(entity).Type()
_, fieldExist := structInfo.FieldByName(name)
if !fieldExist {
return nil, errors.New(fmt.Sprintf("Field `%s` not found", name))
}
if order != "ASC" && order != "DESC" {
return nil, errors.New(fmt.Sprintf("Field `%s` can only be sorted by ASC or DESC", name))
}
orderQuery += fmt.Sprintf("%s %s", name, order)
}
result := database.GetInstance().Order(orderQuery).Find(&items)
if result.Error != nil {
return items, result.Error
}
return items, nil
}