-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
173 lines (134 loc) · 3.94 KB
/
app.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
package main
import (
"encoding/json"
"log"
"net/http"
"errors"
"github.com/julienschmidt/httprouter"
"github.com/qclaogui/database/builder"
)
// CREATE TABLE `notes` (
// `title` varchar(255) DEFAULT NULL,
// `body` text,
// `id` int(11) NOT NULL AUTO_INCREMENT,
// PRIMARY KEY (`id`)
// ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
var (
errInsert = errors.New("Oops! error occurs when insert")
errDelete = errors.New("Oops! error occurs when delete")
errUpdate = errors.New("Oops! error occurs when update")
errNotExist = errors.New("Oops! notes is not exist")
errEmptyNotes = errors.New("Oops! no notes")
)
// AppService service
type AppService struct {
DB builder.Connector
DM *builder.DatabaseManager
router *httprouter.Router
}
// NewAppService new service
func NewAppService() *AppService {
// all done
db, dm := builder.Run("/absolute/path/to/database.yml")
return &AppService{
DB: db,
DM: dm,
router: httprouter.New(),
}
}
// postman api collections
// https://www.getpostman.com/collections/1a1777b69c8b61d8c180
// AppService routes
func (s *AppService) routes() {
// welcome msg
s.router.GET("/", s.welcome)
s.router.GET("/notes", s.GetAll)
s.router.POST("/notes", s.Create)
s.router.GET("/notes/:id", s.GetOne)
s.router.PUT("/notes/:id", s.Update)
s.router.DELETE("/notes/:id", s.Destroy)
}
type resBody struct {
ErrCode int `json:"err_code"`
Data interface{} `json:"data"`
ErrMsg string `json:"err_msg"`
}
func resOK(data interface{}) *resBody {
return &resBody{ErrCode: 0, Data: data, ErrMsg: "ok"}
}
func resNotOK(errCode int, data interface{}, errMsg string) *resBody {
return &resBody{ErrCode: errCode, Data: data, ErrMsg: errMsg}
}
func toJSON(w http.ResponseWriter, result *resBody) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(result)
}
// welcome msg
func (s *AppService) welcome(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
toJSON(w, resOK("Welcome gopher!"))
}
// Create CURD Create
func (s *AppService) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var notes []map[string]string
note := map[string]string{
"title": r.PostFormValue("title"),
"body": r.PostFormValue("body"),
}
notes = append(notes, note)
res := s.DB.Table("notes").Insert(notes)
if res < 1 {
toJSON(w, resNotOK(40001, false, errInsert.Error()))
} else {
toJSON(w, resOK(true))
}
}
// Update CURD Update
func (s *AppService) Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
note := map[string]string{
"title": r.PostFormValue("title"),
"body": r.PostFormValue("body"),
}
res := s.DB.Table("notes").Where("id", ps.ByName("id")).Update(note)
if res < 1 {
toJSON(w, resNotOK(40002, false, errUpdate.Error()))
} else {
toJSON(w, resOK(true))
}
}
// GetOneByTitle CURD Retrieve
func (s *AppService) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res := s.DB.Table("notes").Where("id", ps.ByName("id")).First()
if len(res) < 1 {
toJSON(w, resNotOK(40004, false, errNotExist.Error()))
} else {
toJSON(w, resOK(res))
}
}
// Destroy CURD Delete
func (s *AppService) Destroy(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res := s.DB.Table("notes").Where("id", ps.ByName("id")).Delete()
if res < 1 {
toJSON(w, resNotOK(40005, false, errDelete.Error()))
} else {
toJSON(w, resOK(true))
}
}
// GetAll get all notes
func (s *AppService) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
res := s.DB.Table("notes").Limit(1000).Get()
if len(res) < 1 {
toJSON(w, resNotOK(40006, false, errEmptyNotes.Error()))
} else {
toJSON(w, resOK(res))
}
}
func main() {
// create Service
server := NewAppService()
// add routes
server.routes()
// Run
log.Println("server run at http://localhost:8088")
log.Fatal(http.ListenAndServe(":8088", server.router))
}