Skip to content

Commit

Permalink
Fix data.Search() not handling empty results.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jun 11, 2022
1 parent 83b232a commit 6feb199
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 2 additions & 6 deletions cmd/dictpress/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ func doSearch(c echo.Context) (data.Query, *results, error) {
}
res, total, err := app.data.Search(query)
if err != nil {
if err == sql.ErrNoRows {
return query, out, nil
}

app.logger.Printf("error querying db: %v", err)
return query, nil, errors.New("error querying db")
}
Expand All @@ -141,9 +137,9 @@ func doSearch(c echo.Context) (data.Query, *results, error) {
return query, nil, errors.New("error querying db for definitions")
}

out.Entries = res

pg.SetTotal(total)

out.Entries = res
out.Page = pg.Page
out.PerPage = pg.PerPage
out.TotalPages = pg.TotalPages
Expand Down
10 changes: 9 additions & 1 deletion internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ func (d *Data) Search(q Query) (Entries, int, error) {
pq.StringArray(q.Tags),
q.Status,
q.Offset, q.Limit,
); err != nil || len(out) == 0 {
); err != nil {
if err == sql.ErrNoRows {
return Entries{}, 0, nil
}

return nil, 0, err
}

if len(out) == 0 {
return Entries{}, 0, nil
}

// Replace nulls with [].
for i := range out {
if out[i].Relations == nil {
Expand Down

0 comments on commit 6feb199

Please sign in to comment.