add count to users comment

This commit is contained in:
Umputun
2018-01-15 01:50:14 -06:00
parent f8a69bdbef
commit f72a0c5ef7
3 changed files with 21 additions and 9 deletions
+13 -2
View File
@@ -78,6 +78,8 @@ func (s *Server) Run(port int) {
// api routes
router.Route("/api/v1", func(rapi chi.Router) {
// open routes
rapi.Get("/find", s.findCommentsCtrl)
rapi.Get("/id/{id}", s.commentByIDCtrl)
rapi.Get("/comments", s.findUserCommentsCtrl)
@@ -286,14 +288,23 @@ func (s *Server) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("user")
siteID := r.URL.Query().Get("site")
type resp struct {
Comments []store.Comment
Count int
}
log.Printf("[DEBUG] get comments for userID %s, %s", userID, siteID)
data, err := s.Cache.Get(r.URL.String(), time.Hour, func() ([]byte, error) {
comments, e := s.DataService.User(siteID, userID)
comments, count, e := s.DataService.User(siteID, userID)
if e != nil {
return nil, e
}
return encodeJSONWithHTML(comments)
rc := resp{
Comments: comments,
Count: count,
}
return encodeJSONWithHTML(rc)
})
if err != nil {
+7 -6
View File
@@ -32,7 +32,7 @@ const (
// limits
lastLimit = 1000
userLimit = 100
userLimit = 50
)
// BoltSite defines single site param
@@ -338,14 +338,14 @@ func (b BoltDB) List(siteID string) (list []PostInfo, err error) {
// User extracts all comments for given site and given userID
// "users" bucket has sub-bucket for each userID, and keeps it as ts:ref
func (b *BoltDB) User(siteID string, userID string) (comments []Comment, err error) {
func (b *BoltDB) User(siteID string, userID string) (comments []Comment, totalComments int, err error) {
comments = []Comment{}
commentRefs := []string{}
bdb, err := b.db(siteID)
if err != nil {
return nil, err
return nil, 0, err
}
// get list of references to comments
err = bdb.View(func(tx *bolt.Tx) error {
@@ -360,6 +360,7 @@ func (b *BoltDB) User(siteID string, userID string) (comments []Comment, err err
}
c := userBkt.Cursor()
totalComments = userBkt.Stats().KeyN
for k, v := c.Last(); k != nil; k, v = c.Prev() {
commentRefs = append(commentRefs, string(v))
if len(commentRefs) > userLimit {
@@ -370,21 +371,21 @@ func (b *BoltDB) User(siteID string, userID string) (comments []Comment, err err
})
if err != nil {
return comments, err
return comments, totalComments, err
}
// retrieve comments for refs
for _, v := range commentRefs {
url, commentID, e := ref{value: v}.parseValue()
if e != nil {
return comments, errors.Wrapf(e, "can't parse reference %s", v)
return comments, totalComments, errors.Wrapf(e, "can't parse reference %s", v)
}
if c, e := b.Get(Locator{SiteID: siteID, URL: url}, commentID); e == nil {
comments = append(comments, c)
}
}
return comments, err
return comments, totalComments, err
}
// Get for locator.URL and commentID string
+1 -1
View File
@@ -70,7 +70,7 @@ type Accessor interface {
Put(locator Locator, comment Comment) error // update comment, mutable parts only
Find(locator Locator, sort string) ([]Comment, error) // find comments for locator
Last(siteID string, max int) ([]Comment, error) // last comments for given site, sorted by time
User(siteID string, userID string) ([]Comment, error) // comments by user, sorted by time
User(siteID string, userID string) ([]Comment, int, error) // comments by user, sorted by time
Count(locator Locator) (int, error) // number of comments for the post
List(siteID string) ([]PostInfo, error) // list of commented posts
}