-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
167 lines (134 loc) · 4.1 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
166
167
package main
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type Users struct {
Id int `gorm:"AUTO_INCREMENT" form:"id" json:"id"`
Firstname string `gorm:"not null" form:"firstname" json:"firstname"`
Lastname string `gorm:"not null" form:"lastname" json:"lastname"`
}
func PostUser(c *gin.Context) {
db := InitDb()
defer db.Close()
var user Users
c.Bind(&user)
if user.Firstname != "" && user.Lastname != "" {
// INSERT INTO "users" (name) VALUES (user.Name);
db.Create(&user)
// Display error
c.JSON(201, gin.H{"success": user})
} else {
// Display error
c.JSON(422, gin.H{"error": "Fields are empty"})
}
// curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/users
}
func GetUsers(c *gin.Context) {
// Connection to the database
db := InitDb()
// Close connection database
defer db.Close()
var users []Users
// SELECT * FROM users
db.Find(&users)
// Display JSON result
c.JSON(200, users)
// curl -i http://localhost:10000/api/v1/users
}
func GetUser(c *gin.Context) {
// Connection to the database
db := InitDb()
// Close connection database
defer db.Close()
id := c.Params.ByName("id")
var user Users
// SELECT * FROM users WHERE id = 1;
db.First(&user, id)
if user.Id != 0 {
// Display JSON result
c.JSON(200, user)
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "User not found"})
}
// curl -i http://localhost:10000/api/v1/users/1
}
func UpdateUser(c *gin.Context) {
// Connection to the database
db := InitDb()
// Close connection database
defer db.Close()
// Get id user
id := c.Params.ByName("id")
var user Users
// SELECT * FROM users WHERE id = 1;
db.First(&user, id)
if user.Firstname != "" && user.Lastname != "" {
if user.Id != 0 {
var newUser Users
c.Bind(&newUser)
result := Users{
Id: user.Id,
Firstname: newUser.Firstname,
Lastname: newUser.Lastname,
}
// UPDATE users SET firstname='newUser.Firstname', lastname='newUser.Lastname' WHERE id = user.Id;
db.Save(&result)
// Display modified data in JSON message "success"
c.JSON(200, gin.H{"success": result})
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "User not found"})
}
} else {
// Display JSON error
c.JSON(422, gin.H{"error": "Fields are empty"})
}
// curl -i -X PUT -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Merlyn\" }" http://localhost:10000/api/v1/users/1
}
func DeleteUser(c *gin.Context) {
// Connection to the database
db := InitDb()
// Close connection database
defer db.Close()
// Get id user
id := c.Params.ByName("id")
var user Users
// SELECT * FROM users WHERE id = 1;
db.First(&user, id)
if user.Id != 0 {
// DELETE FROM users WHERE id = user.Id
db.Delete(&user)
// Display JSON result
c.JSON(200, gin.H{"success": "User #" + id + " deleted"})
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "User not found"})
}
// curl -i -X DELETE http://localhost:1000/api/v1/users/1
}
func InitDb() *gorm.DB {
db, err := gorm.Open("postgres", "host=xxx.xxx.xxx.xxx user=user dbname=test sslmode=disable password=ps")
db.LogMode(true)
// Error
if err != nil {
panic(err)
}
// Creating the table
db.AutoMigrate(&Users{})
return db
}
func main() {
r := gin.Default()
v1 := r.Group("api/v1")
{
v1.POST("/users", PostUser)
v1.GET("/users", GetUsers)
v1.GET("/users/:id", GetUser)
v1.PUT("/users/:id", UpdateUser)
v1.DELETE("/users/:id", DeleteUser)
}
r.Run(":10000")
}