add delete quote method
This commit is contained in:
+19
-3
@@ -1,4 +1,4 @@
|
||||
// TODO: READ AND UNDERSTAND + ALTER
|
||||
// TODO: READ AND UNDERSTAND
|
||||
|
||||
package main
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
// GET /quotes
|
||||
func getQuotes(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := db.Query(context.Background(), "SELECT * FROM hlaskovnik")
|
||||
rows, err := db.Query(context.Background(), "SELECT * FROM hlaskovnik;")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
@@ -41,7 +41,7 @@ func createQuote(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = db.QueryRow(
|
||||
context.Background(),
|
||||
"INSERT INTO hlaskovnik(author, body) VALUES($1, $2) RETURNING id",
|
||||
"INSERT INTO hlaskovnik(author, body) VALUES($1, $2) RETURNING id;",
|
||||
q.Author, q.Body,
|
||||
).Scan(&q.Id)
|
||||
|
||||
@@ -53,3 +53,19 @@ func createQuote(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(q)
|
||||
}
|
||||
|
||||
// DELETE /quotes
|
||||
func deleteQuote(w http.ResponseWriter, r *http.Request) {
|
||||
var q Quote
|
||||
err := json.NewDecoder(r.Body).Decode(&q)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid JSON", 400)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.QueryRow(
|
||||
context.Background(),
|
||||
"DELETE FROM hlaskovnik WHERE id = $1 RETURNING *;",
|
||||
q.Id,
|
||||
).Scan(&q.Id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user