llist/delete.go -rw-r--r-- 938 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)
}