domain_delete: delete moderator data

This commit is contained in:
Adhityaa 2018-06-03 20:57:08 +05:30
parent 79f39f8094
commit 13eca3c78a
2 changed files with 52 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package main
import ()
import (
"net/http"
)
func domainDelete(domain string) error {
if domain == "" {
@ -38,6 +40,16 @@ func domainDelete(domain string) error {
return errorInternal
}
statement = `
DELETE FROM moderators
WHERE moderators.domain = $1;
`
_, err = db.Exec(statement, domain)
if err != nil {
logger.Errorf("cannot delete domain moderators: %v", err)
return errorInternal
}
statement = `
DELETE FROM comments
WHERE comments.domain = $1;
@ -50,3 +62,41 @@ func domainDelete(domain string) error {
return nil
}
func domainDeleteHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
Session *string `json:"session"`
Domain *string `json:"domain"`
}
var x request
if err := unmarshalBody(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()})
return
}
o, err := ownerGetBySession(*x.Session)
if err != nil {
writeBody(w, response{"success": false, "message": err.Error()})
return
}
domain := stripDomain(*x.Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil {
writeBody(w, response{"success": false, "message": err.Error()})
return
}
if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
return
}
if err = domainDelete(*x.Domain); err != nil {
writeBody(w, response{"success": false, "message": err.Error()})
return
}
writeBody(w, response{"success": true})
}

View File

@ -13,6 +13,7 @@ func initAPIRouter(router *mux.Router) error {
router.HandleFunc("/api/owner/self", ownerSelfHandler).Methods("POST")
router.HandleFunc("/api/domain/new", domainNewHandler).Methods("POST")
router.HandleFunc("/api/domain/delete", domainDeleteHandler).Methods("POST")
router.HandleFunc("/api/domain/list", domainListHandler).Methods("POST")
router.HandleFunc("/api/domain/update", domainUpdateHandler).Methods("POST")
router.HandleFunc("/api/domain/moderator/new", domainModeratorNewHandler).Methods("POST")