Skip to content

Commit

Permalink
auth with sample db
Browse files Browse the repository at this point in the history
  • Loading branch information
adityabhattad2021 committed Mar 15, 2024
1 parent a54aa34 commit ef6b665
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.venv
venv
__pycache__
secrets.yaml
Expand Down
2 changes: 2 additions & 0 deletions auth/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ecdsa==0.18.0
email_validator==2.1.1
exceptiongroup==1.2.0
fastapi==0.110.0
greenlet==3.0.3
h11==0.14.0
httpcore==1.0.4
httptools==0.6.1
Expand All @@ -33,6 +34,7 @@ PyYAML==6.0.1
rsa==4.9
six==1.16.0
sniffio==1.3.1
SQLAlchemy==2.0.28
starlette==0.36.3
typing_extensions==4.10.0
ujson==5.9.0
Expand Down
53 changes: 53 additions & 0 deletions auth/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from pydantic import BaseModel
from jose import JWTError, jwt
from passlib.context import CryptContext
from sqlalchemy.orm import Session

from . import crud,models,schemas
from .database import SessionLocal,engine

SECRET_KEY = "c93d119ae1ad7d4b19bce549a9e4f5251a37243a63378613aaab7ebd39d712b7"
ALGORITHM = "HS256"
Expand Down Expand Up @@ -54,6 +58,12 @@ class UserInDB(User):
hashed_password: str


def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()


def verify_password(plain_password, hashed_password):
Expand Down Expand Up @@ -148,3 +158,46 @@ async def read_own_items(
current_user: Annotated[User, Depends(get_current_active_user)]
):
return [{"item_id": "foo", "owner": current_user.username}]


@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=status.HTTP_400_BAD_REQUEST,
detail="Email Already Regestered"
)
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=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return db_user

@app.post('/users/{user_id}/items/',response_model=schemas.Item)
def create_item_for_user(
user_id:int,
item:schemas.ItemCreate,
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.Item])
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
34 changes: 34 additions & 0 deletions auth/src/crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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==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_items(db:Session,skip:int=0,limit:int=100):
return db.query(models.Item).offset(skip).limit(limit).all()


def create_user_item(db:Session,item:schemas.ItemCreate,user_id:int):
db_item = models.Item(**item.model_dump(),owner_id=user_id)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
14 changes: 14 additions & 0 deletions auth/src/database.py
Original file line number Diff line number Diff line change
@@ -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"

engine = create_engine(
SQLALCHEMY_DATABASE_URL,connect_args={"check_same_thread":False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


Base = declarative_base()
26 changes: 26 additions & 0 deletions auth/src/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from .database import Base


class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=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 Item(Base):
__tablename__ = "items"

id = Column(Integer, primary_key=True)
title = Column(String, index=True)
description = Column(String, index=True)
owner_id = Column(Integer, ForeignKey("users.id"))

owner = relationship("User", back_populates="items")
35 changes: 35 additions & 0 deletions auth/src/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pydantic import BaseModel


class ItemBase(BaseModel):
title: str
description:str|None = None


class ItemCreate(ItemBase):
pass


class Item(ItemBase):
id: int
owner_id: int

class Config:
orm_mode = True


class UserBase(BaseModel):
email: str


class UserCreate(UserBase):
password: str


class User(UserBase):
id: int
is_active: bool
items: list[Item] = []

class Config:
orm_mode = True

0 comments on commit ef6b665

Please sign in to comment.