-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
247 lines (217 loc) · 6.33 KB
/
main.go
File metadata and controls
247 lines (217 loc) · 6.33 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"database/sql"
"fmt"
"go_dev/internal/controllers"
"log"
"os"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
var db = initDb()
const (
host = "localhost"
port = 5432
user = "postgres"
password = "justtheoldpassword"
dbname = "data"
)
func initDb() *sql.DB {
psqlconn := fmt.Sprintf("host = %s port = %d user = %s password = %s dbname = %s sslmode = disable", host, port, user, password, dbname)
_db, err := sql.Open("postgres", psqlconn)
CheckError(err)
if e := _db.Ping(); e != nil {
log.Fatalf("Error: %v", e)
}
return _db
}
func CheckError(e error) {
if e != nil {
panic(e)
}
}
func main() {
port := os.Getenv("PORT")
defer db.Close()
router := gin.Default()
router.GET("/products", controllers.GetProducts(db))
router.GET("/products/:guid", controllers.GetProduct(db))
router.POST("/products", controllers.AddProduct(db))
router.PUT("/products/:guid", controllers.UpdateProduct(db))
router.DELETE("/products/:guid", controllers.DeleteProduct(db))
log.Fatal(router.Run(":" + port))
}
// package main
// import (
// "database/sql"
// "fmt"
// "net/http"
// "strconv"
// "github.com/gin-gonic/gin"
// _ "github.com/lib/pq"
// )
// type album struct {
// ID string `json:"id"`
// Title string `json:"title"`
// Artist string `json:"artist"`
// Price float64 `json:"price"`
// }
// var db = initDb()
// func initDb() *sql.DB {
// psqlconn := fmt.Sprintf("host = %s port = %d user = %s password = %s dbname = %s sslmode = disable", host, port, user, password, dbname)
// _db, err := sql.Open("postgres", psqlconn)
// CheckError(err)
// return _db
// }
// func getAlbums(c *gin.Context) {
// stmt := `select * from albums`
// rows, err := db.Query(stmt)
// switch err {
// case sql.ErrNoRows:
// c.IndentedJSON(http.StatusBadRequest, gin.H{
// "message": "no records found",
// })
// case nil:
// defer rows.Close()
// a := make([]album, 0)
// var rowsReadErr bool
// for rows.Next() {
// var id int
// var title string
// var artist string
// var price float64
// err = rows.Scan(&id, &title, &artist, &price)
// if err != nil {
// rowsReadErr = true
// break
// }
// a = append(a, album{ID: fmt.Sprintf("%d", id), Title: title, Artist: artist, Price: price})
// }
// if rowsReadErr {
// return
// }
// c.IndentedJSON(http.StatusOK, a)
// default:
// defer rows.Close()
// }
// }
// func postAlbum(c *gin.Context) {
// var newAlbum album
// if err := c.BindJSON(&newAlbum); err != nil {
// return
// }
// stmt := `insert into albums ("title", "artist", "price") values($1, $2, $3) returning id`
// lastInsertId := 0
// db.QueryRow(stmt, newAlbum.Title, newAlbum.Artist, newAlbum.Price).Scan(&lastInsertId)
// newAlbum.ID = fmt.Sprintf("%d", lastInsertId)
// c.IndentedJSON(http.StatusCreated, newAlbum)
// }
// func deleteAll(c *gin.Context) {
// stmt := `delete from albums`
// res, err := db.Exec(stmt)
// if err != nil {
// c.IndentedJSON(http.StatusInternalServerError, gin.H{
// "message": "error deleting rows",
// })
// }
// n, err := res.RowsAffected()
// if err != nil {
// c.IndentedJSON(http.StatusInternalServerError, gin.H{
// "message": "error occurred while checking the returned result from database after deletion",
// })
// return
// }
// if n == 0 {
// c.IndentedJSON(http.StatusBadRequest, gin.H{
// "message": "could not delete recored, there might be no records in table",
// })
// return
// }
// c.IndentedJSON(http.StatusOK, gin.H{
// "message": "successfully deleted all records",
// })
// }
// func deleteAlbumById(c *gin.Context) {
// id, _ := strconv.Atoi(c.Param("id"))
// stmt := `delete from albums where id = $1`
// res, err := db.Exec(stmt, id)
// if err != nil {
// c.IndentedJSON(http.StatusInternalServerError, gin.H{
// "message": "error occurred while deleting artist record",
// })
// return
// }
// n, err := res.RowsAffected()
// if err != nil {
// c.IndentedJSON(http.StatusInternalServerError, gin.H{
// "message": "error occurred while checking the returned result from database after deletion",
// })
// return
// }
// if n == 0 {
// c.IndentedJSON(http.StatusBadRequest, gin.H{
// "message": "could not delete recored, there might be no records for such id",
// })
// return
// }
// c.IndentedJSON(http.StatusOK, gin.H{
// "message": "successfully deleted the record",
// })
// }
// func getAlbumByID(c *gin.Context) {
// id, _ := strconv.Atoi(c.Param("id"))
// stmt := `select * from albums where id = $1`
// row := db.QueryRow(stmt, id)
// var artist, title string
// var price float64
// err := row.Scan(&id, &title, &artist, &price)
// switch err {
// case sql.ErrNoRows:
// c.IndentedJSON(http.StatusBadRequest, gin.H{
// "message": "no records found",
// })
// case nil:
// c.IndentedJSON(http.StatusOK, album{
// ID: fmt.Sprintf("%d", id), Artist: artist, Title: title, Price: price,
// })
// default:
// c.IndentedJSON(http.StatusOK, gin.H{"message": "album not found"})
// }
// }
// func updateAlbum(c *gin.Context) {
// var modifiedAlbum album
// if err := c.ShouldBindJSON(&modifiedAlbum); err != nil {
// c.IndentedJSON(http.StatusNotFound, gin.H{"message": "invalid JSON body"})
// return
// }
// stmt := `update albums set title=$1, artist=$2, price=$3 where id=$4`
// res, err := db.Exec(stmt, modifiedAlbum.Title, modifiedAlbum.Artist, modifiedAlbum.Price, modifiedAlbum.ID)
// if err != nil {
// c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "error occurred while updating album"})
// return
// }
// n, _ := res.RowsAffected()
// if n == 0 {
// c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "could not update please after some time"})
// return
// }
// c.IndentedJSON(http.StatusOK, gin.H{"message": "update made successfully"})
// }
// const (
// host = "localhost"
// port = 5432
// user = "postgres"
// password = "justtheoldpassword"
// dbname = "albums_db"
// )
// func main() {
// defer db.Close()
// router := gin.Default()
// router.GET("/albums", getAlbums)
// router.POST("/albums", postAlbum)
// router.GET("/albums/:id", getAlbumByID)
// router.DELETE("albums/delete/:id", deleteAlbumById)
// router.PUT("albums/update/:id", updateAlbum)
// router.DELETE("albums", deleteAll)
// router.Run("192.168.9.101:9090")
// }