/
adminDelete.go
66 lines (51 loc) · 1.51 KB
/
adminDelete.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"net/http"
. "github.com/Deleplace/programming-idioms/idioms"
"google.golang.org/appengine/log"
)
func idiomDelete(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
idiomIDStr := r.FormValue("idiomId")
idiomID := String2Int(idiomIDStr)
why := r.FormValue("why")
if why == "" {
why = fmt.Sprintf("Admin deletes idiom %d", idiomID)
}
err := dao.deleteIdiom(ctx, idiomID, why)
htmlCacheEvict(ctx, "/about-block-all-idioms")
w.Header().Set("Content-Type", "application/json")
if err != nil {
// fmt.Fprint(w, Response{"success": false, "message": err.Error()})
return err
}
fmt.Fprint(w, Response{"success": true})
return nil
}
func implDelete(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
idiomIDStr := r.FormValue("idiomId")
idiomID := String2Int(idiomIDStr)
implIDStr := r.FormValue("implId")
implID := String2Int(implIDStr)
// Answer to the "Why?" prompt on delete
reason := r.FormValue("reason")
why := r.FormValue("why")
if why == "" {
why = fmt.Sprintf("Admin deletes impl %d: %s", implID, reason)
}
err := dao.deleteImpl(ctx, idiomID, implID, why)
err2 := unindexImpl(ctx, idiomID, implID)
if err2 != nil {
log.Errorf(ctx, "Unindexing impl %d from idiom %d: %v", implID, idiomID, err2)
// But keep going
}
w.Header().Set("Content-Type", "application/json")
if err != nil {
// fmt.Fprint(w, Response{"success": false, "message": err.Error()})
return err
}
fmt.Fprint(w, Response{"success": true})
return nil
}