forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (162 loc) · 4.17 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"net/http"
"os"
"time"
"github.com/jinzhu/gorm"
"github.com/kataras/iris/v12"
_ "github.com/mattn/go-sqlite3"
)
type User struct {
gorm.Model
Salt string `gorm:"type:varchar(255)" json:"salt"`
Username string `gorm:"type:varchar(32)" json:"username"`
Password string `gorm:"type:varchar(200);column:password" json:"-"`
Languages string `gorm:"type:varchar(200);column:languages" json:"languages"`
}
func (u User) TableName() string {
return "gorm_user"
}
type UserSerializer struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Salt string `json:"salt"`
UserName string `json:"user_name"`
Password string `json:"-"`
Languages string `json:"languages"`
}
func (self User) Serializer() UserSerializer {
return UserSerializer{
ID: self.ID,
CreatedAt: self.CreatedAt.Truncate(time.Second),
UpdatedAt: self.UpdatedAt.Truncate(time.Second),
Salt: self.Salt,
Password: self.Password,
Languages: self.Languages,
UserName: self.Username,
}
}
func main() {
app := iris.Default()
db, err := gorm.Open("sqlite3", "test.db")
db.LogMode(true) // show SQL logger
if err != nil {
app.Logger().Fatalf("connect to sqlite3 failed")
return
}
iris.RegisterOnInterrupt(func() {
defer db.Close()
})
if os.Getenv("ENV") != "" {
db.DropTableIfExists(&User{}) // drop table
}
db.AutoMigrate(&User{}) // create table: // AutoMigrate run auto migration for given models, will only add missing fields, won't delete/change current data
app.Post("/post_user", func(ctx iris.Context) {
var user User
user = User{
Username: "gorm",
Salt: "hash---",
Password: "admin",
Languages: "gorm",
}
if err := db.FirstOrCreate(&user); err == nil {
app.Logger().Fatalf("created one record failed: %s", err.Error)
ctx.JSON(iris.Map{
"code": http.StatusBadRequest,
"error": err.Error,
})
return
}
ctx.JSON(
iris.Map{
"code": http.StatusOK,
"data": user.Serializer(),
})
})
app.Get("/get_user/{id:uint}", func(ctx iris.Context) {
var user User
id, _ := ctx.Params().GetUint("id")
app.Logger().Println(id)
if err := db.Where("id = ?", int(id)).First(&user).Error; err != nil {
app.Logger().Fatalf("find one record failed: %t", err == nil)
ctx.JSON(iris.Map{
"code": http.StatusBadRequest,
"error": err.Error,
})
return
}
ctx.JSON(iris.Map{
"code": http.StatusOK,
"data": user.Serializer(),
})
})
app.Delete("/delete_user/{id:uint}", func(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
if id == 0 {
ctx.JSON(iris.Map{
"code": http.StatusOK,
"detail": "query param id should not be nil",
})
return
}
var user User
if err := db.Where("id = ?", id).First(&user).Error; err != nil {
app.Logger().Fatalf("record not found")
ctx.JSON(iris.Map{
"code": http.StatusOK,
"detail": err.Error,
})
return
}
db.Delete(&user)
ctx.JSON(iris.Map{
"code": http.StatusOK,
"data": user.Serializer(),
})
})
app.Patch("/patch_user/{id:uint}", func(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
if id == 0 {
ctx.JSON(iris.Map{
"code": http.StatusOK,
"detail": "query param id should not be nil",
})
return
}
var user User
tx := db.Begin()
if err := tx.Where("id = ?", id).First(&user).Error; err != nil {
app.Logger().Fatalf("record not found")
ctx.JSON(iris.Map{
"code": http.StatusOK,
"detail": err.Error,
})
return
}
var body patchParam
ctx.ReadJSON(&body)
app.Logger().Println(body)
if err := tx.Model(&user).Updates(map[string]interface{}{"username": body.Data.UserName, "password": body.Data.Password}).Error; err != nil {
app.Logger().Fatalf("update record failed")
tx.Rollback()
ctx.JSON(iris.Map{
"code": http.StatusBadRequest,
"error": err.Error,
})
return
}
tx.Commit()
ctx.JSON(iris.Map{
"code": http.StatusOK,
"data": user.Serializer(),
})
})
app.Listen(":8080")
}
type patchParam struct {
Data struct {
UserName string `json:"user_name" form:"user_name"`
Password string `json:"password" form:"password"`
} `json:"data"`
}