commento/api/domain_delete.go

103 lines
2.0 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 votes
USING comments
WHERE comments.commentHex = votes.commentHex AND comments.domain = $1;
`
_, err = db.Exec(statement, domain)
if err != nil {
logger.Errorf("cannot delete votes: %v", err)
return errorInternal
}
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
}
statement = `
DELETE FROM comments
WHERE comments.domain = $1;
`
_, err = db.Exec(statement, domain)
if err != nil {
logger.Errorf(statement, domain)
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
}