From b27b7e681952df44f05c60eea7218798b31d1fb4 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Wed, 15 Jul 2026 09:43:01 +0000 Subject: refactor(store): extract shopping methods from sqlite.go into shopping.go Pure move: UserShoppingItem type, its CRUD methods, and SetShoppingItemChecked/GetShoppingItemChecks, plus their tests and setupTestStoreWithShopping helper, out of sqlite.go. No behavior change. Mirrors the existing internal/handlers/shopping.go naming. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01VTUSAEKfsPc6WGDq45yPHD --- internal/store/shopping.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 internal/store/shopping.go (limited to 'internal/store/shopping.go') diff --git a/internal/store/shopping.go b/internal/store/shopping.go new file mode 100644 index 0000000..40571ac --- /dev/null +++ b/internal/store/shopping.go @@ -0,0 +1,82 @@ +package store + +import "time" + +type UserShoppingItem struct { + ID int64 + Name string + Store string + Checked bool + CreatedAt time.Time +} + +// SaveUserShoppingItem saves a new user shopping item +func (s *Store) SaveUserShoppingItem(name, store string) error { + _, err := s.db.Exec(`INSERT INTO user_shopping_items (name, store) VALUES (?, ?)`, name, store) + return err +} + +// GetUserShoppingItems retrieves all user shopping items +func (s *Store) GetUserShoppingItems() ([]UserShoppingItem, error) { + rows, err := s.db.Query(`SELECT id, name, store, checked, created_at FROM user_shopping_items ORDER BY store, created_at DESC`) + if err != nil { + return nil, err + } + defer func() { _ = rows.Close() }() + + var items []UserShoppingItem + for rows.Next() { + var item UserShoppingItem + if err := rows.Scan(&item.ID, &item.Name, &item.Store, &item.Checked, &item.CreatedAt); err != nil { + return nil, err + } + items = append(items, item) + } + return items, rows.Err() +} + +// ToggleUserShoppingItem toggles the checked state of a user shopping item +func (s *Store) ToggleUserShoppingItem(id int64, checked bool) error { + _, err := s.db.Exec(`UPDATE user_shopping_items SET checked = ? WHERE id = ?`, checked, id) + return err +} + +// DeleteUserShoppingItem removes a user shopping item +func (s *Store) DeleteUserShoppingItem(id int64) error { + _, err := s.db.Exec(`DELETE FROM user_shopping_items WHERE id = ?`, id) + return err +} + +// SetShoppingItemChecked sets the checked state for an external shopping item +func (s *Store) SetShoppingItemChecked(source, itemID string, checked bool) error { + checkedInt := 0 + if checked { + checkedInt = 1 + } + _, err := s.db.Exec(` + INSERT INTO shopping_item_checks (source, item_id, checked, updated_at) + VALUES (?, ?, ?, CURRENT_TIMESTAMP) + ON CONFLICT(source, item_id) DO UPDATE SET checked = ?, updated_at = CURRENT_TIMESTAMP + `, source, itemID, checkedInt, checkedInt) + return err +} + +// GetShoppingItemChecks returns a map of item_id -> checked for a given source +func (s *Store) GetShoppingItemChecks(source string) (map[string]bool, error) { + rows, err := s.db.Query(`SELECT item_id, checked FROM shopping_item_checks WHERE source = ?`, source) + if err != nil { + return nil, err + } + defer func() { _ = rows.Close() }() + + checks := make(map[string]bool) + for rows.Next() { + var itemID string + var checked int + if err := rows.Scan(&itemID, &checked); err != nil { + return nil, err + } + checks[itemID] = checked == 1 + } + return checks, rows.Err() +} -- cgit v1.2.3