-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a54aa34
commit ef6b665
Showing
7 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
.venv | ||
venv | ||
__pycache__ | ||
secrets.yaml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |