forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): add admin plugin crud json apis(untested)
- Loading branch information
cg33
committed
Apr 9, 2020
1 parent
51aca4c
commit 296695e
Showing
18 changed files
with
463 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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) { | ||
params := guard.GetShowNewFormParam(ctx) | ||
|
||
prefix, paramStr := params.Prefix, params.Param.GetRouteParamStr() | ||
|
||
panel := h.table(prefix, ctx) | ||
|
||
formInfo := panel.GetNewForm() | ||
|
||
infoUrl := h.routePathWithPrefix("api_info", prefix) + paramStr | ||
newUrl := h.routePathWithPrefix("api_new", prefix) | ||
|
||
referer := ctx.Headers("Referer") | ||
|
||
if referer != "" && !isInfoUrl(referer) && !isNewUrl(referer, ctx.Query(constant.PrefixKey)) { | ||
infoUrl = referer | ||
} | ||
|
||
f := panel.GetForm() | ||
|
||
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), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"github.com/GoAdminGroup/go-admin/context" | ||
"github.com/GoAdminGroup/go-admin/modules/auth" | ||
"github.com/GoAdminGroup/go-admin/modules/language" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/parameter" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/response" | ||
"github.com/GoAdminGroup/go-admin/template/types" | ||
"github.com/GoAdminGroup/go-admin/template/types/form" | ||
) | ||
|
||
func (h *Handler) ApiDetail(ctx *context.Context) { | ||
prefix := ctx.Query(constant.PrefixKey) | ||
id := ctx.Query(constant.DetailPKKey) | ||
panel := h.table(prefix, ctx) | ||
user := auth.Auth(ctx) | ||
|
||
newPanel := panel.Copy() | ||
|
||
formModel := newPanel.GetForm() | ||
|
||
var fieldList types.FieldList | ||
|
||
if len(panel.GetDetail().FieldList) == 0 { | ||
fieldList = panel.GetInfo().FieldList | ||
} else { | ||
fieldList = panel.GetDetail().FieldList | ||
} | ||
|
||
formModel.FieldList = make([]types.FormField, len(fieldList)) | ||
|
||
for i, field := range fieldList { | ||
formModel.FieldList[i] = types.FormField{ | ||
Field: field.Field, | ||
TypeName: field.TypeName, | ||
Head: field.Head, | ||
Hide: field.Hide, | ||
Join: field.Join, | ||
FormType: form.Default, | ||
FieldDisplay: field.FieldDisplay, | ||
} | ||
} | ||
|
||
param := parameter.GetParam(ctx.Request.URL, | ||
panel.GetInfo().DefaultPageSize, | ||
panel.GetInfo().SortField, | ||
panel.GetInfo().GetSort()) | ||
|
||
paramStr := param.DeleteDetailPk().GetRouteParamStr() | ||
|
||
editUrl := modules.AorEmpty(panel.GetEditable(), h.routePathWithPrefix("show_edit", prefix)+paramStr+ | ||
"&"+constant.EditPKKey+"="+ctx.Query(constant.DetailPKKey)) | ||
deleteUrl := modules.AorEmpty(panel.GetDeletable(), h.routePathWithPrefix("delete", prefix)+paramStr) | ||
infoUrl := h.routePathWithPrefix("info", prefix) + paramStr | ||
|
||
editUrl = user.GetCheckPermissionByUrlMethod(editUrl, h.route("show_edit").Method()) | ||
deleteUrl = user.GetCheckPermissionByUrlMethod(deleteUrl, h.route("delete").Method()) | ||
|
||
deleteJs := "" | ||
|
||
if deleteUrl != "" { | ||
deleteJs = fmt.Sprintf(`<script> | ||
function DeletePost(id) { | ||
swal({ | ||
title: '%s', | ||
type: "warning", | ||
showCancelButton: true, | ||
confirmButtonColor: "#DD6B55", | ||
confirmButtonText: '%s', | ||
closeOnConfirm: false, | ||
cancelButtonText: '%s', | ||
}, | ||
function () { | ||
$.ajax({ | ||
method: 'post', | ||
url: '%s', | ||
data: { | ||
id: id | ||
}, | ||
success: function (data) { | ||
if (typeof (data) === "string") { | ||
data = JSON.parse(data); | ||
} | ||
if (data.code === 200) { | ||
location.href = '%s' | ||
} else { | ||
swal(data.msg, '', 'error'); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
$('.delete-btn').on('click', function (event) { | ||
DeletePost(%s) | ||
}); | ||
</script>`, language.Get("are you sure to delete"), language.Get("yes"), language.Get("cancel"), deleteUrl, infoUrl, id) | ||
} | ||
|
||
title := panel.GetDetail().Title | ||
|
||
if title == "" { | ||
title = panel.GetInfo().Title + language.Get("Detail") | ||
} | ||
|
||
desc := panel.GetDetail().Description | ||
|
||
if desc == "" { | ||
desc = panel.GetInfo().Description + language.Get("Detail") | ||
} | ||
|
||
formInfo, err := newPanel.GetDataWithId(param.WithPKs(id)) | ||
|
||
if err != nil { | ||
response.Error(ctx, err.Error()) | ||
return | ||
} | ||
|
||
response.OkWithData(ctx, map[string]interface{}{ | ||
"panel": formInfo, | ||
"previous": infoUrl, | ||
"footer": deleteJs, | ||
"prefix": h.config.PrefixFixSlash(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/GoAdminGroup/go-admin/context" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/parameter" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/response" | ||
) | ||
|
||
func (h *Handler) ApiList(ctx *context.Context) { | ||
prefix := ctx.Query(constant.PrefixKey) | ||
|
||
panel := h.table(prefix, ctx) | ||
|
||
params := parameter.GetParam(ctx.Request.URL, panel.GetInfo().DefaultPageSize, panel.GetInfo().SortField, | ||
panel.GetInfo().GetSort()) | ||
|
||
panel, panelInfo, urls, err := h.showTableData(ctx, prefix, params, panel, "api_") | ||
if err != nil { | ||
response.Error(ctx, err.Error()) | ||
return | ||
} | ||
|
||
response.OkWithData(ctx, map[string]interface{}{ | ||
"panel": panelInfo, | ||
"footer": panelInfo.Paginator.GetContent() + panel.GetInfo().FooterHtml, | ||
"header": aDataTable().GetDataTableHeader() + panel.GetInfo().HeaderHtml, | ||
"prefix": h.config.PrefixFixSlash(), | ||
"urls": map[string]string{ | ||
"edit": urls[0], | ||
"new": urls[1], | ||
"delete": urls[2], | ||
"export": urls[3], | ||
"detail": urls[4], | ||
"info": urls[5], | ||
"update": urls[6], | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/GoAdminGroup/go-admin/context" | ||
"github.com/GoAdminGroup/go-admin/modules/auth" | ||
"github.com/GoAdminGroup/go-admin/modules/file" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/modules" | ||
"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" | ||
"github.com/GoAdminGroup/go-admin/template/types/form" | ||
"net/url" | ||
) | ||
|
||
func (h *Handler) ApiUpdate(ctx *context.Context) { | ||
param := guard.GetEditFormParam(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 | ||
} | ||
} | ||
|
||
for _, field := range param.Panel.GetForm().FieldList { | ||
if field.FormType == form.File && | ||
len(param.MultiForm.File[field.Field]) == 0 && | ||
param.MultiForm.Value[field.Field+"__delete_flag"][0] != "1" { | ||
delete(param.MultiForm.Value, field.Field) | ||
} | ||
} | ||
|
||
err := param.Panel.UpdateData(param.Value()) | ||
if err != nil { | ||
response.Error(ctx, err.Error()) | ||
return | ||
} | ||
|
||
response.Ok(ctx) | ||
} | ||
|
||
func (h *Handler) ApiUpdateForm(ctx *context.Context) { | ||
params := guard.GetShowFormParam(ctx) | ||
|
||
prefix, param := params.Prefix, params.Param | ||
|
||
panel := h.table(prefix, ctx) | ||
|
||
user := auth.Auth(ctx) | ||
|
||
paramStr := param.GetRouteParamStr() | ||
|
||
newUrl := modules.AorEmpty(panel.GetCanAdd(), h.routePathWithPrefix("api_show_new", prefix)+paramStr) | ||
footerKind := "edit" | ||
if newUrl == "" || !user.CheckPermissionByUrlMethod(newUrl, h.route("api_show_new").Method(), url.Values{}) { | ||
footerKind = "edit_only" | ||
} | ||
|
||
formInfo, err := panel.GetDataWithId(param) | ||
|
||
if err != nil { | ||
response.Error(ctx, err.Error()) | ||
return | ||
} | ||
|
||
infoUrl := h.routePathWithPrefix("api_info", prefix) + param.DeleteField(constant.EditPKKey).GetRouteParamStr() | ||
editUrl := h.routePathWithPrefix("api_edit", prefix) | ||
|
||
f := panel.GetForm() | ||
|
||
response.OkWithData(ctx, map[string]interface{}{ | ||
"panel": formInfo, | ||
"urls": map[string]string{ | ||
"info": infoUrl, | ||
"edit": editUrl, | ||
}, | ||
"pk": panel.GetPrimaryKey().Name, | ||
"header": f.HeaderHtml, | ||
"footer": f.FooterHtml, | ||
"prefix": h.config.PrefixFixSlash(), | ||
"token": h.authSrv().AddToken(), | ||
"operation_footer": formFooter(footerKind, f.IsHideContinueEditCheckBox, f.IsHideContinueNewCheckBox, | ||
f.IsHideResetButton), | ||
}) | ||
} |
Oops, something went wrong.