-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
190 lines (162 loc) · 4.02 KB
/
base.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
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"database/sql"
"bytes"
"net/http"
"encoding/json"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql","root:pwd@(127.0.0.1:3306)/gotest")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Connection established successfully")
}
defer db.Close()
//Checking for connection:
//sql.Open doesn't give an error if the machine is not reachable. Hence it is necessary to Ping.
err = db.Ping()
if err != nil {
fmt.Println(err.Error())
}
type Emp struct {
id int
first_name string
last_name string
}
router := gin.Default()
//API HANDLERS
// GET: Single Employee's details:
router.GET("/emp/:id", func(c *gin.Context) {
var (
emp Emp
result gin.H
)
id := c.Param("id")
row := db.QueryRow("select id,first_name,last_name from emp where id = ?;",id)
err := row.Scan(&emp.id,&emp.first_name,&emp.last_name)
if err != nil {
//if no results found send Nill
result = gin.H{
"result": nil,
"count": 0,
}
} else {
result = gin.H{
"result": gin.H{
"id": emp.id,
"first_name": emp.first_name,
"last_name": emp.last_name,
},
"count": 1,
}
}
c.JSON(http.StatusOK, result)
})
//GET all Employees data:
router.GET("/allemps", func(c *gin.Context) {
var buffer bytes.Buffer
var (
emp Emp
allEmps []Emp
)
rows, err := db.Query("select id,first_name,last_name from emp;")
if err != nil {
fmt.Print(err.Error())
}
for rows.Next() {
err = rows.Scan(&emp.id, &emp.first_name,&emp.last_name)
if err != nil {
fmt.Print(err.Error())
}
allEmps = append(allEmps, emp)
d,err := json.Marshal(emp)
if err != nil {
fmt.Println(err.Error())
}
fmt.Sprintf(string(d))
}
for i:=0;i<len(allEmps);i++ {
buffer.WriteString("id: ")
buffer.WriteString(string(allEmps[i].id))
buffer.WriteString(" First Name: ")
buffer.WriteString(allEmps[i].first_name)
buffer.WriteString(" Last Name: ")
buffer.WriteString(allEmps[i].last_name)
buffer.WriteString("\n")
}
result := buffer.String()
fmt.Println(result)
defer rows.Close()
c.JSON(http.StatusOK, gin.H{
"result": result,
"count": len(allEmps),
})
})
//POST a new Employee data:
router.POST("/emp", func(c *gin.Context) {
var buffer bytes.Buffer
first_name := c.PostForm("first_name")
last_name := c.PostForm("last_name")
stmt, err := db.Prepare("insert into emp(first_name,last_name) values (?,?);")
if err != nil {
fmt.Println(err.Error())
}
_, err = stmt.Exec(first_name,last_name)
if err != nil {
fmt.Println(err.Error())
}
//append String to buffer - Faster, Performance boosted!
buffer.WriteString(first_name)
buffer.WriteString(" ")
buffer.WriteString(last_name)
defer stmt.Close()
name := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf(" %s successfully inserted", name),
})
})
//Delete resources:
router.DELETE("/emp/:id", func(c *gin.Context) {
id := c.Param("id")
stmt, err := db.Prepare("delete from emp where id= ?;")
if err != nil {
fmt.Println(err.Error())
}
_,err = stmt.Exec(id)
if err != nil {
fmt.Println(err.Error())
}
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully deleted userid: %s", id),
})
})
//PUT - update an employee's data:
router.PUT("/emp/:id", func(c *gin.Context) {
var buffer bytes.Buffer
id := c.Param("id")
first_name := c.PostForm("first_name")
last_name := c.PostForm("last_name")
stmt, err := db.Prepare("update emp set first_name= ?, last_name= ? where id= ?")
if err != nil {
fmt.Println(err.Error())
}
_, err = stmt.Exec(first_name,last_name,id)
if err != nil {
fmt.Println(err.Error())
}
//append strings
buffer.WriteString(first_name)
buffer.WriteString(" ")
buffer.WriteString(last_name)
defer stmt.Close()
name := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully updated user %s", name),
})
})
router.Run(":3000")
}