commento/api/comment_count.go
Adhityaa Chandrasekar 8ebc0cd965 api: run go fmt
2018-09-23 01:12:52 -04:00

44 lines
840 B
Go

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})
}