-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (118 loc) · 3.06 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
}
var db *sql.DB
var err error
func main() {
db, err = sql.Open("mysql", "root:Phshappy14$@tcp(127.0.0.1:3306)/brightweb")
if err != nil {
panic(err.Error())
}
defer db.Close()
fmt.Println("running on port 8000")
router := mux.NewRouter()
router.HandleFunc("/posts", getPosts).Methods("GET")
router.HandleFunc("/posts", createPost).Methods("POST")
router.HandleFunc("/posts/{id}", getPost).Methods("GET")
router.HandleFunc("/posts/{id}", updatePost).Methods("PUT")
router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE")
http.ListenAndServe(":8000", router)
}
func getPosts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var posts []Post
result, err := db.Query("SELECT id, title from posts")
if err != nil {
panic(err.Error("error"))
}
defer result.Close()
for result.Next() {
var post Post
err := result.Scan(&post.ID, &post.Title)
if err != nil {
panic(err.Error())
}
posts = append(posts, post)
}
json.NewEncoder(w).Encode(posts)
}
func createPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
stmt, err := db.Prepare("INSERT INTO posts(title) VALUES(?)")
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
keyVal := make(map[string]string)
json.Unmarshal(body, &keyVal)
title := keyVal["title"]
_, err = stmt.Exec(title)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "New post was created")
}
func getPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
result, err := db.Query("SELECT id, title FROM posts WHERE id = ?", params["id"])
if err != nil {
panic(err.Error())
}
defer result.Close()
var post Post
for result.Next() {
err := result.Scan(&post.ID, &post.Title)
if err != nil {
panic(err.Error())
}
}
json.NewEncoder(w).Encode(post)
}
func updatePost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
stmt, err := db.Prepare("UPDATE posts SET title = ? WHERE id = ?")
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
keyVal := make(map[string]string)
json.Unmarshal(body, &keyVal)
newTitle := keyVal["title"]
_, err = stmt.Exec(newTitle, params["id"])
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "Post with ID = %s was updated", params["id"])
}
func deletePost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
stmt, err := db.Prepare("DELETE FROM posts WHERE id = ?")
if err != nil {
panic(err.Error())
}
_, err = stmt.Exec(params["id"])
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "Post with ID = %s was deleted", params["id"])
}