add skip to user's comment rest request #1085

This commit is contained in:
Umputun
2021-08-29 12:45:35 -05:00
parent be46e849a4
commit 8754add874
3 changed files with 49 additions and 22 deletions
+2 -2
View File
@@ -36,7 +36,7 @@ linters:
enable:
- bodyclose
- megacheck
- golint
- revive
- govet
- unconvert
- megacheck
@@ -76,4 +76,4 @@ issues:
exclude-use-default: false
service:
golangci-lint-version: 1.39.x
golangci-lint-version: 1.41.x
+10 -5
View File
@@ -227,17 +227,22 @@ func (s *public) commentByIDCtrl(w http.ResponseWriter, r *http.Request) {
}
}
// GET /comments?site=siteID&user=id - returns comments for given userID
// GET /comments?site=siteID&user=id&limit=123&skip=10 - returns comments for given userID
func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("user")
siteID := r.URL.Query().Get("site")
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = 0
getNumWithDef := func(key string) int {
res, err := strconv.Atoi(r.URL.Query().Get(key))
if err != nil {
res = 0
}
return res
}
limit, skip := getNumWithDef("limit"), getNumWithDef("skip")
resp := struct {
Comments []store.Comment `json:"comments,omitempty"`
Count int `json:"count,omitempty"`
@@ -247,7 +252,7 @@ func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
key := cache.NewKey(siteID).ID(URLKeyWithUser(r)).Scopes(userID, siteID)
data, err := s.cache.Get(key, func() ([]byte, error) {
comments, e := s.dataService.User(siteID, userID, limit, 0, rest.GetUserOrEmpty(r))
comments, e := s.dataService.User(siteID, userID, limit, skip, rest.GetUserOrEmpty(r))
if e != nil {
return nil, e
}
+37 -15
View File
@@ -397,13 +397,15 @@ func TestRest_FindUserComments(t *testing.T) {
c1 := store.Comment{Text: "test test #1",
Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah1"}}
c2 := store.Comment{Text: "test test #3", ParentID: "p1",
c2 := store.Comment{Text: "test test #2", ParentID: "p1",
Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah2"}}
c3 := store.Comment{Text: "test test #3", ParentID: "p1",
Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah3"}}
// add 3 comments
addComment(t, c1, ts)
addComment(t, c2, ts)
addComment(t, c2, ts)
addComment(t, c3, ts)
// add one deleted
id := addComment(t, c2, ts)
@@ -413,22 +415,42 @@ func TestRest_FindUserComments(t *testing.T) {
_, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=blah")
assert.Equal(t, 400, code, "noting for user blah")
res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev")
assert.Equal(t, 200, code)
{
res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev")
assert.Equal(t, 200, code)
resp := struct {
Comments []store.Comment
Count int
}{}
resp := struct {
Comments []store.Comment
Count int
}{}
err = json.Unmarshal([]byte(res), &resp)
assert.NoError(t, err)
require.Equal(t, 3, len(resp.Comments), "should have 3 comments")
assert.Equal(t, 4, resp.Count, "should have 3 count")
err = json.Unmarshal([]byte(res), &resp)
assert.NoError(t, err)
require.Equal(t, 3, len(resp.Comments), "should have 3 comments")
assert.Equal(t, 4, resp.Count, "should have 3+1 count") // TODO: fix as we start to skip deleted
// user comment sorted with -time
assert.True(t, resp.Comments[0].Timestamp.After(resp.Comments[1].Timestamp))
assert.True(t, resp.Comments[1].Timestamp.After(resp.Comments[2].Timestamp))
// user comment sorted with -time
assert.True(t, resp.Comments[0].Timestamp.After(resp.Comments[1].Timestamp))
assert.True(t, resp.Comments[1].Timestamp.After(resp.Comments[2].Timestamp))
}
{
res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev&skip=1&limit=2")
assert.Equal(t, 200, code)
resp := struct {
Comments []store.Comment
Count int
}{}
err = json.Unmarshal([]byte(res), &resp)
assert.NoError(t, err)
require.Equal(t, 2, len(resp.Comments), "should have 2 comments due to the limit")
assert.Equal(t, 4, resp.Count, "should have 4 count")
assert.Equal(t, "https://radio-t.com/blah3", resp.Comments[0].Locator.URL)
assert.Equal(t, "https://radio-t.com/blah2", resp.Comments[1].Locator.URL)
}
}
func TestRest_UserInfo(t *testing.T) {