-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (150 loc) · 3.73 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
package main
import (
"database/sql"
"log"
"strconv"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
)
// var db, err = sql.Open("sqlite3", "employees.db")
var db, err = sql.Open("sqlite3", "file::memory:?cache=shared")
func main() {
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS employees (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, position TEXT, salary REAL)")
if err != nil {
log.Fatal(err)
}
defer db.Close()
r := gin.Default()
r.GET("/", indexHandler)
r.GET("/employee/:id", getEmployeeByIdHandler)
r.POST("/employee/create", createEmployeeHandler)
r.PUT("/employee/update/:id", updateEmployeeHandler)
r.DELETE("/employee/delete/:id", deleteEmployeeHandler)
r.GET("/employees", getAllEmployeesHandler)
r.Run(":8080")
}
// Model
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
Position string `json:"position"`
Salary float64 `json:"salary"`
}
// Handler
func indexHandler(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Server Online!!",
})
}
func getEmployeeByIdHandler(c *gin.Context) {
id := c.Param("id")
var employee Employee
err := db.QueryRow("SELECT id, name, position, salary FROM employees WHERE id = ?", id).Scan(&employee.ID, &employee.Name, &employee.Position, &employee.Salary)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to get employee",
})
return
}
c.JSON(200, employee)
}
func createEmployeeHandler(c *gin.Context) {
var employee Employee
err := c.BindJSON(&employee)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to bind JSON",
})
return
}
result, err := db.Exec("INSERT INTO employees (name, position, salary) VALUES (?, ?, ?)", employee.Name, employee.Position, employee.Salary)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to create employee",
})
return
}
id, _ := result.LastInsertId()
employee.ID = int(id)
c.JSON(200, employee)
}
func updateEmployeeHandler(c *gin.Context) {
id := c.Param("id")
var employee Employee
err := c.BindJSON(&employee)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to bind JSON",
})
return
}
_, err = db.Exec("UPDATE employees SET name = ?, position = ?, salary = ? WHERE id = ?", employee.Name, employee.Position, employee.Salary, id)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to update employee",
})
return
}
c.JSON(200, "Updated employee successfully")
}
func deleteEmployeeHandler(c *gin.Context) {
id := c.Param("id")
_, err := db.Exec("DELETE FROM employees WHERE id = ?", id)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to delete employee",
})
return
}
c.JSON(200, gin.H{
"message": "Employee deleted",
})
}
func getAllEmployeesHandler(c *gin.Context) {
pageStr := c.DefaultQuery("page", "1")
pageSizeStr := c.DefaultQuery("size", "10")
page, err := strconv.Atoi(pageStr)
if err != nil {
c.JSON(500, gin.H{
"message": "page must be a number",
})
return
}
pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil {
c.JSON(500, gin.H{
"message": "size must be a number",
})
return
}
offset := (page - 1) * pageSize
rows, err := db.Query("SELECT id, name, position, salary FROM employees LIMIT ? OFFSET ?", pageSize, offset)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to get employees",
})
return
}
var employees []Employee
for rows.Next() {
var employee Employee
err := rows.Scan(&employee.ID, &employee.Name, &employee.Position, &employee.Salary)
if err != nil {
c.JSON(500, gin.H{
"message": "Failed to get employees",
})
return
}
employees = append(employees, employee)
}
if len(employees) == 0 {
c.JSON(404, gin.H{
"message": "No employees found",
})
return
}
c.JSON(200, employees)
}