-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (64 loc) · 1.16 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
package main
import (
"github.com/gin-gonic/gin"
"restapi.com/hello/db"
h "restapi.com/hello/helpers"
p "restapi.com/hello/person"
)
func main() {
db := db.New()
r := gin.Default()
r.PUT("/api/create", func(c *gin.Context) {
var p p.Person
if c.BindJSON(&p) != nil {
return
}
id := db.Create(&p)
h.JOk(c, id)
})
r.GET("/api/read", func(c *gin.Context) {
var id int
if c.BindJSON(&id) != nil {
return
}
if p, err := db.Read(id); err != nil {
h.JErr(c, err.Error())
} else {
h.JOk(c, p)
}
})
r.PUT("/api/update", func(c *gin.Context) {
query := &struct {
ID int
Person p.Person
}{}
if c.BindJSON(query) != nil {
return
}
if err := db.Update(query.ID, &query.Person); err != nil {
h.JErr(c, err.Error())
} else {
h.JDone(c)
}
})
r.DELETE("/api/delete", func(c *gin.Context) {
var id int
if c.BindJSON(&id) != nil {
return
}
if err := db.Delete(id); err != nil {
h.JErr(c, err.Error())
} else {
h.JDone(c)
}
})
r.GET("/api/search", func(c *gin.Context) {
var sub string
if c.BindJSON(&sub) != nil {
return
}
res := db.Search(sub)
h.JOk(c, res)
})
r.Run("127.0.0.1:8080")
}