-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (105 loc) · 2.72 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
type studentinfo struct {
Sid string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Course string `json:"course,omitempty"`
}
func getMYsqlDB() *sql.DB {
db, err := sql.Open("mysql", "root:@(127.0.0.1:3306)/studentinfo?parseTime=true")
if err != nil {
log.Fatal(err)
}
return db
}
func getstudent(w http.ResponseWriter, r *http.Request) {
db := getMYsqlDB()
defer db.Close()
ss := []studentinfo{}
s := studentinfo{}
rows, err := db.Query("select * from student")
if err != nil {
fmt.Fprint(w, err)
} else {
for rows.Next() {
rows.Scan(&s.Sid, &s.Name, &s.Course)
ss = append(ss, s)
}
json.NewEncoder(w).Encode(ss)
}
fmt.Fprint(w, "GET the student")
}
func deletestudent(w http.ResponseWriter, r *http.Request) {
db := getMYsqlDB()
defer db.Close()
parms := mux.Vars(r)
sid, _ := strconv.Atoi(parms["sid"])
result, err := db.Exec("delete from student where sid=?", sid)
if err != nil {
fmt.Fprint(w, err)
} else {
_, err := result.RowsAffected()
if err != nil {
fmt.Print(w, err)
} else {
fmt.Fprint(w, "Suscesssful")
}
}
fmt.Fprint(w, "delete the student")
}
func updatestudent(w http.ResponseWriter, r *http.Request) {
db := getMYsqlDB()
defer db.Close()
s := studentinfo{}
json.NewDecoder(r.Body).Decode(&s)
parms := mux.Vars(r)
sid, _ := strconv.Atoi(parms["sid"])
result, err := db.Exec("update student set name=?,course=?,where sid=?", sid, s.Name, s.Course)
if err != nil {
fmt.Fprint(w, err)
} else {
_, err := result.RowsAffected()
if err != nil {
json.NewEncoder(w).Encode("error")
} else {
json.NewEncoder(w).Encode(s)
}
}
fmt.Fprint(w, "update the student")
}
func addstudent(w http.ResponseWriter, r *http.Request) {
db := getMYsqlDB()
defer db.Close()
s := studentinfo{}
json.NewDecoder(r.Body).Decode(s)
sid, _ := strconv.Atoi(s.Sid)
result, err := db.Exec("insert into student(sid, name ,course)value(?,?,?)", sid, s.Name, s.Course)
if err != nil {
fmt.Fprint(w, err)
} else {
_, err := result.LastInsertId()
if err != nil {
json.NewEncoder(w).Encode("{No record is inserted}")
} else {
json.NewEncoder(w).Encode(s)
}
}
fmt.Fprint(w, "add the student")
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/students", getstudent).Methods("GET")
r.HandleFunc("/students", addstudent).Methods("POST")
r.HandleFunc("/students/{Sid}", updatestudent).Methods("PUT")
r.HandleFunc("/students/{Sid}", deletestudent).Methods("DELETE")
http.ListenAndServe(":8080", r)
}