From d9f52bb63436ed54ae92f0106cc200d2b6ff0ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 12 Sep 2020 21:40:13 -0700 Subject: [PATCH] API: Add the possibility to filter entries by category ID --- api/entry.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/api/entry.go b/api/entry.go index 9fcfa603..4ceeadca 100644 --- a/api/entry.go +++ b/api/entry.go @@ -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)