diff --git a/sql_app/__init__.py b/sql_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sql_app/crud.py b/sql_app/crud.py new file mode 100644 index 0000000..f4051da --- /dev/null +++ b/sql_app/crud.py @@ -0,0 +1,38 @@ +# CRUD (Create, Read, Update, Delete) from db + +from sqlalchemy.orm import Session + +from . import models, schemas + + +def get_user(db: Session, user_id: int): + return db.query(models.User).filter(models.User.id == user_id).first() + + +def get_user_by_email(db: Session, email: str): + return db.query(models.User).filter(models.User.email == email).first() + + +def get_users(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.User).offset(skip).limit(limit).all() + + +def create_user(db: Session, user: schemas.UserCreate): + fake_hashed_password = user.password + "notreallyhashed" + db_user = models.User(email=user.email, hashed_password=fake_hashed_password) + db.add(db_user) + db.commit() + db.refresh(db_user) + return db_user + + +def get_iot_entities(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.IotEntity).offset(skip).limit(limit).all() + + +def create_iot_entity(db: Session, item: schemas.IotEntityCreate, user_id: int): + db_item = models.Item(**item.dict(), owner_id=user_id) + db.add(db_item) + db.commit() + db.refresh(db_item) + return db_item diff --git a/sql_app/database.py b/sql_app/database.py new file mode 100644 index 0000000..c3c1892 --- /dev/null +++ b/sql_app/database.py @@ -0,0 +1,14 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db" +# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db" + +# For sqlite +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() diff --git a/sql_app/main.py b/sql_app/main.py new file mode 100644 index 0000000..e69de29 diff --git a/sql_app/models.py b/sql_app/models.py new file mode 100644 index 0000000..7568109 --- /dev/null +++ b/sql_app/models.py @@ -0,0 +1,25 @@ +from sqlalchemy import Boolean, Column, ForeignKey, Integer, String +from sqlalchemy.orm import relationship + +from .database import Base + + +class User(Base): + __tablename__ = "user_accounts" + + id = Column(Integer, primary_key=True, index=True) + email = Column(String, unique=True, index=True) + hashed_password = Column(String) + is_active = Column(Boolean, default=True) + + items = relationship("Item", back_populates="owner") + + +class IotEntity(Base): + __tablename__ = "iot_entities" + + id = Column(Integer, primary_key=True, index=True) + description = Column(String, index=True) + owner_id = Column(Integer, ForeignKey("users.id")) + + owner = relationship("User", back_populates="items") diff --git a/sql_app/schemas.py b/sql_app/schemas.py new file mode 100644 index 0000000..df6e6fb --- /dev/null +++ b/sql_app/schemas.py @@ -0,0 +1,37 @@ +from typing import Optional + +from pydantic import BaseModel + + +class IotEntityBase(BaseModel): + id: int + description: str + + +class IotEntityCreate(IotEntityBase): + pass + + +class IotEntity(IotEntityBase): + id: int + description: str + + class Config: + orm_mode = True + + +class UserBase(BaseModel): + email: str + + +class UserCreate(UserBase): + password: str + + +class User(UserBase): + id: int + is_active: bool + authorized_devices: list[IotEntity] = [] + + class Config: + orm_mode = True