feat(db): initialise sqlite support, list-db command.
This commit is contained in:
parent
1635a588e7
commit
7678178e36
71
src/pantry/core/sqlite.go
Normal file
71
src/pantry/core/sqlite.go
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "modernc.org/sqlite"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitDB(path string) (*sql.DB, error) {
|
||||||
|
db, err := sql.Open("sqlite", path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
schema := `
|
||||||
|
CREATE TABLE IF NOT EXISTS inventory (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
quantity REAL NOT NULL,
|
||||||
|
unit TEXT NOT NULL,
|
||||||
|
expiry DATE,
|
||||||
|
added_at DATETIME NOT NULL
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err = db.Exec(schema)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to apply schema %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListItems(db *sql.DB) ([]InventoryItem, error) {
|
||||||
|
rows, err := db.Query("SELECT name, quantity, unit, expiry, added_at FROM inventory ORDER BY name")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list items: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var items []InventoryItem
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var item InventoryItem
|
||||||
|
var expiry sql.NullString
|
||||||
|
var addedAtStr string
|
||||||
|
|
||||||
|
err = rows.Scan(&item.Name, &item.Quantity, &item.Unit, &expiry, &addedAtStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to scan row: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expiry.Valid {
|
||||||
|
parsed, err := time.Parse("2006-01-02", expiry.String)
|
||||||
|
if err != nil {
|
||||||
|
item.Expiry = &parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedTime, err := time.Parse("2006-01-02", addedAtStr)
|
||||||
|
if err != nil {
|
||||||
|
item.AddedAt = parsedTime
|
||||||
|
}
|
||||||
|
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
@ -12,8 +12,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
dbPath := filepath.Join(filepath.Dir(util.GetDefaultPath()), "inventory.db")
|
||||||
|
|
||||||
|
err := os.MkdirAll(filepath.Dir(dbPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to create data directory:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := core.InitDB(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to initialize database:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
dataFile := util.GetDefaultPath()
|
dataFile := util.GetDefaultPath()
|
||||||
err := os.MkdirAll(filepath.Dir(dataFile), 0755)
|
err = os.MkdirAll(filepath.Dir(dataFile), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to create data directory:", err)
|
fmt.Println("Failed to create data directory:", err)
|
||||||
return
|
return
|
||||||
|
|
@ -114,6 +129,23 @@ func main() {
|
||||||
case "list":
|
case "list":
|
||||||
inventory.ListItems()
|
inventory.ListItems()
|
||||||
|
|
||||||
|
case "list-db":
|
||||||
|
items, err := core.ListItems(db)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to list items:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) == 0 {
|
||||||
|
fmt.Println("Pantry is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Your pantry:")
|
||||||
|
for _, item := range items {
|
||||||
|
fmt.Printf("- %s: %.2f %s (%s)\n", item.Name, item.Quantity, item.Unit, util.DaysUntilExpiry(item.Expiry))
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unknown command:", command)
|
fmt.Println("Unknown command:", command)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue