forked from devpranoy/CRUD-pygo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.go
126 lines (115 loc) · 2.99 KB
/
crud.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "password"
dbname = "postgres"
)
type event struct {
Name string
}
type message struct {
Message string
}
// CREATE
func create(w http.ResponseWriter, r *http.Request) {
var createEvent event
reqBody, err := ioutil.ReadAll(r.Body)
json.Unmarshal(reqBody, &createEvent)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
sqlStatement := `INSERT INTO data (name) VALUES ($1)`
_, err = db.Exec(sqlStatement, createEvent.Name)
if err != nil {
panic(err)
}
defer db.Close()
w.WriteHeader(http.StatusCreated)
var response message
response.Message = "Success"
json.NewEncoder(w).Encode(response)
}
// READ
func read(w http.ResponseWriter, r *http.Request) {
name := "None"
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
sqlStatement := `SELECT * FROM data `
err = db.QueryRow(sqlStatement).Scan(&name)
if err != nil {
panic(err)
}
defer db.Close()
var response message
response.Message = name
json.NewEncoder(w).Encode(response)
}
// UPDATE
func update(w http.ResponseWriter, r *http.Request) {
var createEvent event
reqBody, err := ioutil.ReadAll(r.Body)
json.Unmarshal(reqBody, &createEvent)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
sqlStatement := `UPDATE data SET name = ($1)`
_, err = db.Exec(sqlStatement, createEvent.Name)
if err != nil {
panic(err)
}
defer db.Close()
var response message
response.Message = "Success"
json.NewEncoder(w).Encode(response)
}
// DELETE
func delete(w http.ResponseWriter, r *http.Request) {
var createEvent event
reqBody, err := ioutil.ReadAll(r.Body)
json.Unmarshal(reqBody, &createEvent)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
sqlStatement := `DELETE FROM data WHERE name = ($1)`
_, err = db.Exec(sqlStatement, createEvent.Name)
if err != nil {
panic(err)
}
defer db.Close()
var response message
response.Message = "Success"
json.NewEncoder(w).Encode(response)
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/create", create).Methods("POST")
router.HandleFunc("/read", read).Methods("GET")
router.HandleFunc("/update", update).Methods("PUT")
router.HandleFunc("/delete", delete).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
}