package main
import (
"net/http"
"strconv"
)
func delete(rw http.ResponseWriter, req *http.Request) {
name, pass, ok := req.BasicAuth()
if !ok || !checkPasswd(name, pass) {
rw.Header().Set("WWW-Authenticate", "Basic realm=\"Credentials\"")
http.Error(rw, "Invalid password", http.StatusUnauthorized)
return
}
ty := req.URL.Query().Get("t") // type
id, err := strconv.Atoi(req.URL.Query().Get("id"))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
switch ty {
case "post":
if _, err = db.Exec(DE_LINK, id); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
case "comm":
if _, err = db.Exec(DE_COMM, id); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(rw, "No such post type: "+ty, http.StatusBadRequest)
return
}
http.Redirect(rw, req, "../", http.StatusFound)
}