Skip to content

Commit

Permalink
fixed cli bugs & show new form bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhg5 committed Oct 6, 2019
1 parent 9f7f771 commit 0135b4c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 40 deletions.
17 changes: 15 additions & 2 deletions admincli/cli.go
Expand Up @@ -123,9 +123,21 @@ func generating() {
)

if driver.Value != "sqlite" {

defaultPort := "3306"
defaultUser := "root"

if driver.Value == "postgresql" {
defaultPort = "5432"
defaultUser = "postgres"
} else if driver.Value == "mssql" {
defaultPort = "1433"
defaultUser = "sa"
}

host := promptWithDefault("sql address", "127.0.0.1")
port := promptWithDefault("sql port", "3306")
user := promptWithDefault("sql username", "root")
port := promptWithDefault("sql port", defaultPort)
user := promptWithDefault("sql username", defaultUser)
password := promptPassword()

name = prompt("sql database name")
Expand Down Expand Up @@ -508,6 +520,7 @@ func getType(typeName string) string {
}

func getLatestVersion() string {
http.DefaultClient.Timeout = time.Duration(time.Second * 3)
res, err := http.Get("https://goproxy.cn/github.com/chenhg5/go-admin/@v/list")

if err != nil || res.Body == nil {
Expand Down
1 change: 0 additions & 1 deletion examples/gin/main.go
Expand Up @@ -47,7 +47,6 @@ func main() {
IndexUrl: "/",
Debug: true,
ColorScheme: adminlte.COLORSCHEME_SKIN_BLACK,
SqlLog: true,
}

adminPlugin := admin.NewAdmin(datamodel.Generators)
Expand Down
2 changes: 1 addition & 1 deletion modules/db/dialect/postgresql.go
Expand Up @@ -9,5 +9,5 @@ func (postgresql) GetName() string {
}

func (postgresql) ShowTables() string {
return "select tablename from pg_catalog.pg_tables"
return "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';"
}
2 changes: 1 addition & 1 deletion modules/system/version.go
@@ -1,3 +1,3 @@
package system

const Version = "v0.2.5"
const Version = "v0.2.6"
3 changes: 0 additions & 3 deletions plugins/admin/controller/edit.go
@@ -1,7 +1,6 @@
package controller

import (
"fmt"
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/menu"
Expand All @@ -24,8 +23,6 @@ func showForm(ctx *context.Context, alert template2.HTML, panel table.Table, id

formData, groupFormData, groupHeaders, title, description := panel.GetDataFromDatabaseWithId(id)

fmt.Println("groupHeaders", groupHeaders)

user := auth.Auth(ctx)

tmpl, tmplName := aTemplate().GetTemplate(isPjax(ctx))
Expand Down
5 changes: 3 additions & 2 deletions plugins/admin/controller/new.go
Expand Up @@ -27,6 +27,7 @@ func showNewForm(ctx *context.Context, alert template2.HTML, panel table.Table,

formList, groupFormList, groupHeaders := table.GetNewFormList(panel.GetForm().GroupHeaders, panel.GetForm().Group,
panel.GetForm().FormList, panel.GetPrimaryKey().Name)

tmpl, tmplName := aTemplate().GetTemplate(isPjax(ctx))
buf := template.Execute(tmpl, tmplName, user, types.Panel{
Content: alert + aForm().
Expand All @@ -52,6 +53,8 @@ func NewForm(ctx *context.Context) {

param := guard.GetNewFormParam(ctx)

table.RefreshTableList()

if param.HasAlert() {
showNewForm(ctx, param.Alert, param.Panel, param.GetUrl(), param.GetInfoUrl())
return
Expand All @@ -70,8 +73,6 @@ func NewForm(ctx *context.Context) {
param.Panel.InsertDataFromDatabase(param.Value())
}

table.RefreshTableList()

panelInfo := param.Panel.GetDataFromDatabase(param.Path, param.Param)

dataTable := aDataTable().
Expand Down
3 changes: 0 additions & 3 deletions plugins/admin/models/user.go
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"github.com/chenhg5/go-admin/modules/db"
"github.com/chenhg5/go-admin/modules/db/dialect"
"strconv"
Expand Down Expand Up @@ -129,8 +128,6 @@ func (t UserModel) New(username, password, name, avatar string) UserModel {
"avatar": avatar,
})

fmt.Println("UserModel new id", id)

t.Id = id
t.UserName = username
t.Password = password
Expand Down
18 changes: 18 additions & 0 deletions plugins/admin/modules/helper.go
@@ -1,6 +1,8 @@
package modules

import (
"bytes"
"encoding/gob"
"github.com/NebulousLabs/fastrand"
"strconv"
)
Expand Down Expand Up @@ -77,3 +79,19 @@ func GetPage(page string) (pageInt int) {
}
return
}

func CopyMap(m map[string]string) map[string]string {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(m)
if err != nil {
panic(err)
}
var cm map[string]string
err = dec.Decode(&cm)
if err != nil {
panic(err)
}
return cm
}
56 changes: 29 additions & 27 deletions plugins/admin/modules/table/table.go
Expand Up @@ -589,9 +589,11 @@ func (tb DefaultTable) GetDataFromDatabaseWithId(id string) ([]types.Form, [][]t
columnsModel, _ := tb.sql().Table(tb.form.Table).ShowColumns()
columns := getColumns(columnsModel, tb.connectionDriver)

formList := tb.form.FormList.Copy()

for i := 0; i < len(tb.form.FormList); i++ {
if checkInTable(columns, tb.form.FormList[i].Field) {
fields = append(fields, tb.form.FormList[i].Field)
if checkInTable(columns, formList[i].Field) {
fields = append(fields, formList[i].Field)
}
}

Expand All @@ -610,47 +612,47 @@ func (tb DefaultTable) GetDataFromDatabaseWithId(id string) ([]types.Form, [][]t
list := make([]types.Form, len(value))
for j := 0; j < len(value); j++ {
for i := 0; i < len(tb.form.FormList); i++ {
if value[j] == tb.form.FormList[i].Field {
if checkInTable(columns, tb.form.FormList[i].Field) {
if tb.form.FormList[i].FormType.IsSelect() {
valueArr := tb.form.FormList[i].FilterFn(types.RowModel{
if value[j] == formList[i].Field {
if checkInTable(columns, formList[i].Field) {
if formList[i].FormType.IsSelect() {
valueArr := formList[i].FilterFn(types.RowModel{
ID: id,
Value: db.GetValueFromDatabaseType(tb.form.FormList[i].TypeName, res[tb.form.FormList[i].Field]).String(),
Value: db.GetValueFromDatabaseType(formList[i].TypeName, res[formList[i].Field]).String(),
Row: res,
}).([]string)
for _, v := range tb.form.FormList[i].Options {
for _, v := range formList[i].Options {
if modules.InArray(valueArr, v["value"]) {
v["selected"] = "selected"
}
}
} else {
tb.form.FormList[i].Value = tb.form.FormList[i].FilterFn(types.RowModel{
formList[i].Value = formList[i].FilterFn(types.RowModel{
ID: id,
Value: db.GetValueFromDatabaseType(tb.form.FormList[i].TypeName, res[tb.form.FormList[i].Field]).String(),
Value: db.GetValueFromDatabaseType(formList[i].TypeName, res[formList[i].Field]).String(),
Row: res,
}).(string)
}
} else {
if tb.form.FormList[i].FormType.IsSelect() {
valueArr := tb.form.FormList[i].FilterFn(types.RowModel{
if formList[i].FormType.IsSelect() {
valueArr := formList[i].FilterFn(types.RowModel{
ID: id,
Value: "",
Row: res,
}).([]string)
for _, v := range tb.form.FormList[i].Options {
for _, v := range formList[i].Options {
if modules.InArray(valueArr, v["value"]) {
v["selected"] = "selected"
}
}
} else {
tb.form.FormList[i].Value = tb.form.FormList[i].FilterFn(types.RowModel{
formList[i].Value = formList[i].FilterFn(types.RowModel{
ID: id,
Value: "",
Row: res,
}).(string)
}
}
list[j] = tb.form.FormList[i]
list[j] = formList[i]
break
}
}
Expand All @@ -663,39 +665,39 @@ func (tb DefaultTable) GetDataFromDatabaseWithId(id string) ([]types.Form, [][]t
}

for i := 0; i < len(tb.form.FormList); i++ {
if checkInTable(columns, tb.form.FormList[i].Field) {
if tb.form.FormList[i].FormType.IsSelect() {
valueArr := tb.form.FormList[i].FilterFn(types.RowModel{
if checkInTable(columns, formList[i].Field) {
if formList[i].FormType.IsSelect() {
valueArr := formList[i].FilterFn(types.RowModel{
ID: id,
Value: db.GetValueFromDatabaseType(tb.form.FormList[i].TypeName, res[tb.form.FormList[i].Field]).String(),
Value: db.GetValueFromDatabaseType(formList[i].TypeName, res[formList[i].Field]).String(),
Row: res,
}).([]string)
for _, v := range tb.form.FormList[i].Options {
for _, v := range formList[i].Options {
if modules.InArray(valueArr, v["value"]) {
v["selected"] = "selected"
}
}
} else {
tb.form.FormList[i].Value = tb.form.FormList[i].FilterFn(types.RowModel{
formList[i].Value = formList[i].FilterFn(types.RowModel{
ID: id,
Value: db.GetValueFromDatabaseType(tb.form.FormList[i].TypeName, res[tb.form.FormList[i].Field]).String(),
Value: db.GetValueFromDatabaseType(formList[i].TypeName, res[formList[i].Field]).String(),
Row: res,
}).(string)
}
} else {
if tb.form.FormList[i].FormType.IsSelect() {
valueArr := tb.form.FormList[i].FilterFn(types.RowModel{
if formList[i].FormType.IsSelect() {
valueArr := formList[i].FilterFn(types.RowModel{
ID: id,
Value: "",
Row: res,
}).([]string)
for _, v := range tb.form.FormList[i].Options {
for _, v := range formList[i].Options {
if modules.InArray(valueArr, v["value"]) {
v["selected"] = "selected"
}
}
} else {
tb.form.FormList[i].Value = tb.form.FormList[i].FilterFn(types.RowModel{
formList[i].Value = formList[i].FilterFn(types.RowModel{
ID: id,
Value: "",
Row: res,
Expand All @@ -704,7 +706,7 @@ func (tb DefaultTable) GetDataFromDatabaseWithId(id string) ([]types.Form, [][]t
}
}

return tb.form.FormList, groupFormList, groupHeaders, tb.form.Title, tb.form.Description
return formList, groupFormList, groupHeaders, tb.form.Title, tb.form.Description
}

// UpdateDataFromDatabase update data.
Expand Down
13 changes: 13 additions & 0 deletions template/types/types.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/chenhg5/go-admin/modules/db"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/plugins/admin/models"
"github.com/chenhg5/go-admin/plugins/admin/modules"
"github.com/chenhg5/go-admin/template/types/form"
"html/template"
)
Expand Down Expand Up @@ -163,6 +164,18 @@ type FormPanel struct {

type FormList []Form

func (f FormList) Copy() FormList {
formList := make(FormList, len(f))
copy(formList, f)
for i := 0; i < len(formList); i++ {
formList[i].Options = make([]map[string]string, len(f[i].Options))
for j := 0; j < len(f[i].Options); j++ {
formList[i].Options[j] = modules.CopyMap(f[i].Options[j])
}
}
return formList
}

func (f FormList) FindByField(field string) Form {
for i := 0; i < len(f); i++ {
if f[i].Field == field {
Expand Down

0 comments on commit 0135b4c

Please sign in to comment.