feat: backend foundation setup with SQLite and FastAPI
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
backend/venv/
|
||||||
|
backend/data/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.env
|
||||||
|
.DS_Store
|
||||||
24
backend/database.py
Normal file
24
backend/database.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Create data directory if it doesn't exist
|
||||||
|
os.makedirs("data", exist_ok=True)
|
||||||
|
|
||||||
|
SQLALCHEMY_DATABASE_URL = "sqlite:///./data/inventory.db"
|
||||||
|
|
||||||
|
# connect_args={"check_same_thread": False} is required for SQLite in FastAPI/Starlette
|
||||||
|
engine = create_engine(
|
||||||
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
12
backend/main.py
Normal file
12
backend/main.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from . import models
|
||||||
|
from .database import engine
|
||||||
|
|
||||||
|
# Create the database tables
|
||||||
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
app = FastAPI(title="Inventory PWA API", version="0.1.0")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def read_root():
|
||||||
|
return {"message": "Inventory API is running"}
|
||||||
60
backend/models.py
Normal file
60
backend/models.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import datetime
|
||||||
|
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Float
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from .database import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
username = Column(String, unique=True, index=True)
|
||||||
|
role = Column(String, default="user") # 'admin' or 'user'
|
||||||
|
|
||||||
|
audit_logs = relationship("AuditLog", back_populates="user")
|
||||||
|
|
||||||
|
class Item(Base):
|
||||||
|
__tablename__ = "items"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
barcode = Column(String, unique=True, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
category = Column(String, index=True)
|
||||||
|
quantity = Column(Float, default=0.0)
|
||||||
|
min_quantity = Column(Float, default=1.0)
|
||||||
|
image_url = Column(String, nullable=True)
|
||||||
|
|
||||||
|
# Store labels template extracted data simply as JSON text for now
|
||||||
|
labels_data = Column(Text, nullable=True)
|
||||||
|
|
||||||
|
class AuditLog(Base):
|
||||||
|
__tablename__ = "audit_logs"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
|
user_id = Column(Integer, ForeignKey("users.id"))
|
||||||
|
action = Column(String) # e.g., 'CHECK_IN', 'CHECK_OUT', 'CREATE_ITEM'
|
||||||
|
target_item_id = Column(Integer, nullable=True)
|
||||||
|
quantity_change = Column(Float, nullable=True)
|
||||||
|
|
||||||
|
user = relationship("User", back_populates="audit_logs")
|
||||||
|
|
||||||
|
class Intervention(Base):
|
||||||
|
__tablename__ = "interventions"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String)
|
||||||
|
status = Column(String, default="ACTIVE")
|
||||||
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
|
|
||||||
|
items = relationship("InterventionItem", back_populates="intervention")
|
||||||
|
|
||||||
|
class InterventionItem(Base):
|
||||||
|
__tablename__ = "intervention_items"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
intervention_id = Column(Integer, ForeignKey("interventions.id"))
|
||||||
|
item_id = Column(Integer, ForeignKey("items.id"))
|
||||||
|
required_quantity = Column(Float, default=1.0)
|
||||||
|
checked_out_quantity = Column(Float, default=0.0)
|
||||||
|
|
||||||
|
intervention = relationship("Intervention", back_populates="items")
|
||||||
5
backend/requirements.txt
Normal file
5
backend/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
fastapi>=0.100.0
|
||||||
|
uvicorn[standard]>=0.23.0
|
||||||
|
sqlalchemy>=2.0.0
|
||||||
|
pydantic>=2.0.0
|
||||||
|
pydantic-settings>=2.0.0
|
||||||
Reference in New Issue
Block a user