|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + _ "github.com/go-sql-driver/mysql" |
| 11 | + "github.com/gorilla/mux" |
| 12 | +) |
| 13 | + |
| 14 | +//Tag structure for your database |
| 15 | +type User struct { |
| 16 | + ID int `json:"id"` |
| 17 | + Name string `json:"name"` |
| 18 | + Country string `json:"country"` |
| 19 | + Number string `json:"number"` |
| 20 | +} |
| 21 | + |
| 22 | +var db *sql.DB |
| 23 | +var err error |
| 24 | + |
| 25 | +// function to open connection to mysql database |
| 26 | +func dbConn() (db *sql.DB) { |
| 27 | + dbDriver := "mysql" |
| 28 | + dbUser := "root" |
| 29 | + dbPass := "" // your password, leave it like this if there is no password |
| 30 | + dbName := "usertest" // your database name |
| 31 | + dbIP := "127.0.0.1:3306" |
| 32 | + db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@tcp("+dbIP+")/"+dbName) |
| 33 | + if err != nil { |
| 34 | + panic(err.Error()) |
| 35 | + } |
| 36 | + return db |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +//View all the records |
| 41 | +func Index(w http.ResponseWriter, r *http.Request) { |
| 42 | + w.Header().Set("Content-Type", "application/json") |
| 43 | + db := dbConn() |
| 44 | + |
| 45 | + var users []User |
| 46 | + |
| 47 | + result, err := db.Query("SELECT * FROM users") |
| 48 | + if err != nil { |
| 49 | + panic(err.Error()) |
| 50 | + } |
| 51 | + defer result.Close() |
| 52 | + |
| 53 | + for result.Next() { |
| 54 | + var post User |
| 55 | + err = result.Scan(&post.ID, &post.Name, &post.Country, &post.Number) |
| 56 | + if err != nil { |
| 57 | + panic(err.Error()) |
| 58 | + } |
| 59 | + |
| 60 | + users = append(users, post) |
| 61 | + } |
| 62 | + json.NewEncoder(w).Encode(users) |
| 63 | + defer db.Close() |
| 64 | +} |
| 65 | + |
| 66 | +func insertUser(w http.ResponseWriter, r *http.Request) { |
| 67 | + w.Header().Set("Content-Type", "application/json") |
| 68 | + db := dbConn() |
| 69 | + vars := mux.Vars(r) |
| 70 | + Name := vars["name"] |
| 71 | + Country := vars["country"] |
| 72 | + Number := vars["number"] |
| 73 | + |
| 74 | + // perform a db.Query insert |
| 75 | + stmt, err := db.Prepare("INSERT INTO users(name, country, number) VALUES(?,?,?)") |
| 76 | + if err != nil { |
| 77 | + panic(err.Error()) |
| 78 | + } |
| 79 | + _, err = stmt.Exec(Name, Country, Number) |
| 80 | + if err != nil { |
| 81 | + panic(err.Error()) |
| 82 | + } |
| 83 | + fmt.Fprintf(w, "New user was created") |
| 84 | + defer db.Close() |
| 85 | +} |
| 86 | +func getUser(w http.ResponseWriter, r *http.Request) { |
| 87 | + w.Header().Set("Content-Type", "application/json") |
| 88 | + db := dbConn() |
| 89 | + params := mux.Vars(r) |
| 90 | + |
| 91 | + // perform a db.Query insert |
| 92 | + stmt, err := db.Query("SELECT * FROM users WHERE id = ?", params["id"]) |
| 93 | + if err != nil { |
| 94 | + panic(err.Error()) |
| 95 | + } |
| 96 | + defer stmt.Close() |
| 97 | + var post User |
| 98 | + for stmt.Next() { |
| 99 | + |
| 100 | + err = stmt.Scan(&post.ID, &post.Name, &post.Country, &post.Number) |
| 101 | + if err != nil { |
| 102 | + panic(err.Error()) |
| 103 | + } |
| 104 | + } |
| 105 | + json.NewEncoder(w).Encode(post) |
| 106 | + defer db.Close() |
| 107 | +} |
| 108 | +func delUser(w http.ResponseWriter, r *http.Request) { |
| 109 | + w.Header().Set("Content-Type", "application/json") |
| 110 | + db := dbConn() |
| 111 | + params := mux.Vars(r) |
| 112 | + |
| 113 | + // perform a db.Query insert |
| 114 | + stmt, err := db.Prepare("DELETE FROM users WHERE id = ?") |
| 115 | + if err != nil { |
| 116 | + panic(err.Error()) |
| 117 | + } |
| 118 | + _, err = stmt.Exec(params["id"]) |
| 119 | + fmt.Fprintf(w, "User with ID = %s was deleted", params["id"]) |
| 120 | + defer db.Close() |
| 121 | +} |
| 122 | + |
| 123 | +func updateUser(w http.ResponseWriter, r *http.Request) { |
| 124 | + w.Header().Set("Content-Type", "application/json") |
| 125 | + db := dbConn() |
| 126 | + params := mux.Vars(r) |
| 127 | + Name := params["name"] |
| 128 | + Country := params["country"] |
| 129 | + Number := params["number"] |
| 130 | + |
| 131 | + // perform a db.Query insert |
| 132 | + stmt, err := db.Prepare("Update users SET name = ?, country = ?, number = ? WHERE id = ?") |
| 133 | + if err != nil { |
| 134 | + panic(err.Error()) |
| 135 | + } |
| 136 | + _, err = stmt.Exec(Name, Country, Number, params["id"]) |
| 137 | + if err != nil { |
| 138 | + panic(err.Error()) |
| 139 | + } |
| 140 | + fmt.Fprintf(w, "User with ID = %s was updated", params["id"]) |
| 141 | + defer db.Close() |
| 142 | +} |
| 143 | +func main() { |
| 144 | + log.Println("Server started on: http://localhost:8080") |
| 145 | + router := mux.NewRouter() |
| 146 | + //On postman try http://localhost:8080/all with method GET |
| 147 | + router.HandleFunc("/all", Index).Methods("GET") |
| 148 | + //On postman try http://localhost:8080/add?name=Test&country=LEB&number=7777777 with metho POST |
| 149 | + router.HandleFunc("/add", insertUser).Methods("POST").Queries("name", "{name}", "country", "{country}", "number", "{number}") |
| 150 | + //On postman try http://localhost:8080/get/1 with method GET |
| 151 | + router.HandleFunc("/get/{id}", getUser).Methods("GET") |
| 152 | + //On postman try http://localhost:8080/update/1 with method PUT |
| 153 | + router.HandleFunc("/update/{id}", updateUser).Methods("PUT").Queries("name", "{name}", "country", "{country}", "number", "{number}") |
| 154 | + //On postman try http://localhost:8080/del/1 with method DELETE |
| 155 | + router.HandleFunc("/del/{id}", delUser).Methods("DELETE") |
| 156 | + |
| 157 | + http.ListenAndServe(":8080", router) |
| 158 | + |
| 159 | +} |
0 commit comments