add counts to list

This commit is contained in:
Umputun
2018-01-08 19:35:42 -06:00
parent 1e938702d6
commit decf9c177b
4 changed files with 22 additions and 5 deletions
-1
View File
@@ -96,7 +96,6 @@ func (s *Server) Run() {
s.mod = admin{dataService: s.DataService, exporter: s.Exporter, respCache: s.respCache}
rauth.Mount("/admin", s.mod.routes())
})
})
// add robots and file server for static content from /web
+14 -2
View File
@@ -350,21 +350,33 @@ func (b *BoltDB) IsBlocked(siteID string, userID string) (blocked bool) {
}
// List returns list of buckets, which is list of all commented posts
func (b BoltDB) List(siteID string) (list []string, err error) {
func (b BoltDB) List(siteID string) (list []PostInfo, err error) {
bdb, err := b.db(siteID)
if err != nil {
return nil, err
}
posts := []string{}
err = bdb.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
if string(name) != lastBucketName && string(name) != userBucketName {
list = append(list, string(name))
posts = append(posts, string(name))
}
return nil
})
})
if err != nil {
return nil, errors.Wrapf(err, "failed to get list of posts for %s", siteID)
}
for _, post := range posts {
count, err := b.Count(Locator{SiteID: siteID, URL: post})
if err != nil {
return nil, errors.Wrapf(err, "failed to get count of comments for posts for %s", post)
}
list = append(list, PostInfo{URL: post, Count: count})
}
return list, err
}
+1 -1
View File
@@ -116,7 +116,7 @@ func TestBoltDB_List(t *testing.T) {
res, err := b.List("radio-t")
assert.Nil(t, err)
assert.Equal(t, []string{"https://radio-t.com", "https://radio-t.com/2"}, res)
assert.Equal(t, []PostInfo{{URL: "https://radio-t.com", Count: 2}, {URL: "https://radio-t.com/2", Count: 1}}, res)
}
func TestBoltDB_GetForUser(t *testing.T) {
+7 -1
View File
@@ -51,6 +51,12 @@ type Edit struct {
Summary string `json:"summary"`
}
// PostInfo holds summary for given post url
type PostInfo struct {
URL string `json:"url"`
Count int `json:"count"`
}
// Interface combines all store interfaces
type Interface interface {
Accessor
@@ -67,7 +73,7 @@ type Accessor interface {
GetByID(siteID string, commentID string) (Comment, error) // comment by id
GetByUser(siteID string, userID string) ([]Comment, error) // comment by user
Count(locator Locator) (int, error) // number of comments for the post
List(siteID string) ([]string, error) // list of commented posts
List(siteID string) ([]PostInfo, error) // list of commented posts
}
// Admin defines all store ops avail for admin only