From c7b18e70ea87c287240243aed867b3a8b51689cb Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Sun, 20 Sep 2026 08:11:38 +0000 Subject: Make shopping items editable: tap to rename, move store/section, delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tapping an item outside shopping mode now opens an edit sheet. User-added items can be renamed, moved between stores and sections, and deleted. PlanToEat items show why they cannot be edited — the client is read-only, so presenting fields that silently would not save is worse than saying so — and offer removal, which marks them checked, the same mechanism shopping mode already uses. This needed a store registry first. Stores previously existed only because some item referenced them by name, with Trello's list names quietly acting as the registry; removing Trello as a source made the gap visible. Without a registry you cannot move an item into an empty store, and moving the last item out of a store makes it vanish mid-edit. Migration 034 adds shopping_stores and shopping_sections, seeded from the stores currently in use, and gives items a section column — sections previously existed only as PlanToEat category strings, so a user item could not be filed under one at all. Registered stores and sections now appear in the list even when empty, which is the whole point of the registry. Two deliberate choices about destructive edges: * Renaming a store renames it and moves its items in one transaction. Items reference the store by name, so doing either alone orphans them. * Deleting a store or section keeps the items, moving them to Unassigned or clearing the section. Losing the list is far worse than re-filing an item. * An UPDATE matching no rows is an error, not a silent success, so a stale id cannot look like a saved edit. Section options are rebuilt per store in the edit sheet rather than shipped as one flat list, so a Costco item cannot be filed under a Safeway aisle. 18 new store tests, 11 handler tests. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018AwnALZL6bD4VBtATDVv3B --- internal/store/shopping_registry.go | 193 ++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 internal/store/shopping_registry.go (limited to 'internal/store/shopping_registry.go') diff --git a/internal/store/shopping_registry.go b/internal/store/shopping_registry.go new file mode 100644 index 0000000..f3cc515 --- /dev/null +++ b/internal/store/shopping_registry.go @@ -0,0 +1,193 @@ +package store + +import ( + "database/sql" + "errors" + "fmt" + "strings" +) + +// ShoppingStore is a registered store, with its sections. +type ShoppingStore struct { + ID int64 + Name string + Sections []ShoppingSection +} + +// ShoppingSection is an aisle or grouping within a store. +type ShoppingSection struct { + ID int64 + StoreID int64 + Name string +} + +// ErrStoreNotFound is returned when an operation names a store that is not +// registered. Callers surface this rather than silently creating one, so a +// typo does not quietly produce a second store. +var ErrStoreNotFound = errors.New("store not found") + +// GetShoppingStores returns every registered store with its sections. +func (s *Store) GetShoppingStores() ([]ShoppingStore, error) { + rows, err := s.db.Query(`SELECT id, name FROM shopping_stores ORDER BY sort_order, name`) + if err != nil { + return nil, err + } + defer func() { _ = rows.Close() }() + + var stores []ShoppingStore + byID := map[int64]int{} + for rows.Next() { + var st ShoppingStore + if err := rows.Scan(&st.ID, &st.Name); err != nil { + return nil, err + } + byID[st.ID] = len(stores) + stores = append(stores, st) + } + if err := rows.Err(); err != nil { + return nil, err + } + + secRows, err := s.db.Query(`SELECT id, store_id, name FROM shopping_sections ORDER BY sort_order, name`) + if err != nil { + return nil, err + } + defer func() { _ = secRows.Close() }() + + for secRows.Next() { + var sec ShoppingSection + if err := secRows.Scan(&sec.ID, &sec.StoreID, &sec.Name); err != nil { + return nil, err + } + if idx, ok := byID[sec.StoreID]; ok { + stores[idx].Sections = append(stores[idx].Sections, sec) + } + } + return stores, secRows.Err() +} + +// AddShoppingStore registers a store. Adding one that already exists is a +// no-op rather than an error — the intent ("there should be a store called +// this") is already satisfied. +func (s *Store) AddShoppingStore(name string) error { + name = strings.TrimSpace(name) + if name == "" { + return fmt.Errorf("store name is required") + } + _, err := s.db.Exec(`INSERT OR IGNORE INTO shopping_stores (name) VALUES (?)`, name) + return err +} + +// RenameShoppingStore renames a store and moves its items with it. +// +// Items reference a store by name, so the two have to change together or every +// item in the store is orphaned. Done in one transaction for that reason. +func (s *Store) RenameShoppingStore(id int64, newName string) error { + newName = strings.TrimSpace(newName) + if newName == "" { + return fmt.Errorf("store name is required") + } + + tx, err := s.db.Begin() + if err != nil { + return err + } + defer func() { _ = tx.Rollback() }() + + var oldName string + if err := tx.QueryRow(`SELECT name FROM shopping_stores WHERE id = ?`, id).Scan(&oldName); err != nil { + if errors.Is(err, sql.ErrNoRows) { + return ErrStoreNotFound + } + return err + } + if _, err := tx.Exec(`UPDATE shopping_stores SET name = ? WHERE id = ?`, newName, id); err != nil { + return err + } + if _, err := tx.Exec(`UPDATE user_shopping_items SET store = ? WHERE store = ?`, newName, oldName); err != nil { + return err + } + return tx.Commit() +} + +// DeleteShoppingStore removes a store. Its items are not deleted; they are +// moved to unassigned so nothing silently disappears with the store. +func (s *Store) DeleteShoppingStore(id int64) error { + tx, err := s.db.Begin() + if err != nil { + return err + } + defer func() { _ = tx.Rollback() }() + + var name string + if err := tx.QueryRow(`SELECT name FROM shopping_stores WHERE id = ?`, id).Scan(&name); err != nil { + if errors.Is(err, sql.ErrNoRows) { + return ErrStoreNotFound + } + return err + } + if _, err := tx.Exec( + `UPDATE user_shopping_items SET store = 'Unassigned', section = '' WHERE store = ?`, name); err != nil { + return err + } + // Sections go with the store via ON DELETE CASCADE. + if _, err := tx.Exec(`DELETE FROM shopping_stores WHERE id = ?`, id); err != nil { + return err + } + return tx.Commit() +} + +// AddShoppingSection adds a section to a store. +func (s *Store) AddShoppingSection(storeID int64, name string) error { + name = strings.TrimSpace(name) + if name == "" { + return fmt.Errorf("section name is required") + } + _, err := s.db.Exec( + `INSERT OR IGNORE INTO shopping_sections (store_id, name) VALUES (?, ?)`, storeID, name) + return err +} + +// DeleteShoppingSection removes a section, clearing it from its items rather +// than deleting them. +func (s *Store) DeleteShoppingSection(id int64) error { + tx, err := s.db.Begin() + if err != nil { + return err + } + defer func() { _ = tx.Rollback() }() + + var storeName, sectionName string + err = tx.QueryRow(` + SELECT st.name, se.name + FROM shopping_sections se JOIN shopping_stores st ON st.id = se.store_id + WHERE se.id = ?`, id).Scan(&storeName, §ionName) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil // already gone + } + return err + } + if _, err := tx.Exec( + `UPDATE user_shopping_items SET section = '' WHERE store = ? AND section = ?`, + storeName, sectionName); err != nil { + return err + } + if _, err := tx.Exec(`DELETE FROM shopping_sections WHERE id = ?`, id); err != nil { + return err + } + return tx.Commit() +} + +// ShoppingStoreNames returns just the registered store names, for pickers. +func (s *Store) ShoppingStoreNames() ([]string, error) { + stores, err := s.GetShoppingStores() + if err != nil { + return nil, err + } + names := make([]string, 0, len(stores)) + for _, st := range stores { + names = append(names, st.Name) + } + return names, nil +} -- cgit v1.2.3