From 13eca3c78af198cdacb7944602316479324c74e5 Mon Sep 17 00:00:00 2001 From: Adhityaa Date: Sun, 3 Jun 2018 20:57:08 +0530 Subject: [PATCH] domain_delete: delete moderator data --- api/domain_delete.go | 52 +++++++++++++++++++++++++++++++++++++++++++- api/router_api.go | 1 + 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/api/domain_delete.go b/api/domain_delete.go index 2689c99..b8be4a0 100644 --- a/api/domain_delete.go +++ b/api/domain_delete.go @@ -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}) +} diff --git a/api/router_api.go b/api/router_api.go index 729c3c0..2935390 100644 --- a/api/router_api.go +++ b/api/router_api.go @@ -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")