-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
161 lines (139 loc) · 3.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"gin_sqlite_crud_demo/models"
"github.com/brianvoe/gofakeit"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/robfig/cron/v3"
"gopkg.in/ini.v1"
"math"
"net/http"
"os"
"strconv"
"time"
)
var db *gorm.DB
const pagesize = 10
func init() {
cfg, err := ini.Load("conf/app.ini")
if err != nil {
fmt.Println("Can Not Load App.ini file, Please Check app.ini file is exist")
os.Exit(1)
}
dbPath := cfg.Section("database").Key("PATH").String()
fmt.Println(dbPath)
db, err = gorm.Open("sqlite3", dbPath)
if err != nil {
fmt.Println("Cant Not Open db file, Please Check db file or db config")
os.Exit(1)
}
dbExist := db.HasTable("students")
if !dbExist {
db.CreateTable(&models.Student{})
}
go AddUserAuto(db)
}
func main() {
defer db.Close()
router := gin.Default()
router.LoadHTMLGlob("templates/**/*")
router.Static("/static", "./static")
router.GET("/", func(c *gin.Context) {
var model = models.Student{}
page, _ := strconv.Atoi(c.Query("page"))
if page == 0 {
page = 1
}
datas := model.GetList(page, pagesize, db)
total_count := model.GetListTotalCount(db)
var page_List []int
page_count := int(math.Ceil(float64(total_count / pagesize)))
if total_count%pagesize != 0 {
page_count += 1
}
for i := 1; i <= page_count; i++ {
page_List = append(page_List, i)
}
c.HTML(200, "index/home", gin.H{
"Datas": datas,
"CurPage": page,
"PageList": page_List,
})
})
router.GET("/add", func(c *gin.Context) {
c.HTML(http.StatusOK, "index/add", gin.H{})
})
router.GET("/detail", func(c *gin.Context) {
id, _ := strconv.Atoi(c.Query("id"))
var model models.Student
model = model.GetDetail(id, db)
c.HTML(200, "index/edit", gin.H{
"Detail": model,
})
})
router.POST("/save", func(c *gin.Context) {
age, _ := strconv.Atoi(c.PostForm("age"))
gender, _ := strconv.Atoi(c.PostForm("gender"))
timeLayout := "2006-01-02" //转化所需模板
loc, _ := time.LoadLocation("Local")
barth := c.PostForm("birthday")
id, _ := strconv.Atoi(c.PostForm("id"))
birthDay, _ := time.ParseInLocation(timeLayout, barth, loc)
var student models.Student
if id != 0 {
student = student.GetDetail(id, db)
student.BirthDay = birthDay
student.Name = c.PostForm("name")
student.Email = c.PostForm("email")
student.Phone = c.PostForm("phone")
student.Gender = gender
student.Address = c.PostForm("address")
student.Update(&student, db)
} else {
student = models.Student{Name: c.PostForm("name"), Age: age, Address: c.PostForm("address"), Gender: gender, Phone: c.PostForm("phone"), Email: c.PostForm("email"), BirthDay: birthDay}
student.Create(&student, db)
}
c.Redirect(302, "/")
})
router.GET("/search", func(c *gin.Context) {
searchKey := c.Query("searchkey")
page, _ := strconv.Atoi(c.Query("page"))
if page == 0 {
page = 1
}
var model = models.Student{}
students := model.Search(searchKey, page, pagesize, db)
total_count := model.SearchResultCount(searchKey, db)
var page_List []int
page_count := int(math.Ceil(float64(total_count / pagesize)))
if total_count%pagesize != 0 {
page_count += 1
}
for i := 1; i <= page_count; i++ {
page_List = append(page_List, i)
}
c.HTML(200, "index/home", gin.H{
"Datas": students,
"CurPage": page,
"PageList": page_List,
"SearchKey": searchKey,
})
})
router.DELETE("/delete", func(c *gin.Context) {
id, _ := strconv.Atoi(c.Query("id"))
var model models.Student
model.Delete(id, db)
})
var _ = router.Run(":8080")
}
func AddUserAuto(db *gorm.DB) {
c := cron.New(cron.WithSeconds())
_, _ = c.AddFunc("*/30 * * * * *", func() {
user := models.Student{Name: gofakeit.Name(), Age: gofakeit.Number(10, 20), Email: gofakeit.Email(), Phone: gofakeit.Phone(), BirthDay: gofakeit.Date(), Gender: gofakeit.Number(0, 1), Address: gofakeit.Address().Address}
user.Create(&user, db)
})
c.Run()
select {}
}