From d53b98560390b720d0c0a6586204449785f5209a Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 11 Mar 2018 13:08:08 -0500 Subject: [PATCH] add count for multi posts --- app/rest/api/rest.go | 36 ++++++++++++++++++++++++++++++++++++ remark.rest | 10 ++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index be5a374c..b6c470ab 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -82,6 +82,7 @@ func (s *Rest) Run(port int) { rapi.Get("/comments", s.findUserCommentsCtrl) rapi.Get("/last/{max}", s.lastCommentsCtrl) rapi.Get("/count", s.countCtrl) + rapi.Post("/count", s.countMultiCtrl) rapi.Get("/list", s.listCtrl) rapi.Get("/config", s.configCtrl) rapi.Post("/preview", s.previewCommentCtrl) @@ -375,6 +376,41 @@ func (s *Rest) countCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, JSON{"count": count, "locator": locator}) } +// POST /count?site=siteID - get number of comments for posts from post body +func (s *Rest) countMultiCtrl(w http.ResponseWriter, r *http.Request) { + siteID := r.URL.Query().Get("site") + + posts := []string{} + + if err := render.DecodeJSON(r.Body, &posts); err != nil { + rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get list of posts from request") + return + } + + key := rest.URLKey(r) + strings.Join(posts, ",") + data, err := s.Cache.Get(key, 8*time.Hour, func() ([]byte, error) { + lst, e := s.DataService.List(siteID, 0, 0) + if e != nil { + return nil, e + } + res := []store.PostInfo{} + for _, l := range lst { + for _, p := range posts { + if p == l.URL { + res = append(res, l) + } + } + } + return encodeJSONWithHTML(res) + }) + + if err != nil { + rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get counts for "+siteID) + return + } + renderJSONFromBytes(w, r, data) +} + // GET /list?site=siteID&limit=50&skip=10 - list posts with comments func (s *Rest) listCtrl(w http.ResponseWriter, r *http.Request) { diff --git a/remark.rest b/remark.rest index 2d0caea0..25269420 100644 --- a/remark.rest +++ b/remark.rest @@ -62,6 +62,16 @@ GET {{host}}/api/v1/comments?site=remark&user=disqus_umputun ### get counts GET {{host}}/api/v1/count?site=remark&url=https://radio-t.com/p/2017/12/16/podcast-576/ +### get counts for many +POST {{host}}/api/v1/count?site=remark +Content-Type: application/json + +[ + "https://radio-t.com/p/2017/12/16/podcast-576/", + "https://radio-t.com/p/2017/12/16/podcast-577/", + "https://radio-t.com/p/2017/12/16/podcast-578/" +] + ### list commented posts GET {{host}}/api/v1/list?site=remark&limit=10&skip=5