diff options
Diffstat (limited to 'internal/handlers/shopping.go')
| -rw-r--r-- | internal/handlers/shopping.go | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/internal/handlers/shopping.go b/internal/handlers/shopping.go index e06cad6..69cb2b7 100644 --- a/internal/handlers/shopping.go +++ b/internal/handlers/shopping.go @@ -241,17 +241,39 @@ func (h *Handler) aggregateShoppingLists(ctx context.Context) []models.ShoppingS } unified := models.UnifiedShoppingItem{ - ID: fmt.Sprintf("user-%d", item.ID), - Name: item.Name, - Store: storeName, - Source: "user", - Checked: item.Checked, + ID: fmt.Sprintf("user-%d", item.ID), + Name: item.Name, + Category: item.Section, + Store: storeName, + Source: "user", + Checked: item.Checked, } - storeMap[storeName][""] = append(storeMap[storeName][""], unified) + storeMap[storeName][item.Section] = append(storeMap[storeName][item.Section], unified) } } - // 3. Convert map to sorted slice + // 3. Every registered store appears even when empty. Without this you + // cannot move an item into a store that has nothing in it yet, and moving + // the last item out of a store makes it vanish mid-edit. + registered, err := h.store.GetShoppingStores() + if err != nil { + log.Printf("ERROR [Shopping/Stores]: %v", err) + } else { + for _, st := range registered { + if storeMap[st.Name] == nil { + storeMap[st.Name] = make(map[string][]models.UnifiedShoppingItem) + } + // Likewise for sections: an empty section still needs to be a + // destination in the edit form. + for _, sec := range st.Sections { + if _, ok := storeMap[st.Name][sec.Name]; !ok { + storeMap[st.Name][sec.Name] = nil + } + } + } + } + + // 4. Convert map to sorted slice var stores []models.ShoppingStore for storeName, categories := range storeMap { store := models.ShoppingStore{Name: storeName} |
