commento/api/domain_delete.go

88 lines
1.8 KiB
Go
Raw Normal View History

package main
2018-06-03 23:27:08 +08:00
import (
"net/http"
)
func domainDelete(domain string) error {
if domain == "" {
return errorMissingField
}
statement := `
DELETE FROM
domains
WHERE domain = $1;
`
_, err := db.Exec(statement, domain)
if err != nil {
return errorNoSuchDomain
}
statement = `
DELETE FROM views
WHERE views.domain = $1;
`
_, err = db.Exec(statement, domain)
if err != nil {
logger.Errorf("cannot delete views: %v", err)
return errorInternal
}
2018-06-03 23:27:08 +08:00
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
}
// comments, votes, and pages are handled by domainClear
if err = domainClear(domain); err != nil {
logger.Errorf("cannot clear domain: %v", err)
return errorInternal
}
return nil
}
2018-06-03 23:27:08 +08:00
func domainDeleteHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
OwnerToken *string `json:"ownerToken"`
2018-06-20 11:50:11 +08:00
Domain *string `json:"domain"`
2018-06-03 23:27:08 +08:00
}
var x request
if err := bodyUnmarshal(r, &x); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
2018-06-03 23:27:08 +08:00
return
}
o, err := ownerGetByOwnerToken(*x.OwnerToken)
2018-06-03 23:27:08 +08:00
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
2018-06-03 23:27:08 +08:00
return
}
2018-07-24 15:00:45 +08:00
domain := domainStrip(*x.Domain)
2018-06-03 23:27:08 +08:00
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
2018-06-03 23:27:08 +08:00
return
}
if !isOwner {
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
2018-06-03 23:27:08 +08:00
return
}
if err = domainDelete(*x.Domain); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
2018-06-03 23:27:08 +08:00
return
}
bodyMarshal(w, response{"success": true})
2018-06-03 23:27:08 +08:00
}