From 0d66c6f50fc4106c858601fb7f9bcd8baead732d Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Mar 2022 16:13:50 +0300 Subject: [PATCH] sql_app: another api app to test In sql_app/main.py, new path fucntions are tested for database connections, quary. Signed-off-by: HeshamTB --- .gitignore | 1 + sql_app/.gitignore | 1 + sql_app/main.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ sql_app/models.py | 6 +++--- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 sql_app/.gitignore diff --git a/.gitignore b/.gitignore index 6f72a58..c55c0ea 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ server.key server.orig.key __pycache__ venv/ +*.db diff --git a/sql_app/.gitignore b/sql_app/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/sql_app/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/sql_app/main.py b/sql_app/main.py index e69de29..61ffeea 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -0,0 +1,53 @@ +from fastapi import Depends, FastAPI, HTTPException +from sqlalchemy.orm import Session + +from . import crud, models, schemas +from .database import SessionLocal, engine + +models.Base.metadata.create_all(bind=engine) + +app = FastAPI() + + +# Dependency +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +@app.post("/users/", response_model=schemas.User) +def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): + db_user = crud.get_user_by_email(db, email=user.email) + if db_user: + raise HTTPException(status_code=400, detail="Email already registered") + return crud.create_user(db=db, user=user) + + +@app.get("/users/", response_model=list[schemas.User]) +def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): + users = crud.get_users(db, skip=skip, limit=limit) + return users + + +@app.get("/users/{user_id}", response_model=schemas.User) +def read_user(user_id: int, db: Session = Depends(get_db)): + db_user = crud.get_user(db, user_id=user_id) + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + return db_user + + +@app.post("/users/{user_id}/items/", response_model=schemas.IotEntity) +def create_item_for_user( + user_id: int, item: schemas.IotEntityCreate, db: Session = Depends(get_db) +): + return crud.create_user_item(db=db, item=item, user_id=user_id) + + +@app.get("/items/", response_model=list[schemas.IotEntity]) +def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): + items = crud.get_items(db, skip=skip, limit=limit) + return items diff --git a/sql_app/models.py b/sql_app/models.py index 7568109..46ad3b0 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -12,7 +12,7 @@ class User(Base): hashed_password = Column(String) is_active = Column(Boolean, default=True) - items = relationship("Item", back_populates="owner") + #items = relationship("Item", back_populates="owner") class IotEntity(Base): @@ -20,6 +20,6 @@ class IotEntity(Base): id = Column(Integer, primary_key=True, index=True) description = Column(String, index=True) - owner_id = Column(Integer, ForeignKey("users.id")) + owner_id = Column(Integer, ForeignKey("user_accounts.id")) - owner = relationship("User", back_populates="items") + #owner = relationship("User", back_populates="items")