Skip to content

Commit

Permalink
return 200 and empty list on user comments if nothing #1265
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Feb 10, 2022
1 parent 74b47d3 commit 7b47bd2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions backend/app/rest/api/rest_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
limit, skip := getNumWithDef("limit"), getNumWithDef("skip")

resp := struct {
Comments []store.Comment `json:"comments,omitempty"`
Count int `json:"count,omitempty"`
Comments []store.Comment `json:"comments"`
Count int `json:"count"`
}{}

log.Printf("[DEBUG] get comments for userID %s, %s", userID, siteID)
Expand All @@ -220,6 +220,10 @@ func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
data, err := s.cache.Get(key, func() ([]byte, error) {
comments, e := s.dataService.User(siteID, userID, limit, skip, rest.GetUserOrEmpty(r))
if e != nil {
if strings.Contains(e.Error(), "no comments for user") { // store returns this error when no comments found
resp.Comments, resp.Count = []store.Comment{}, 0
return encodeJSONWithHTML(resp)
}
return nil, e
}
comments = filterComments(comments, func(c store.Comment) bool { return !c.Deleted })
Expand All @@ -235,7 +239,6 @@ func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) {
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get comment by user id", rest.ErrCommentNotFound)
return
}

if err = R.RenderJSONFromBytes(w, r, data); err != nil {
log.Printf("[WARN] can't render found comments for user %s", userID)
}
Expand Down
6 changes: 3 additions & 3 deletions backend/app/rest/api/rest_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ func TestRest_FindUserComments(t *testing.T) {
err := srv.DataService.Delete(c2.Locator, id, store.SoftDelete)
assert.NoError(t, err)

_, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=blah")
assert.Equal(t, 400, code, "noting for user blah")

comments, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=blah")
assert.Equal(t, 200, code, "noting for user blah")
assert.Equal(t, `{"comments":[],"count":0}`+"\n", comments)
{
res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev")
assert.Equal(t, 200, code)
Expand Down

0 comments on commit 7b47bd2

Please sign in to comment.