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

count.js: add comment count display JS

This commit is contained in:
Adhityaa Chandrasekar
2019-03-02 15:14:42 -05:00
parent 216016a4be
commit 15b1640f89
4 changed files with 132 additions and 11 deletions

View File

@ -1,27 +1,51 @@
package main
import (
"github.com/lib/pq"
"net/http"
)
func commentCount(domain string, path string) (int, error) {
// path can be empty
func commentCount(domain string, paths []string) (map[string]int, error) {
commentCounts := map[string]int{}
if domain == "" {
return 0, errorMissingField
return nil, errorMissingField
}
p, err := pageGet(domain, path)
if len(paths) == 0 {
return nil, errorEmptyPaths
}
statement := `
SELECT path, commentCount
FROM pages
WHERE domain = $1 AND path = ANY($2);
`
rows, err := db.Query(statement, domain, pq.Array(paths))
if err != nil {
return 0, errorInternal
logger.Errorf("cannot get comments: %v", err)
return nil, errorInternal
}
defer rows.Close()
for rows.Next() {
var path string
var commentCount int
if err = rows.Scan(&path, &commentCount); err != nil {
logger.Errorf("cannot scan path and commentCount: %v", err)
return nil, errorInternal
}
commentCounts[path] = commentCount
}
return p.CommentCount, nil
return commentCounts, nil
}
func commentCountHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
Domain *string `json:"domain"`
Path *string `json:"path"`
Domain *string `json:"domain"`
Paths *[]string `json:"paths"`
}
var x request
@ -31,13 +55,12 @@ func commentCountHandler(w http.ResponseWriter, r *http.Request) {
}
domain := domainStrip(*x.Domain)
path := *x.Path
count, err := commentCount(domain, path)
commentCounts, err := commentCount(domain, *x.Paths)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
bodyMarshal(w, response{"success": true, "count": count})
bodyMarshal(w, response{"success": true, "commentCounts": commentCounts})
}