Skip to content

Commit 3ba796d

Browse files
author
陈弘桂
committedJul 24, 2018
organize: make structure more clear
1 parent 8abc850 commit 3ba796d

File tree

11 files changed

+252
-266
lines changed

11 files changed

+252
-266
lines changed
 

‎config/data.go

-7
This file was deleted.

‎controllers/delete.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package controller
22

33
import (
44
"github.com/valyala/fasthttp"
5-
"goAdmin/transform"
5+
"goAdmin/models"
66
)
77

88
func DeleteData(ctx *fasthttp.RequestCtx) {
@@ -13,7 +13,7 @@ func DeleteData(ctx *fasthttp.RequestCtx) {
1313

1414
id := string(ctx.FormValue("id")[:])
1515

16-
transform.DeleteDataFromDatabase(prefix, id)
16+
models.GlobalTableList[prefix].DeleteDataFromDatabase(prefix, id)
1717

1818
// TODO: 增加反馈
1919

‎controllers/edit.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"goAdmin/auth"
77
"goAdmin/menu"
88
"goAdmin/template"
9-
"goAdmin/transform"
9+
"goAdmin/models"
1010
)
1111

1212
// 显示表单
@@ -30,7 +30,8 @@ func ShowForm(ctx *fasthttp.RequestCtx) {
3030
pageSize = "10"
3131
}
3232

33-
formData, title, description := transform.TransfromFormData(id, prefix)
33+
formData, title, description := models.GlobalTableList[prefix].GetDataFromDatabaseWithId(prefix, id)
34+
3435
url := "/edit/" + prefix + "?id=" + id
3536
previous := "/info/" + prefix + "?page=" + page + "&pageSize=" + pageSize
3637

@@ -55,7 +56,7 @@ func EditForm(ctx *fasthttp.RequestCtx) {
5556

5657
form, _ := ctx.MultipartForm()
5758

58-
transform.UpdateDataFromDatabase(prefix, (*form).Value)
59+
models.GlobalTableList[prefix].UpdateDataFromDatabase(prefix, (*form).Value)
5960

6061
// TODO: 增加反馈
6162

‎controllers/new.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package controller
33
import (
44
"bytes"
55
"github.com/valyala/fasthttp"
6-
"goAdmin/config"
76
"goAdmin/menu"
87
"goAdmin/template"
9-
"goAdmin/transform"
8+
"goAdmin/models"
109
)
1110

1211
// 显示新建表单
@@ -32,9 +31,9 @@ func ShowNewForm(ctx *fasthttp.RequestCtx) {
3231
previous := "/info/" + prefix + "?page=" + page + "&pageSize=" + pageSize
3332

3433
if string(ctx.Request.Header.Peek("X-PJAX")[:]) == "true" {
35-
template.NewPanelPjax(config.GlobalTableList[prefix].Form.FormList, url, previous, id, config.GlobalTableList[prefix].Form.Title, config.GlobalTableList[prefix].Form.Description, buffer)
34+
template.NewPanelPjax(models.GlobalTableList[prefix].Form.FormList, url, previous, id, models.GlobalTableList[prefix].Form.Title, models.GlobalTableList[prefix].Form.Description, buffer)
3635
} else {
37-
template.NewPanel(config.GlobalTableList[prefix].Form.FormList, url, previous, id, (*menu.GlobalMenu).GlobalMenuList, config.GlobalTableList[prefix].Form.Title, config.GlobalTableList[prefix].Form.Description, buffer)
36+
template.NewPanel(models.GlobalTableList[prefix].Form.FormList, url, previous, id, (*menu.GlobalMenu).GlobalMenuList, models.GlobalTableList[prefix].Form.Title, models.GlobalTableList[prefix].Form.Description, buffer)
3837
}
3938

4039
ctx.Response.AppendBody(buffer.Bytes())
@@ -52,7 +51,7 @@ func NewForm(ctx *fasthttp.RequestCtx) {
5251

5352
form, _ := ctx.MultipartForm()
5453

55-
transform.InsertDataFromDatabase(prefix, (*form).Value)
54+
models.GlobalTableList[prefix].InsertDataFromDatabase(prefix, (*form).Value)
5655

5756
// TODO: 增加反馈
5857

‎controllers/show.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"goAdmin/auth"
77
"goAdmin/menu"
88
"goAdmin/template"
9-
"goAdmin/transform"
9+
"goAdmin/models"
1010
)
1111

1212
// 显示列表
@@ -27,7 +27,12 @@ func ShowInfo(ctx *fasthttp.RequestCtx) {
2727
pageSize = []byte("10")
2828
}
2929

30-
thead, infoList, paginator, title, description := transform.TransfromData(string(page), string(pageSize), path, prefix)
30+
thead, infoList, paginator, title, description := models.GlobalTableList[prefix].GetDataFromDatabase(map[string]string{
31+
"page" : string(page),
32+
"path" : string(path),
33+
"prefix" : prefix,
34+
"pageSize" : string(pageSize),
35+
})
3136

3237
menu.GlobalMenu.SetActiveClass(path)
3338

‎models/base.go

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package models
2+
3+
import (
4+
"strconv"
5+
"goAdmin/connections/mysql"
6+
"strings"
7+
)
8+
9+
// 表单列
10+
type FormStruct struct {
11+
Field string
12+
TypeName string
13+
Head string
14+
Default string
15+
Editable bool
16+
FormType string
17+
Value string
18+
}
19+
20+
// 数据过滤函数
21+
type FieldValueFun func(value string) string
22+
23+
// 展示列
24+
type FieldStruct struct {
25+
ExcuFun FieldValueFun
26+
Field string
27+
TypeName string
28+
Head string
29+
}
30+
31+
func (field *FieldStruct) SetHead(head string) *FieldStruct {
32+
(*field).Head = head
33+
return field
34+
}
35+
36+
func (field *FieldStruct) SetTypeName(typeName string) *FieldStruct {
37+
(*field).TypeName = typeName
38+
return field
39+
}
40+
41+
func (field *FieldStruct) SetField(fieldName string) *FieldStruct {
42+
(*field).Field = fieldName
43+
return field
44+
}
45+
46+
// 展示面板
47+
type InfoPanel struct {
48+
FieldList []FieldStruct
49+
Table string
50+
Title string
51+
Description string
52+
}
53+
54+
// 表单面板
55+
type FormPanel struct {
56+
FormList []FormStruct
57+
Table string
58+
Title string
59+
Description string
60+
}
61+
62+
// 一个管理数据模块的抽象表示
63+
type GlobalTable struct {
64+
Info InfoPanel
65+
Form FormPanel
66+
}
67+
68+
// 查数据
69+
func (tableModel GlobalTable) GetDataFromDatabase(queryParam map[string]string) ([]string, []map[string]string, map[string]interface{}, string, string) {
70+
71+
pageInt, _ := strconv.Atoi(queryParam["page"])
72+
73+
title := tableModel.Info.Title
74+
description := tableModel.Info.Description
75+
76+
thead := make([]string, 0)
77+
fields := ""
78+
79+
for i := 0; i < len(tableModel.Info.FieldList); i++ {
80+
if tableModel.Info.FieldList[i].Field != "id" {
81+
fields += tableModel.Info.FieldList[i].Field + ","
82+
}
83+
thead = append(thead, tableModel.Info.FieldList[i].Head)
84+
}
85+
86+
fields += "id"
87+
88+
res, _ := mysql.Query("select "+fields+" from "+tableModel.Info.Table+" where id > 0 order by created_at desc LIMIT ? OFFSET ?",
89+
queryParam["pageSize"], (pageInt-1)*10)
90+
91+
infoList := make([]map[string]string, 0)
92+
93+
for i := 0; i < len(res); i++ {
94+
95+
// TODO: 加入对象池
96+
tempModelData := make(map[string]string, 0)
97+
98+
for j := 0; j < len(tableModel.Info.FieldList); j++ {
99+
tempModelData[tableModel.Info.FieldList[j].Head] = tableModel.Info.FieldList[j].ExcuFun(GetStringFromType(tableModel.Info.FieldList[j].TypeName, res[i][tableModel.Info.FieldList[j].Field]))
100+
}
101+
102+
tempModelData["id"] = GetStringFromType("int", res[i]["id"])
103+
104+
infoList = append(infoList, tempModelData)
105+
}
106+
107+
total, _ := mysql.Query("select count(*) from "+tableModel.Info.Table+" where id > ?", 0)
108+
size := int(total[0]["count(*)"].(int64))
109+
110+
paginator := GetPaginator(queryParam["path"], pageInt, queryParam["page"], queryParam["pageSize"], size, queryParam["prefix"])
111+
112+
return thead, infoList, paginator, title, description
113+
114+
}
115+
116+
// 查单个数据
117+
func (tableModel GlobalTable) GetDataFromDatabaseWithId(prefix string, id string) ([]FormStruct, string, string) {
118+
119+
fields := ""
120+
121+
for i := 0; i < len(tableModel.Form.FormList); i++ {
122+
fields += tableModel.Form.FormList[i].Field + ","
123+
}
124+
125+
fields = fields[0 : len(fields)-1]
126+
127+
res, _ := mysql.Query("select "+fields+" from "+tableModel.Form.Table+" where id = ?", id)
128+
129+
for i := 0; i < len(tableModel.Form.FormList); i++ {
130+
tableModel.Form.FormList[i].Value = GetStringFromType(tableModel.Form.FormList[i].TypeName, res[0][tableModel.Form.FormList[i].Field])
131+
}
132+
133+
return tableModel.Form.FormList, tableModel.Form.Title, tableModel.Form.Description
134+
}
135+
136+
// 改数据
137+
func (tableModel GlobalTable) UpdateDataFromDatabase(prefix string, dataList map[string][]string) {
138+
139+
fields := ""
140+
valueList := make([]interface{}, 0)
141+
for k, v := range dataList {
142+
if k != "id" && k != "_previous_" && k != "_method" && k != "_token" {
143+
fields += k + " = ?,"
144+
valueList = append(valueList, v[0])
145+
}
146+
}
147+
148+
fields = fields[0 : len(fields)-1]
149+
valueList = append(valueList, dataList["id"][0])
150+
151+
mysql.Exec("update "+tableModel.Form.Table+" set "+fields+" where id = ?", valueList...)
152+
}
153+
154+
// 增数据
155+
func (tableModel GlobalTable) InsertDataFromDatabase(prefix string, dataList map[string][]string) {
156+
157+
fields := ""
158+
queStr := ""
159+
valueList := make([]interface{}, 0)
160+
for k, v := range dataList {
161+
if k != "id" && k != "_previous_" && k != "_method" && k != "_token" {
162+
fields += k + " = ?,"
163+
queStr += "?,"
164+
valueList = append(valueList, v[0])
165+
}
166+
}
167+
168+
fields = fields[0 : len(fields)-1]
169+
queStr = queStr[0 : len(queStr)-1]
170+
valueList = append(valueList, dataList["id"][0])
171+
172+
mysql.Exec("insert into "+tableModel.Form.Table+"("+fields+") values ("+queStr+")", valueList...)
173+
}
174+
175+
// 删数据
176+
func (tableModel GlobalTable) DeleteDataFromDatabase(prefix string, id string) {
177+
mysql.Exec("delete from "+tableModel.Form.Table+" where id = ?", id)
178+
}
179+
180+
func GetStringFromType(typeName string, value interface{}) string {
181+
typeName = strings.ToUpper(typeName)
182+
if value == nil {
183+
return ""
184+
}
185+
switch typeName {
186+
case "INT":
187+
return strconv.FormatInt(value.(int64), 10)
188+
case "TINYINT":
189+
return strconv.FormatInt(value.(int64), 10)
190+
case "MEDIUMINT":
191+
return strconv.FormatInt(value.(int64), 10)
192+
case "SMALLINT":
193+
return strconv.FormatInt(value.(int64), 10)
194+
case "BIGINT":
195+
return strconv.FormatInt(value.(int64), 10)
196+
case "FLOAT":
197+
return strconv.FormatFloat(value.(float64), 'g', 5, 32)
198+
case "DOUBLE":
199+
return strconv.FormatFloat(value.(float64), 'g', 5, 32)
200+
case "DECIMAL":
201+
return string(value.(uint8))
202+
case "DATE":
203+
return value.(string)
204+
case "TIME":
205+
return value.(string)
206+
case "YEAR":
207+
return value.(string)
208+
case "DATETIME":
209+
return value.(string)
210+
case "TIMESTAMP":
211+
return value.(string)
212+
case "VARCHAR":
213+
return value.(string)
214+
case "MEDIUMTEXT":
215+
return value.(string)
216+
case "LONGTEXT":
217+
return value.(string)
218+
case "TINYTEXT":
219+
return value.(string)
220+
case "TEXT":
221+
return value.(string)
222+
default:
223+
return ""
224+
}
225+
}

‎models/global.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models
2+
3+
// map下标是路由前缀,对应的值是GlobalTable类型,为表单与表格的数据抽象表示
4+
var GlobalTableList = map[string]GlobalTable{
5+
"user": GetUserTable(),
6+
}

‎transform/paginator.go ‎models/paginator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package transform
1+
package models
22

33
import "strconv"
44

‎models/panel.go

-60
This file was deleted.

‎models/user.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package models
22

3-
import "goAdmin/components"
3+
import (
4+
"goAdmin/components"
5+
)
46

57
func GetUserTable() (userTable GlobalTable) {
68

‎transform/transfrom.go

-185
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.