API: Add the possibility to filter entries by category ID

This commit is contained in:
Frédéric Guillot 2020-09-12 21:40:13 -07:00
parent 04c4890124
commit d9f52bb634
1 changed files with 9 additions and 1 deletions

View File

@ -138,7 +138,15 @@ func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
return
}
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
userID := request.UserID(r)
categoryID := request.QueryInt64Param(r, "category_id", 0)
if categoryID > 0 && !h.store.CategoryExists(userID, categoryID) {
json.BadRequest(w, r, errors.New("Invalid category ID"))
return
}
builder := h.store.NewEntryQueryBuilder(userID)
builder.WithCategoryID(categoryID)
builder.WithStatuses(statuses)
builder.WithOrder(order)
builder.WithDirection(direction)