diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-07-15 09:43:01 +0000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-07-16 02:50:42 +0000 |
| commit | b27b7e681952df44f05c60eea7218798b31d1fb4 (patch) | |
| tree | b71195de03f65ab57341ae1628b07009a6c7d22e /internal/store/shopping.go | |
| parent | 7d59a3020c9a98bb8af7240f584ac4c7813a46f6 (diff) | |
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VTUSAEKfsPc6WGDq45yPHD
Diffstat (limited to 'internal/store/shopping.go')
| -rw-r--r-- | internal/store/shopping.go | 82 |
1 files changed, 82 insertions, 0 deletions
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() +} |
