-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (119 loc) · 2.98 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
package main
import (
"database/sql"
"flag"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"strconv"
)
var err error
type Laptop struct {
ID int `json:"id"`
Brand string `json:"brand"`
Model string `json:"model"`
}
func main() {
var (
host string
port int
user string
dbname string
sslmode string
password string
appPort int
)
flag.StringVar(&host, "host", "localhost", "Db host address")
flag.IntVar(&port, "port", 5432, "Host connection port")
flag.StringVar(&user, "user", "gorm", "The username of the database owner")
flag.StringVar(&dbname, "db", "laptop_db", "Database name on host")
flag.StringVar(&sslmode, "ssl", "disable", "SSL connection mode, default disable")
flag.StringVar(&password, "pwd", "gormPassword", "Password for the database user owner")
flag.IntVar(&appPort, "apprt", 8080, "Port for running LaptopAPI")
flag.Usage()
flag.Parse()
var CnString = "host=" + host + " port=" + strconv.Itoa(port) + " user=" + user + " dbname=" + dbname + " sslmode=" + sslmode + " password=" + password
db, err := sql.Open("postgres", CnString)
if err != nil {
panic(err)
}
defer db.Close()
r := gin.Default()
r.GET("/laptop/", func(c *gin.Context) {
selectAll := "SELECT * FROM laptops"
rows, _ := db.Query(selectAll)
all := []Laptop{}
var single Laptop
for rows.Next() {
var (
id int
model string
brand string
)
rows.Scan(&id, &brand, &model)
single = Laptop{
ID: id,
Brand: brand,
Model: model,
}
all = append(all, single)
}
if err != nil {
c.AbortWithStatus(404)
} else {
if len(all) == 0 {
c.JSON(404, "404 Not found!")
} else {
c.JSON(200, all)
}
}
})
r.GET("/laptop/:id", func(c *gin.Context) {
queryId := c.Params.ByName("id")
selectById := "SELECT * FROM laptops WHERE id = $1"
rows, err := db.Query(selectById, queryId)
defer rows.Close()
if err != nil {
c.AbortWithStatus(404)
} else {
var single Laptop
var (
id int
brand string
model string
)
rows.Next()
rows.Scan(&id, &brand, &model)
single = Laptop{
ID: id,
Brand: brand,
Model: model,
}
if single.ID == 0 {
c.JSON(404, "404 Not found")
} else {
c.JSON(200, single)
}
}
})
r.POST("/laptop/", func(c *gin.Context) {
var laptop Laptop
c.BindJSON(&laptop)
insert := "INSERT INTO laptops (id, brand, model) VALUES ( $1, $2, $3)"
db.Exec(insert, laptop.ID, laptop.Brand, laptop.Model)
c.JSON(201, laptop)
})
r.PUT("/laptop/:id", func(c *gin.Context) {
var laptop Laptop
c.BindJSON(&laptop)
update := "UPDATE laptops SET brand = $1, model = $2 WHERE id = $3"
db.Exec(update, laptop.Brand, laptop.Model, laptop.ID)
c.JSON(200, laptop)
})
r.DELETE("/laptop/:id", func(c *gin.Context) {
id := c.Params.ByName("id")
delete := "DELETE FROM laptops WHERE id = $1"
db.Exec(delete, id)
c.JSON(200, gin.H{"msg": "Element with id:" + id + " has been deleted"})
})
r.Run(":" + strconv.Itoa(appPort))
}