Started with items

This commit is contained in:
2026-06-03 14:24:14 +02:00
parent 78731c1728
commit 0a44df319d
9 changed files with 279 additions and 24 deletions

52
storage/inventory.go Normal file
View File

@@ -0,0 +1,52 @@
package storage
import "MiauInv/models"
func AddItem(item models.Item) error {
_, err := DB.Exec(
"INSERT INTO items(name, category, description, total_quantity) VALUES(?,?,?,?)",
item.Name,
item.Category,
item.Description,
item.TotalQuantity,
)
return err
}
func GetItems() ([]models.Item, error) {
rows, err := DB.Query(`
SELECT id,name,category,description,total_quantity
FROM items
`)
if err != nil {
return nil, err
}
defer rows.Close()
var items []models.Item
for rows.Next() {
var item models.Item
err = rows.Scan(
&item.ID,
&item.Name,
&item.Category,
&item.Description,
&item.TotalQuantity,
)
if err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
}

View File

@@ -41,30 +41,42 @@ func InitDB(filepath string) error {
FOREIGN KEY(user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
category TEXT,
total_quantity INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS locations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS item_allocations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INTEGER NOT NULL,
location_id INTEGER,
project_id INTEGER,
quantity INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
category TEXT,
description TEXT,
total_quantity INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS locations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
description TEXT
);
CREATE TABLE IF NOT EXISTS stock (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INTEGER NOT NULL,
location_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
FOREIGN KEY(item_id) REFERENCES items(id),
FOREIGN KEY(location_id) REFERENCES locations(id)
);
CREATE TABLE IF NOT EXISTS project_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INTEGER NOT NULL,
project_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
FOREIGN KEY(item_id) REFERENCES items(id),
FOREIGN KEY(project_id) REFERENCES projects(id)
);
`
_, err = DB.Exec(schema)