mirror of
https://github.com/documize/community.git
synced 2025-07-20 05:39:42 +02:00
Display category names in documents list (space view)
We pre-fetch category names and attach them to documents.
This commit is contained in:
parent
1d4a20cdfe
commit
5467771542
8 changed files with 804 additions and 722 deletions
|
@ -166,9 +166,6 @@ func (h *Handler) BySpace(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Sort by title.
|
||||
sort.Sort(doc.ByName(documents))
|
||||
|
||||
// Remove documents that cannot be seen due to lack of
|
||||
// category view/access permission.
|
||||
cats, err := h.Store.Category.GetBySpace(ctx, spaceID)
|
||||
|
@ -178,6 +175,30 @@ func (h *Handler) BySpace(w http.ResponseWriter, r *http.Request) {
|
|||
// Keep the latest version when faced with multiple versions.
|
||||
filtered = FilterLastVersion(filtered)
|
||||
|
||||
// Sort document list by ID.
|
||||
sort.Sort(doc.ByID(filtered))
|
||||
|
||||
// Attach category membership to each document.
|
||||
// Put category names into map for easier retrieval.
|
||||
catNames := make(map[string]string)
|
||||
for i := range cats {
|
||||
catNames[cats[i].RefID] = cats[i].Name
|
||||
}
|
||||
// Loop the smaller list which is categories assigned to documents.
|
||||
for i := range members {
|
||||
// Get name of category
|
||||
cn := catNames[members[i].CategoryID]
|
||||
// Find document that is assigned this category.
|
||||
j := sort.Search(len(filtered), func(k int) bool { return filtered[k].RefID <= members[i].DocumentID })
|
||||
// Attach category name to document
|
||||
if j < len(filtered) && filtered[j].RefID == members[i].DocumentID {
|
||||
filtered[j].Category = append(filtered[j].Category, cn)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort document list by title.
|
||||
sort.Sort(doc.ByName(filtered))
|
||||
|
||||
response.WriteJSON(w, filtered)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue