-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (129 loc) · 2.91 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
package main
import (
"database/sql"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
type Person struct {
Id int
First_Name string `json:"first_name"`
Last_Name string `json:"last_name"`
}
func main() {
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/db_person")
if err != nil {
fmt.Print(err.Error())
}
defer db.Close()
// make sure connection is available
err = db.Ping()
if err != nil {
fmt.Print(err.Error())
}
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.GET("/person", func(c *gin.Context) {
var (
person Person
persons []Person
)
rows, err := db.Query("select id, first_name, last_name from person;")
if err != nil {
fmt.Print(err.Error())
}
for rows.Next() {
err = rows.Scan(&person.Id, &person.First_Name, &person.Last_Name)
persons = append(persons, person)
if err != nil {
fmt.Print(err.Error())
}
}
defer rows.Close()
c.JSON(http.StatusOK, gin.H{
"result": persons,
"count": len(persons),
})
})
r.GET("/person/:id", func(c *gin.Context) {
var (
person Person
result gin.H
)
id := c.Param("id")
row := db.QueryRow("select id, first_name, last_name from person where id = ?;", id)
err = row.Scan(&person.Id, &person.First_Name, &person.Last_Name)
if err != nil {
// If no results send null
result = gin.H{
"result": nil,
"count": 0,
}
} else {
result = gin.H{
"result": person,
"count": 1,
}
fmt.Println("get person by ID")
}
c.JSON(http.StatusOK, result)
})
r.POST("/person", func(c *gin.Context) {
var p Person
c.BindJSON(&p)
stmt, err := db.Prepare("insert into person (first_name, last_name) values(?,?);")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(p.First_Name, p.Last_Name)
if err != nil {
fmt.Print(err.Error())
}
output := p.First_Name + " " + p.Last_Name + " created"
fmt.Println(output)
c.JSON(http.StatusOK, gin.H{
"message": output,
})
})
r.PUT("/person", func(c *gin.Context) {
var p Person
c.BindJSON(&p)
stmt, err := db.Prepare("update person set first_name= ?, last_name= ? where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(p.First_Name, p.Last_Name, p.Id)
if err != nil {
fmt.Print(err.Error())
}
output := "Successful update id " + strconv.Itoa(p.Id)
fmt.Println(output)
c.JSON(http.StatusOK, gin.H{
"message": output,
})
})
r.DELETE("/person", func(c *gin.Context) {
var p Person
c.BindJSON(&p)
stmt, err := db.Prepare("delete from person where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(p.Id)
if err != nil {
fmt.Print(err.Error())
}
output := "Delete person id " + strconv.Itoa(p.Id)
fmt.Println(output)
c.JSON(http.StatusOK, gin.H{
"message": output,
})
})
r.Run(":8081")
}