-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
65 lines (57 loc) · 1.79 KB
/
util.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package controller
import (
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
)
const (
incDel = "cutil.QueryStringBool(rc, \"includeDeleted\")"
incFalse = ", false"
)
func checkRev(ret *golang.Block, m *model.Model) {
if !m.IsRevision() {
return
}
hc := m.HistoryColumn()
incDel := ""
if m.IsSoftDelete() {
incDel = incFalse
}
prmsStr := m.PKs().ToRefs("ret.")
ret.W("\t\tprms := ps.Params.Get(%q, nil, ps.Logger).Sanitize(%q)", m.Package, m.Package)
const msg = "\t\t%s, err := as.Services.%s.GetAll%s(ps.Context, nil, %s, prms%s, ps.Logger)"
ret.W(msg, hc.CamelPlural(), m.Proper(), hc.ProperPlural(), prmsStr, incDel)
ret.WE(2, `""`)
}
func checkGrp(ret *golang.Block, grp *model.Column, override ...string) {
if grp == nil {
return
}
x := "ret"
if len(override) > 0 {
x = override[0]
}
ret.W("\t\tif %s.%s != %sArg {", x, grp.Proper(), grp.Camel())
ret.W("\t\t\treturn \"\", errors.New(\"unauthorized: incorrect [%s]\")", grp.Camel())
ret.W("\t\t}")
}
func controllerModelFromPath(m *model.Model) *golang.Block {
ret := golang.NewBlock(m.Proper()+"FromPath", "func")
ret.W("func %sFromPath(rc *fasthttp.RequestCtx, as *app.State, ps *cutil.PageState) (*%s, error) {", m.Package, m.ClassRef())
pks := m.PKs()
lo.ForEach(pks, func(col *model.Column, _ int) {
controllerArgFor(col, ret, "nil", 1)
})
args := lo.Map(pks, func(x *model.Column, _ int) string {
return x.Camel() + "Arg"
})
suffix := ""
if m.IsSoftDelete() {
suffix = ", includeDeleted"
ret.W("\tincludeDeleted := rc.UserValue(\"includeDeleted\") != nil || " + incDel)
}
ret.W("\treturn as.Services.%s.Get(ps.Context, nil, %s%s, ps.Logger)", m.Proper(), strings.Join(args, ", "), suffix)
ret.W("}")
return ret
}