Sfoglia il codice sorgente

API: Add the possibility to filter entries by category ID

Frédéric Guillot 5 anni fa
parent
commit
d9f52bb634
1 ha cambiato i file con 9 aggiunte e 1 eliminazioni
  1. 9 1
      api/entry.go

+ 9 - 1
api/entry.go

@@ -138,7 +138,15 @@ func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
 		return
 		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.WithStatuses(statuses)
 	builder.WithOrder(order)
 	builder.WithOrder(order)
 	builder.WithDirection(direction)
 	builder.WithDirection(direction)