Browse Source

GET categories returns total_unread & feed_count

kramanathan01 2 years ago
parent
commit
fa3de272e8
4 changed files with 17 additions and 8 deletions
  1. 10 2
      api/category.go
  2. 2 2
      model/category.go
  3. 1 0
      template/functions.go
  4. 4 4
      template/templates/views/categories.html

+ 10 - 2
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) {
 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 {
 	if err != nil {
 		json.ServerError(w, r, err)
 		json.ServerError(w, r, err)
 		return
 		return
 	}
 	}
-
 	json.OK(w, r, categories)
 	json.OK(w, r, categories)
 }
 }
 
 

+ 2 - 2
model/category.go

@@ -11,8 +11,8 @@ type Category struct {
 	Title        string `json:"title"`
 	Title        string `json:"title"`
 	UserID       int64  `json:"user_id"`
 	UserID       int64  `json:"user_id"`
 	HideGlobally bool   `json:"hide_globally"`
 	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 {
 func (c *Category) String() string {

+ 1 - 0
template/functions.go

@@ -107,6 +107,7 @@ func (f *funcMap) Map() template.FuncMap {
 		"nonce": func() string {
 		"nonce": func() string {
 			return crypto.GenerateRandomStringHex(16)
 			return crypto.GenerateRandomStringHex(16)
 		},
 		},
+		"deRef": func(i *int) int { return *i },
 
 
 		// These functions are overrode at runtime after the parsing.
 		// These functions are overrode at runtime after the parsing.
 		"elapsed": func(timezone string, t time.Time) string {
 		"elapsed": func(timezone string, t time.Time) string {

+ 4 - 4
template/templates/views/categories.html

@@ -15,7 +15,7 @@
 {{ else }}
 {{ else }}
     <div class="items">
     <div class="items">
         {{ range .categories }}
         {{ range .categories }}
-        <article role="article" class="item category-item {{if gt .TotalUnread 0 }} category-has-unread{{end}}">
+        <article role="article" class="item category-item {{if gt (deRef .TotalUnread) 0 }} category-has-unread{{end}}">
             <div class="item-header" dir="auto">
             <div class="item-header" dir="auto">
                 <span class="item-title">
                 <span class="item-title">
                     <a href="{{ route "categoryEntries" "categoryID" .ID }}">{{ .Title }}</a>
                     <a href="{{ route "categoryEntries" "categoryID" .ID }}">{{ .Title }}</a>
@@ -25,7 +25,7 @@
             <div class="item-meta">
             <div class="item-meta">
                 <ul class="item-meta-info">
                 <ul class="item-meta-info">
                     <li class="item-meta-info-feed-count">
                     <li class="item-meta-info-feed-count">
-                        {{ 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 }}
                     </li>
                     </li>
                 </ul>
                 </ul>
                 <ul class="item-meta-icons">
                 <ul class="item-meta-icons">
@@ -38,7 +38,7 @@
                     <li class="item-meta-icons-edit">
                     <li class="item-meta-icons-edit">
                         <a href="{{ route "editCategory" "categoryID" .ID }}">{{ icon "edit" }}<span class="icon-label">{{ t "menu.edit_category" }}</span></a>
                         <a href="{{ route "editCategory" "categoryID" .ID }}">{{ icon "edit" }}<span class="icon-label">{{ t "menu.edit_category" }}</span></a>
                     </li>
                     </li>
-                    {{ if eq .FeedCount 0 }}
+                    {{ if eq (deRef .FeedCount) 0 }}
                     <li class="item-meta-icons-delete">
                     <li class="item-meta-icons-delete">
                         <a href="#"
                         <a href="#"
                             data-confirm="true"
                             data-confirm="true"
@@ -49,7 +49,7 @@
                             data-url="{{ route "removeCategory" "categoryID" .ID }}">{{ icon "delete" }}<span class="icon-label">{{ t "action.remove" }}</span></a>
                             data-url="{{ route "removeCategory" "categoryID" .ID }}">{{ icon "delete" }}<span class="icon-label">{{ t "action.remove" }}</span></a>
                     </li>
                     </li>
                     {{ end }}
                     {{ end }}
-                    {{ if gt .TotalUnread 0 }}
+                    {{ if gt (deRef .TotalUnread) 0 }}
                       <li class="item-meta-icons-mark-as-read">
                       <li class="item-meta-icons-mark-as-read">
                         <a href="#"
                         <a href="#"
                             data-confirm="true"
                             data-confirm="true"