api: add option to delete own comments
This commit is contained in:
@@ -41,6 +41,12 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
cm, err := commentGetByCommentHex(*x.CommentHex)
|
||||
if err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain, _, err := commentDomainPathGet(*x.CommentHex)
|
||||
if err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
@@ -53,7 +59,7 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !isModerator {
|
||||
if !isModerator && cm.CommenterHex != c.CommenterHex {
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
return
|
||||
}
|
||||
|
41
api/comment_get.go
Normal file
41
api/comment_get.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
)
|
||||
|
||||
func commentGetByCommentHex(commentHex string) (comment, error) {
|
||||
if commentHex == "" {
|
||||
return comment{}, errorMissingField
|
||||
}
|
||||
|
||||
statement := `
|
||||
SELECT
|
||||
commentHex,
|
||||
commenterHex,
|
||||
markdown,
|
||||
html,
|
||||
parentHex,
|
||||
score,
|
||||
state,
|
||||
creationDate
|
||||
FROM comments
|
||||
WHERE comments.commentHex = $1;
|
||||
`
|
||||
row := db.QueryRow(statement, commentHex)
|
||||
|
||||
c := comment{}
|
||||
if err := row.Scan(
|
||||
&c.CommentHex,
|
||||
&c.CommenterHex,
|
||||
&c.Markdown,
|
||||
&c.Html,
|
||||
&c.ParentHex,
|
||||
&c.Score,
|
||||
&c.State,
|
||||
&c.CreationDate); err != nil {
|
||||
// TODO: is this the only error?
|
||||
return c, errorNoSuchComment
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
Reference in New Issue
Block a user