1
mirror of https://gitlab.com/commento/commento.git synced 2025-06-29 22:56:37 -04:00

api,db: add comments count endpoint

Closes https://gitlab.com/commento/commento-ce/issues/27
This commit is contained in:
Adhityaa Chandrasekar
2018-09-23 00:40:06 -04:00
parent 299649cea2
commit 330131f390
6 changed files with 118 additions and 2 deletions

43
api/comment_count.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"net/http"
)
func commentCount(domain string, path string) (int, error) {
// path can be empty
if domain == "" {
return 0, errorMissingField
}
p, err := pageGet(domain, path)
if err != nil {
return 0, errorInternal
}
return p.CommentCount, nil
}
func commentCountHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
Domain *string `json:"domain"`
Path *string `json:"path"`
}
var x request
if err := bodyUnmarshal(r, &x); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
domain := domainStrip(*x.Domain)
path := *x.Path
count, err := commentCount(domain, path)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
bodyMarshal(w, response{"success": true, "count": count})
}