From fa3de272e84ff7a7c6fed2d81edfaf20c38cfa79 Mon Sep 17 00:00:00 2001 From: kramanathan01 <5899848+kramanathan01@users.noreply.github.com> Date: Mon, 19 Jun 2023 09:50:08 -0700 Subject: [PATCH] GET categories returns total_unread & feed_count --- api/category.go | 12 ++++++++++-- model/category.go | 4 ++-- template/functions.go | 1 + template/templates/views/categories.html | 8 ++++---- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/api/category.go b/api/category.go index 3f4723a2..f35b0295 100644 --- a/api/category.go +++ b/api/category.go @@ -97,12 +97,20 @@ func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) { } func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) { - categories, err := h.store.Categories(request.UserID(r)) + var categories model.Categories + var err error + includeCounts := request.QueryStringParam(r, "counts", "false") + + if includeCounts == "true" { + categories, err = h.store.CategoriesWithFeedCount(request.UserID(r)) + } else { + categories, err = h.store.Categories(request.UserID(r)) + } + if err != nil { json.ServerError(w, r, err) return } - json.OK(w, r, categories) } diff --git a/model/category.go b/model/category.go index a66e63db..ed223286 100644 --- a/model/category.go +++ b/model/category.go @@ -11,8 +11,8 @@ type Category struct { Title string `json:"title"` UserID int64 `json:"user_id"` HideGlobally bool `json:"hide_globally"` - FeedCount int `json:"-"` - TotalUnread int `json:"-"` + FeedCount *int `json:"feed_count,omitempty"` + TotalUnread *int `json:"total_unread,omitempty"` } func (c *Category) String() string { diff --git a/template/functions.go b/template/functions.go index a3f5b341..0a3a5657 100644 --- a/template/functions.go +++ b/template/functions.go @@ -107,6 +107,7 @@ func (f *funcMap) Map() template.FuncMap { "nonce": func() string { return crypto.GenerateRandomStringHex(16) }, + "deRef": func(i *int) int { return *i }, // These functions are overrode at runtime after the parsing. "elapsed": func(timezone string, t time.Time) string { diff --git a/template/templates/views/categories.html b/template/templates/views/categories.html index 34655aa0..c1d30e3f 100644 --- a/template/templates/views/categories.html +++ b/template/templates/views/categories.html @@ -15,7 +15,7 @@ {{ else }}
{{ range .categories }} -
+
{{ .Title }} @@ -25,7 +25,7 @@
  • - {{ if eq .FeedCount 0 }}{{ t "page.categories.no_feed" }}{{ else }}{{ plural "page.categories.feed_count" .FeedCount .FeedCount }}{{ end }} + {{ if eq (deRef .FeedCount) 0 }}{{ t "page.categories.no_feed" }}{{ else }}{{ plural "page.categories.feed_count" (deRef .FeedCount) (deRef .FeedCount) }}{{ end }}