delete works and with test
This commit is contained in:
52
web.go
Normal file
52
web.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func handleMain(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
|
||||
tmpl, err := template.ParseFS(content, "templates/*.html")
|
||||
if err != nil {
|
||||
log.Fatalf("Error parsing templates: %v", err)
|
||||
}
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
data := db.GetAll()
|
||||
err := tmpl.ExecuteTemplate(w, "index.html", data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleDelete(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
q := req.URL.Query()
|
||||
vals, ok := q["id"]
|
||||
if ok {
|
||||
id := vals[0]
|
||||
noodle := db.Get(id)
|
||||
noodle.IsUp = false
|
||||
*pc <- noodle
|
||||
log.Printf("Deleting noodle=%v", noodle)
|
||||
//log.Printf("Deleting noodle=%s", id)
|
||||
db.Delete(noodle.Id)
|
||||
}
|
||||
|
||||
http.Redirect(w, req, "/", 307)
|
||||
}
|
||||
}
|
||||
|
||||
func handleAdd(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if req.Method != "POST" {
|
||||
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, req, "/", 307)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user