delete works and with test

This commit is contained in:
2025-07-30 20:29:24 -04:00
parent 379eb6e206
commit e92644cf72
10 changed files with 427 additions and 36 deletions

52
web.go Normal file
View 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)
}
}