-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.go
executable file
·41 lines (34 loc) · 1.05 KB
/
sorting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package templates
var Sorting = `package gen
import (
"context"
"gorm.io/gorm"
)
{{range $obj := .Model.ObjectEntities}}
{{if not $obj.IsExtended}}
func (s {{$obj.Name}}SortType) Apply(ctx context.Context, sorts *[]string, joins *[]string) error {
return s.ApplyWithAlias(ctx, TableName("{{$obj.TableName}}"), sorts, joins)
}
func (s {{$obj.Name}}SortType) ApplyWithAlias(ctx context.Context, alias string, sorts *[]string, joins *[]string) error {
aliasPrefix := alias + "."
{{range $col := $obj.Columns}}{{if $col.IsSortable}}
if s.{{$col.MethodName}} != nil {
*sorts = append(*sorts, aliasPrefix+SnakeString("{{$col.Name}}")+" "+s.{{$col.MethodName}}.String())
}
{{end}}{{end}}
{{range $rel := $obj.Relationships}}
{{if not $rel.Target.IsExtended}}
{{$varName := (printf "s.%s" $rel.MethodName)}}
if {{$varName}} != nil {
_alias := alias + "_{{$rel.Name}}"
*joins = append(*joins, {{$rel.JoinString}})
err := {{$varName}}.ApplyWithAlias(ctx, _alias, sorts, joins)
if err != nil {
return err
}
}{{end}}{{end}}
return nil
}
{{end}}
{{end}}
`