Last active
November 21, 2019 12:10
-
-
Save kira924age/8962ed2e0202ff0672a1903ef51993f4 to your computer and use it in GitHub Desktop.
booklet2019b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/go-chi/chi" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
type Question struct { | |
Id int `json:"id"` | |
Title string `json:"title"` | |
Body string `json:"body"` | |
Category string `json:"category"` | |
Created_at string `json:"created_at"` | |
} | |
var Db *sql.DB | |
func init() { | |
var err error | |
Db, err = sql.Open("mysql", "user:dbpassword@/heap_overflow") | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
defer Db.Close() | |
r := chi.NewRouter() | |
r.Route("/questions", func(r chi.Router) { | |
r.Get("/", getAllQuestions) // GET /questions/ | |
r.Post("/", createQuestion) // POST /questions | |
r.Route("/{questionID}", func(r chi.Router) { | |
r.Get("/", getQuestion) // GET /questions/{qustionID} | |
r.Delete("/", deleteQuestion) // DELETE /questions{questionID} | |
r.Put("/", updateQuestion) // PUT /questions{questionID} | |
}) | |
}) | |
http.ListenAndServe(":3000", r) | |
} | |
func updateQuestion(w http.ResponseWriter, r *http.Request) { | |
questionID := chi.URLParam(r, "questionID") | |
var q Question | |
err := json.NewDecoder(r.Body).Decode(&q) | |
if err != nil { | |
log.Fatal(err) | |
} | |
stmt, err := Db.Prepare("UPDATE `questions` set title=?, body=?, category=? WHERE id=?") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer stmt.Close() | |
_, err = stmt.Exec(q.Title, q.Body, q.Category, questionID) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func deleteQuestion(w http.ResponseWriter, r *http.Request) { | |
questionID := chi.URLParam(r, "questionID") | |
rows, err := Db.Query("DELETE FROM questions WHERE id = ?", questionID) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer rows.Close() | |
} | |
func createQuestion(w http.ResponseWriter, r *http.Request) { | |
var q Question | |
err := json.NewDecoder(r.Body).Decode(&q) | |
if err != nil { | |
log.Fatal(err) | |
} | |
stmt, err := Db.Prepare("INSERT INTO `questions`(`title`, `body`, `category`, `created_at`) VALUES(?, ?, ?, NOW())") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer stmt.Close() | |
_, err = stmt.Exec(q.Title, q.Body, q.Category) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func getAllQuestions(w http.ResponseWriter, r *http.Request) { | |
rows, err := Db.Query("SELECT * FROM questions") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer rows.Close() | |
var id int | |
var title, body, category, created_at string | |
var jsonStr string | |
qs := make([]Question, 0) | |
for rows.Next() { | |
err := rows.Scan(&id, &category, &title, &body, &created_at) | |
if err != nil { | |
log.Fatal(err) | |
} | |
q := Question{} | |
q.Id = id | |
q.Title = title | |
q.Body = body | |
q.Category = category | |
q.Created_at = created_at | |
qs = append(qs, q) | |
} | |
jsonBytes, err := json.MarshalIndent(qs, "", " ") | |
if err != nil { | |
log.Fatal(err) | |
} | |
jsonStr = string(jsonBytes) | |
w.Write([]byte(fmt.Sprintf(jsonStr))) | |
} | |
func getQuestion(w http.ResponseWriter, r *http.Request) { | |
questionID := chi.URLParam(r, "questionID") | |
q := Question{} | |
err := Db.QueryRow("SELECT * FROM questions WHERE id = ?", questionID).Scan(&(q.Id), &(q.Category), &(q.Title), &(q.Body), &(q.Created_at)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
jsonBytes, err := json.MarshalIndent(q, "", " ") | |
if err != nil { | |
log.Fatal(err) | |
} | |
jsonStr := string(jsonBytes) | |
w.Write([]byte(fmt.Sprintf(jsonStr))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment