forked from GoAdminGroup/go-admin
/
api_create.go
62 lines (53 loc) · 1.76 KB
/
api_create.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
package controller
import (
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/file"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/guard"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/response"
)
func (h *Handler) ApiCreate(ctx *context.Context) {
param := guard.GetNewFormParam(ctx)
if len(param.MultiForm.File) > 0 {
err := file.GetFileEngine(h.config.FileUploadEngine.Name).Upload(param.MultiForm)
if err != nil {
response.Error(ctx, err.Error())
return
}
}
err := param.Panel.InsertData(param.Value())
if err != nil {
response.Error(ctx, err.Error())
return
}
response.Ok(ctx)
}
func (h *Handler) ApiCreateForm(ctx *context.Context) {
var (
params = guard.GetShowNewFormParam(ctx)
prefix, paramStr = params.Prefix, params.Param.GetRouteParamStr()
panel = h.table(prefix, ctx)
formInfo = panel.GetNewFormInfo()
infoUrl = h.routePathWithPrefix("api_info", prefix) + paramStr
newUrl = h.routePathWithPrefix("api_new", prefix)
referer = ctx.Referer()
f = panel.GetActualNewForm()
)
if referer != "" && !isInfoUrl(referer) && !isNewUrl(referer, ctx.Query(constant.PrefixKey)) {
infoUrl = referer
}
response.OkWithData(ctx, map[string]interface{}{
"panel": formInfo,
"urls": map[string]string{
"info": infoUrl,
"new": newUrl,
},
"pk": panel.GetPrimaryKey().Name,
"header": f.HeaderHtml,
"footer": f.FooterHtml,
"prefix": h.config.PrefixFixSlash(),
"token": h.authSrv().AddToken(),
"operation_footer": formFooter("new", f.IsHideContinueEditCheckBox, f.IsHideContinueNewCheckBox,
f.IsHideResetButton, f.FormNewBtnWord),
})
}