Skip to content

Commit

Permalink
feat(admin): add admin plugin crud json apis(untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Apr 9, 2020
1 parent 51aca4c commit 296695e
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 132 deletions.
6 changes: 5 additions & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,14 @@ func (ctx *Context) PostForm() url.Values {
return ctx.Request.PostForm
}

func (ctx *Context) WantsHTML() bool {
func (ctx *Context) WantHTML() bool {
return ctx.Method() == "GET" && strings.Contains(ctx.Headers("Accept"), "html")
}

func (ctx *Context) WantJSON() bool {
return strings.Contains(ctx.Headers("Accept"), "json")
}

// AddHeader adds the key, value pair to the header.
func (ctx *Context) AddHeader(key, value string) {
ctx.Response.Header.Add(key, value)
Expand Down
7 changes: 7 additions & 0 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ type Config struct {

// Favicon string `json:"favicon",yaml:"favicon",ini:"favicon"`

// Is open admin plugin json api
OpenAdminApi bool `json:"open_admin_api",yaml:"open_admin_api",ini:"open_admin_api"`

prefix string
}

Expand Down Expand Up @@ -751,6 +754,10 @@ func GetUrlPrefix() string {
return globalCfg.UrlPrefix
}

func GetOpenAdminApi() bool {
return globalCfg.OpenAdminApi
}

func GetTheme() string {
return globalCfg.Theme
}
Expand Down
65 changes: 65 additions & 0 deletions plugins/admin/controller/api_create.go
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),
})
}
130 changes: 130 additions & 0 deletions plugins/admin/controller/api_detail.go
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(),
})
}
39 changes: 39 additions & 0 deletions plugins/admin/controller/api_list.go
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],
},
})
}
86 changes: 86 additions & 0 deletions plugins/admin/controller/api_update.go
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),
})
}
Loading

0 comments on commit 296695e

Please sign in to comment.