Skip to content

Commit

Permalink
add post validator & post hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Oct 7, 2019
1 parent c947ae3 commit ee4caf6
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 30 deletions.
3 changes: 2 additions & 1 deletion adapter/beego/beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -106,7 +107,7 @@ func (bee *Beego) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Request.URL.Path, ctx.Request.Method) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/buffalo/buffalo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -152,7 +153,7 @@ func (bu *Buffalo) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Request().URL.Path, ctx.Request().Method) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/chi/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
cfg "github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -158,7 +159,7 @@ func (bu *Chi) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Request.URL.Path, ctx.Request.Method) {
alert := template.Get(config.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(config.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (e *Echo) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Path(), ctx.Request().Method) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -172,7 +173,7 @@ func (fast *Fasthttp) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, string(ctx.Path()), string(ctx.Method())) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -111,7 +112,7 @@ func (gins *Gin) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Request.URL.Path, ctx.Request.Method) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gorilla
import (
"bytes"
"errors"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/system"
"github.com/chenhg5/go-admin/plugins/admin/modules/constant"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (g *Gorilla) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Request.RequestURI, ctx.Request.Method) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("Permission Denied")).GetContent()

panel = types.Panel{
Expand Down
3 changes: 2 additions & 1 deletion adapter/iris/iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chenhg5/go-admin/engine"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/logger"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/modules/system"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (is *Iris) Content(contextInterface interface{}, c types.GetPanel) {
var panel types.Panel

if !auth.CheckPermissions(user, ctx.Path(), ctx.Method()) {
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
alert := template.Get(globalConfig.Theme).Alert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template2.HTML("no permission")).GetContent()

panel = types.Panel{
Expand Down
4 changes: 2 additions & 2 deletions admincli/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GOCMD=go
GOBUILD=$(GOCMD) build
BINARY_NAME=admincli
LASTVERSION=v0.4.1
VERSION=v0.4.2
LASTVERSION=v0.4.2
VERSION=v0.5.0

all:
GO111MODULE=on $(GOBUILD) -o ./build/mac/$(BINARY_NAME)
Expand Down
5 changes: 5 additions & 0 deletions examples/datamodel/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datamodel
import (
"fmt"
"github.com/chenhg5/go-admin/modules/db"
form2 "github.com/chenhg5/go-admin/plugins/admin/modules/form"
"github.com/chenhg5/go-admin/plugins/admin/modules/table"
"github.com/chenhg5/go-admin/template/types"
"github.com/chenhg5/go-admin/template/types/form"
Expand Down Expand Up @@ -227,5 +228,9 @@ func GetUserTable() (userTable table.Table) {
userTable.GetForm().Title = "Users"
userTable.GetForm().Description = "Users"

userTable.GetForm().PostHook = func(values form2.Values) {
fmt.Println("userTable.GetForm().PostHook", values)
}

return
}
2 changes: 1 addition & 1 deletion modules/system/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package system

const Version = "v0.4.3"
const Version = "v0.5.0"
11 changes: 10 additions & 1 deletion plugins/admin/controller/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/plugins/admin/modules"
"github.com/chenhg5/go-admin/plugins/admin/modules/constant"
Expand Down Expand Up @@ -66,7 +67,15 @@ func EditForm(ctx *context.Context) {
} else if param.IsRole() { // role edit
editRole(param.Value())
} else {
param.Panel.UpdateDataFromDatabase(param.Value())
err := param.Panel.UpdateDataFromDatabase(param.Value())
if err != nil {
alert := aAlert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").
SetContent(template2.HTML(err.Error())).
GetContent()
showForm(ctx, alert, param.Panel, param.Id, param.GetUrl(), param.GetInfoUrl())
return
}
}

table.RefreshTableList()
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ShowEditMenu(ctx *context.Context) {

if ctx.Query("id") == "" {
getMenuInfoPanel(ctx, template.Get(config.Theme).Alert().
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> `+language.Get("error")+`!`)).
SetTheme("warning").
SetContent(template2.HTML("wrong id")).
GetContent())
Expand Down
11 changes: 10 additions & 1 deletion plugins/admin/controller/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/plugins/admin/modules"
"github.com/chenhg5/go-admin/plugins/admin/modules/constant"
Expand Down Expand Up @@ -71,7 +72,15 @@ func NewForm(ctx *context.Context) {
} else if param.IsRole() { // role edit
newRole(param.Value())
} else {
param.Panel.InsertDataFromDatabase(param.Value())
err := param.Panel.InsertDataFromDatabase(param.Value())
if err != nil {
alert := aAlert().SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").
SetContent(template2.HTML(err.Error())).
GetContent()
showForm(ctx, alert, param.Panel, param.Id, param.GetUrl(), param.GetInfoUrl())
return
}
}

editUrl := modules.AorB(param.Panel.GetEditable(), param.GetEditUrl(), "")
Expand Down
4 changes: 4 additions & 0 deletions plugins/admin/modules/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ func (f Values) Get(key string) string {
return ""
}

func (f Values) Add(key string, value string) {
f[key] = []string{value}
}

func (f Values) IsEmpty(key ...string) bool {
for _, k := range key {
if f.Get(k) == "" {
Expand Down
3 changes: 2 additions & 1 deletion plugins/admin/modules/guard/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/plugins/admin/modules/form"
"github.com/chenhg5/go-admin/plugins/admin/modules/parameter"
"github.com/chenhg5/go-admin/plugins/admin/modules/response"
Expand Down Expand Up @@ -175,7 +176,7 @@ func alertWithTitleAndDesc(ctx *context.Context, title, desc, msg string) {

func getAlert(msg string) template2.HTML {
return template.Get(config.Get().Theme).Alert().
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").
SetContent(template2.HTML(msg)).
GetContent()
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/modules/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Alert(ctx *context.Context, config config.Config, desc, title, msg string)
user := auth.Auth(ctx)

alert := template.Get(config.Theme).Alert().
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> Error!`)).
SetTitle(template2.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").
SetContent(template2.HTML(msg)).
GetContent()
Expand Down
48 changes: 42 additions & 6 deletions plugins/admin/modules/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type Table interface {
GetDataFromDatabase(path string, params parameter.Parameters) PanelInfo
GetDataFromDatabaseWithIds(path string, params parameter.Parameters, ids []string) PanelInfo
GetDataFromDatabaseWithId(id string) ([]types.Form, [][]types.Form, []string, string, string)
UpdateDataFromDatabase(dataList form.Values)
InsertDataFromDatabase(dataList form.Values)
UpdateDataFromDatabase(dataList form.Values) error
InsertDataFromDatabase(dataList form.Values) error
DeleteDataFromDatabase(id string)
}

Expand Down Expand Up @@ -779,15 +779,51 @@ func (tb DefaultTable) GetDataFromDatabaseWithId(id string) ([]types.Form, [][]t
}

// UpdateDataFromDatabase update data.
func (tb DefaultTable) UpdateDataFromDatabase(dataList form.Values) {
_, _ = tb.sql().Table(tb.form.Table).
func (tb DefaultTable) UpdateDataFromDatabase(dataList form.Values) error {

if tb.form.PostValidator != nil {
if err := tb.form.PostValidator(dataList); err != nil {
return err
}
}

_, err := tb.sql().Table(tb.form.Table).
Where(tb.primaryKey.Name, "=", dataList.Get(tb.primaryKey.Name)).
Update(tb.getValues(dataList))

if err != nil {
return err
}

if tb.form.PostHook != nil {
go tb.form.PostHook(dataList)
}

return nil
}

// InsertDataFromDatabase insert data.
func (tb DefaultTable) InsertDataFromDatabase(dataList form.Values) {
_, _ = tb.sql().Table(tb.form.Table).Insert(tb.getValues(dataList))
func (tb DefaultTable) InsertDataFromDatabase(dataList form.Values) error {

if tb.form.PostValidator != nil {
if err := tb.form.PostValidator(dataList); err != nil {
return err
}
}

id, err := tb.sql().Table(tb.form.Table).Insert(tb.getValues(dataList))

if err != nil {
return err
}

dataList.Add(tb.GetPrimaryKey().Name, strconv.Itoa(int(id)))

if tb.form.PostHook != nil {
go tb.form.PostHook(dataList)
}

return nil
}

func (tb DefaultTable) getValues(dataList form.Values) dialect.H {
Expand Down
23 changes: 15 additions & 8 deletions template/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/plugins/admin/models"
"github.com/chenhg5/go-admin/plugins/admin/modules"
form2 "github.com/chenhg5/go-admin/plugins/admin/modules/form"
"github.com/chenhg5/go-admin/template/types/form"
"html/template"
)
Expand Down Expand Up @@ -165,16 +166,22 @@ const (

// FormPanel
type FormPanel struct {
FormList FormList
Group [][]string
GroupHeaders []string
Table string
Title string
Description string
HeaderHtml template.HTML
FooterHtml template.HTML
FormList FormList
Group [][]string
GroupHeaders []string
Table string
Title string
Description string
PostValidator PostValidator
PostHook PostHookFn
HeaderHtml template.HTML
FooterHtml template.HTML
}

type PostValidator func(values form2.Values) error

type PostHookFn func(values form2.Values)

type FormList []Form

func (f FormList) Copy() FormList {
Expand Down

0 comments on commit ee4caf6

Please sign in to comment.