added support to filter request.user.profile relation fields

This commit is contained in:
Gani Georgiev
2022-07-20 22:33:24 +03:00
parent 8a08a4764d
commit 1a5180d7d3
6 changed files with 260 additions and 106 deletions
+13 -1
View File
@@ -37,6 +37,7 @@ type Provider struct {
query *dbx.SelectQuery
page int
perPage int
countColumn string
sort []SortField
filter []FilterData
}
@@ -67,6 +68,13 @@ func (s *Provider) Query(query *dbx.SelectQuery) *Provider {
return s
}
// CountColumn specifies an optional distinct column to use in the
// SELECT COUNT query.
func (s *Provider) CountColumn(countColumn string) *Provider {
s.countColumn = countColumn
return s
}
// Page sets the `page` field of the current search provider.
//
// Normalization on the `page` value is done during `Exec()`.
@@ -190,7 +198,11 @@ func (s *Provider) Exec(items any) (*Result, error) {
// count
var totalCount int64
countQuery := modelsQuery
if err := countQuery.Select("count(*)").Row(&totalCount); err != nil {
countQuery.Distinct(false).Select("COUNT(*)")
if s.countColumn != "" {
countQuery.Select("COUNT(DISTINCT(" + s.countColumn + "))")
}
if err := countQuery.Row(&totalCount); err != nil {
return nil, err
}